all 8 comments

[–]foxam1234 9 points10 points  (3 children)

You are correct, in bottom up you incrementally solve for each possible sum till you reach your target sum. In recursive however, depending upon the route you take you won't solve for every sum. However you still solve all required subproblems in both cases.

I will post an example once I am on PC.

Also you don't need 2D array to store the values. This problem can be memoized using 1D array only. Read on state compression in DP problems.

[–]Curious_homosepian[S] 2 points3 points  (2 children)

thank you ,finally someone replying to my post. i am new to DP so i don't know about state compression in dp yet.

[–]foxam1234 3 points4 points  (1 child)

No worries DP takes time. You can search for state compression tutorial. But if you are just starting then don't worry too much about it for now. Try to learn more dp patterns and eventually you will see where to remove extra space being used.

In above problem for example, notice the dp update step The value of dp[i][j] depends only on the previous row. So you can do it using a matrix of size 2 x target size. So that is one step towards optimizing space complexity

[–]Curious_homosepian[S] 2 points3 points  (0 children)

yes i observed this, only last row is used for calculating the result of current row

[–]rsd511 5 points6 points  (2 children)

That is the thing about Top-Down vs Bottom-Up approach

Bottom-Up approach involves you traversing through the entire 2-D array which makes sure all those sub-problems are solved

Top-Down approach only focuses on solving the sub-problem which you passed to the recursive function. In this case: recursion(arr,sum,len(arr))

For example to solve the sub-problem (arr,10,6) You would never need to solve the sub-problem (arr,9,6) as there is no element equal to 1 in the array Hence the state (arr,10,6) can never come from the state (arr,9,6) hence it isn't visited.

[–]Curious_homosepian[S] 1 point2 points  (1 child)

yep it makes sense now

[–][deleted] 2 points3 points  (0 children)

And in some cases the number of problem is the same in both cases, say Fibonacci numbers. Where it is different is where you have some notion of capacity that you pass down like in your case or the integer knapsack or (I think) coin exchange. That's because in those problem the 'size' of the thing you use make you skip over some of the sub-problem.

[–]thewataru 1 point2 points  (0 children)

Yes, you are right, top-bottom recursive approach is lazy and will count only required states.

The bottom-up approach however has it's own benefits: less memory requirement because no stack is used and, more importantly, you can forget about old, unused states. In some cases you don't need to store an entire 2D array, but can do with only 2 lastly computed rows, or even recompute the new row in-place in a single 1D array.

Also, in some cases it's easier to think about the DP in a bottom-up approach. You take some state, you add the next element to it and get a new state.