Dynamic Programming
Algorithms · Advanced Placement
Dynamic Programming: the big picture
Dynamic programming applies when a problem has optimal substructure, meaning an optimal solution is composed of optimal solutions to subproblems, and overlapping subproblems, meaning the same subproblems recur so that solving them once and storing the results avoids exponential recomputation. The method has two implementations: top-down with memoisation, which preserves the recursive formulation and computes only the subproblems actually reached, and bottom-up tabulation, which fills a table in an order guaranteeing dependencies are ready and usually has lower constant overhead. The design procedure is to characterise the structure of an optimal solution, define the value recursively, compute the value, and then reconstruct the solution itself, which requires either storing the choices or recomputing them from the value table. Classic problems illustrate the range: rod cutting and the knapsack show optimal substructure over a single dimension of resource; longest common subsequence and edit distance fill a two-dimensional table and underlie file comparison and sequence alignment; matrix chain multiplication optimises over parenthesisation; and optimal binary search trees minimise expected search cost given access frequencies. The distinction from greedy algorithms is that dynamic programming considers all choices at each step while greedy commits to one.
Optimal Substructure, Memoisation & Tabulation
Dynamic programming applies when two conditions hold: an optimal solution is built from optimal solutions to subproblems, and the same subproblems keep recurring. The second condition is what makes it pay — a naive recursion recomputes them exponentially often, and storing the answers collapses that to the number of distinct subproblems. Top-down memoisation keeps the recursive shape and computes only what is reached; bottom-up tabulation fills a table in dependency order with lower overhead. Both give the same answers; the choice is practical.
{{illustration:algos-l11-f1}}
{{illustration:algos-l11-f2}}
Key relations
- The dynamic programming conditions. aᵢ = bⱼ: L(i,j) = L(i−1,j−1) + 1 ; else: L(i,j) = max(L(i−1,j), L(i,j−1))
Knapsack, Edit Distance & Matrix Chain Order
The classic dynamic programs are worth knowing individually because each illustrates a different structure. The knapsack optimises over one resource dimension and is where the distinction between the 0-1 and fractional versions matters. Edit distance fills a two-dimensional table and underlies spell checking and sequence alignment. Matrix chain multiplication optimises over parenthesisation, where the subproblems are intervals rather than prefixes — and the saving can be enormous, since multiplication order changes the operation count by orders of magnitude.
{{illustration:algos-l11-f3}}
{{illustration:algos-l11-f4}}
Key relations
- Knapsack and matrix chain recurrences. K(i,w) = max(K(i−1,w), vᵢ + K(i−1, w − wᵢ)) ; M(i,j) = min_(i ≤ k < j)(M(i,k) + M(k+1,j) + p_(i−1)pₖpⱼ)