Optimal substructure + overlapping subproblems β efficient solutions
F(n) = F(n-1) + F(n-2) Base: F(0) = 0, F(1) = 1
Fibonacci β Tabulation
| Memoization | Tabulation | |
|---|---|---|
| Approach | Recursive+cache | Iterative+table |
| Subproblems | Only needed | All computed |
| Stack | O(n) recursion | No stack |
| Ease | Natural | Need order |
| Problem | Time | Space |
|---|---|---|
| Fibonacci | O(n) | O(n) |
| 0/1 Knapsack | O(nW) | O(nW) |
| LCS | O(mn) | O(mn) |
| Coin Change | O(nA) | O(A) |
0/1 Knapsack: Select items to maximize value within weight capacity.
LCS: Longest Common Subsequence of two strings.
Coin Change: Minimum coins to reach target amount.
Edit Distance: Min edits to transform one string to another.
1. Define state: what does dp[i] represent?
2. Recurrence: how to compute dp[i] from smaller?
3. Base cases: initial values.
4. Fill order: ensure dependencies come first.
5. Answer location in table.
Bioinformatics: DNA alignment (LCS, Edit Distance).
Finance: Portfolio optimization (Knapsack).
NLP: Viterbi algorithm, CYK parsing.
Routing: Shortest paths (Bellman-Ford = DP on graphs).