Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bottom Up DP from Top Down DP

I was thinking if there was some generalized way of converting top down Dynamic Programing to Bottom Up Programming.

Can we think of some mechanism which gives formal way by which top down DP can we converted to bottom up DP.

NOTE: I am beginner in Dynamic Programing, and few problems I have seen in which a top down approach is converted to bottom up approach are very different. So I am not sure if a generalized way is possible.

By generalized I would mean, how should arrays be initialized, what should be size of array and how many dimensions should array have.

like image 207
coder hacker Avatar asked May 15 '14 14:05

coder hacker


2 Answers

The execution of a dynamic program can be viewed as a directed acyclic graph, where each vertex is a subproblem, and arcs indicate that the solution to a particular subproblem was required to compute the solution for another subproblem. What a top-down recursive program with memoization is doing is, in essence, topologically sorting the subgraph of this graph reachable from the root problem via depth-first search. To convert it to a bottom-up approach, you need to work out a suitable topological order yourself, which will vary from problem to problem.

like image 172
David Eisenstat Avatar answered Oct 27 '22 02:10

David Eisenstat


Top Down solution is usually better, because it only solves the necessary subproblems. Converting a bottom up solution to top down is pretty straightforward, you just need to calculate and store the subproblems on-demand instead of precalculating all off them. The other way can be tricky, because you need to know which subproblems to solve. Depending on the problem, the difficulty of finding the subproblems without inspecting the upper problems can range from easy to impossible. For example consider the following: you have an infinite colored, weighted graph, and its verticles are colored with 10 colors. Whats the distance to the closest blue verticle from a given A verticle. Solving it up-down is possible, but going bottom-up is impossible as you would need to start from all blue verticles of the graph.

like image 32
Kicsi Avatar answered Oct 27 '22 00:10

Kicsi