What is Dynamic Programming (DP)?
Dynamic Programming is a technique used in algorithms to solve complex problems by breaking
them down into smaller subproblems, solving each subproblem once, and storing their solutions to
avoid recomputing.
Key Concepts in DP
- Overlapping Subproblems: Same subproblems are solved multiple times.
- Optimal Substructure: The overall solution depends on solutions to smaller parts.
Dynamic Programming Process
1. Define the subproblems.
2. Write a recurrence relation.
3. Store solutions.
4. Build the solution from smaller ones.
Two Main Ways to Implement DP
- Top-Down (Memoization): Solve big problem by recursively solving smaller ones and caching
results.
- Bottom-Up (Tabulation): Solve all smaller subproblems first, build up to the big one iteratively.
Example: Fibonacci Numbers
Fib(0) = 0
Fib(1) = 1
Fib(n) = Fib(n-1) + Fib(n-2)
DP Table for Fibonacci
n :01234567
Fib : 0 1 1 2 3 5 8 13
Characteristics of a DP Problem
- Overlapping subproblems
- Optimal substructure
Popular Problems Solved Using DP
- Longest Common Subsequence (LCS)
- Matrix Chain Multiplication
- 0/1 Knapsack Problem
- Coin Change Problem
- Edit Distance (Levenshtein Distance)
Textual Diagram of DP
[Small Solution] -> [Small Solution] -> [Small Solution] -> [Bigger Solution]
Why is Dynamic Programming Powerful?
- Reduces time complexity from exponential to polynomial.
- Used heavily in AI, bioinformatics, games, optimization, etc.