Dynamic Programming & Greedy Technique - Notes
DYNAMIC PROGRAMMING (DP)
Definition:
Dynamic Programming is a method for solving complex problems by breaking them down into simpler subproblems and
storing the results of already solved subproblems to avoid redundant work.
Key Concepts:
1. Overlapping Subproblems
Example: Fibonacci sequence
2. Optimal Substructure
Example: Shortest path in a graph
3. Memoization (Top-Down)
Store results in a table (usually recursion + cache)
4. Tabulation (Bottom-Up)
Build table from smallest subproblem up to the final solution
Steps to Solve a DP Problem:
1. Define the state (dp[i] or dp[i][j])
2. Define the recurrence relation
3. Identify base cases
4. Implement using top-down (memoization) or bottom-up (tabulation)
5. Optimize space if possible
Common Examples:
- Fibonacci Numbers - 1D DP: dp[n] = dp[n-1] + dp[n-2]
- 0/1 Knapsack - 2D DP: dp[i][w] = max(dp[i-1][w], value + dp[i-1][w-weight])
- Longest Common Subsequence - 2D DP: dp[i][j] = dp[i-1][j-1] + 1 (if match)
- Longest Increasing Subsequence - 1D DP
- Matrix Chain Multiplication - Interval DP
- Coin Change - 1D DP: dp[i] += dp[i - coin] or dp[i] = min(dp[i], 1 + dp[i - coin])
- Edit Distance - 2D DP: Insert, delete, or replace
GREEDY TECHNIQUE
Definition:
Greedy algorithms make locally optimal choices at each step, hoping to find a global optimum.
Characteristics:
1. Greedy Choice Property
2. No need to solve all subproblems
3. Faster and easier, but only works if the problem has the greedy property
Common Greedy Examples:
- Activity Selection - Sort by end time
- Fractional Knapsack - Take max value/weight ratio first
- Huffman Encoding - Merge lowest frequency
- Job Sequencing - Sort by profit and deadline
- Coin Change - Only works when denominations are canonical
- Kruskal's Algorithm - Minimum Spanning Tree
Dynamic Programming & Greedy Technique - Notes
- Prim's Algorithm - MST using priority queue
- Dijkstra's Algorithm - Shortest path using greedy strategy
DP vs Greedy:
- Subproblem Reuse: DP - Yes, Greedy - No
- Time Complexity: DP - Higher, Greedy - Lower
- Optimal for all cases: DP - Yes, Greedy - Not always
- Use Case: DP - overlapping subproblems, Greedy - greedy property
How to Decide Between DP and Greedy?
- Use DP if optimal substructure and overlapping subproblems exist
- Use Greedy if local optimum leads to global optimum
- Test greedy on small input to verify
Practice Problems:
Dynamic Programming:
- Fibonacci
- 0/1 Knapsack
- Longest Common Subsequence
- Coin Change
- Edit Distance
- Matrix Chain Multiplication
Greedy:
- Activity Selection
- Fractional Knapsack
- Job Sequencing
- Huffman Coding
- Kruskal's and Prim's Algorithm