In what way is a recursive design different from top-down design
Tabulation vs MemoizationPrerequisite Dynamic Programming, How to solve Dynamic Programming problems?
Before getting to the definitions of the above two terms consider the following statements: Attention reader! Dont stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course. In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.
Both versions say the same thing, the difference simply lies in the way of conveying the message and thats exactly what Bottom Up and Top Down DP do. Version 1 can be related to as Bottom Up DP and Version-2 can be related as Top Down DP. Tabulation Method Bottom Up Dynamic Programming As the name itself suggests starting from the bottom and accumulating answers to the top. Lets discuss in terms of state transition. Lets describe a state for our DP problem to be dp[x] with dp[0] as base stateand dp[n] as our destination state.So, we need to find the value of destination state i.e dp[n]. Now, Why do we call it tabulation method? To know this lets first write some code to calculate the factorial of a number using bottom up approach.Once, again as our general procedure to solve a DP we first define a state. In this case, we define a state as dp[x], where dp[x] is to find the factorial of x. Now, it is quite obvious that dp[x+1] = dp[x] * (x+1) The above code clearly follows the bottom-up approach as it starts its transition from the bottom-most base case dp[0] and reaches its destination state dp[n]. Here, we may notice that the dp table is being populated sequentially and we are directly accessing the calculated states from the table itself and hence, we call it tabulation method. Memoization Method Top Down Dynamic Programming Once, again lets describe it in terms of state transition. If we need to find the value for some state say dp[n] and instead of starting from the base state that i.e dp[0] we ask our answer from the states that can reach the destination state dp[n] following the state transition relation, then it is the top-down fashion of DP. Here, we start our journey from the top most destination state and compute its answer by taking in count the values of states that can reach the destination state, till we reach the bottom most base state. Once again, lets write the code for the factorial problem in the top-down fashion As we can see we are storing the most recent cache up to a limit so that if next time we got a call from the same state we simply return it from the memory. So, this is why we call it memoization as we are storing the most recent state values. In this case the memory layout is linear thats why it may seem that the memory is being filled in a sequential manner like the tabulation method, but you may consider any other top down DP having 2D memory layout like Min Cost Path, here the memory is not filled in a sequential manner. This article is contributed by Nitish Kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article using or mail your article to . See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Article Tags :
Dynamic Programming
Practice Tags :
Dynamic Programming
Read Full Article
|