Memoization and Dynamic Programming
Explore how memoization and dynamic programming optimize recursive algorithms by using depth-first search and evaluation order. Learn to implement these techniques in Java to efficiently solve graph problems like longest path in DAGs, leveraging dependency graphs and topological sorting.
We'll cover the following...
Our topological sort algorithm is arguably the model for a wide class of dynamic programming algorithms. Recall that the dependency graph of a recurrence has a vertex for every recursive subproblem and an edge from one subproblem to another if evaluating the first subproblem requires a recursive evaluation of the second. The dependency graph must be acyclic, or the naïve recursive algorithm would never halt.
Evaluating any recurrence with memoization is exactly the same as performing a depth-first search of the dependency graph. In particular, a vertex of the dependency graph is marked if the value of the corresponding subproblem has already been computed. The black box subroutines and are proxies for the actual value computation.
Dynamic programming and reverse topological ordering
Carrying this analogy further, evaluating a recurrence using dynamic programming is the same as evaluating all subproblems in the dependency graph of the recurrence in reverse topological order—every subproblem is considered after the subproblems it depends on. Thus, every dynamic programming algorithm is equivalent to a postorder traversal of the dependency graph of its underlying recurrence.
Algorithm
...