Dynamic Programming
Why This Chapter Matters
DP is tested in GATE every year — 6-10 marks. Classical problems (LCS, LIS, 0/1 Knapsack, Matrix Chain, Edit Distance, Coin Change) with recurrences and time complexity analysis are regularly asked.
Prerequisites
Core Concepts
1. What is Dynamic Programming?
DP is an algorithmic technique for solving optimization problems with:
Approaches:
2. Classical DP Problems
#### Fibonacci (Basic DP example)
Naive: O(2^n) | With DP: O(n) time, O(n) space (or O(1) with two variables)
#### Longest Common Subsequence (LCS)
Given strings X[1..m] and Y[1..n], find length of longest common subsequence.
Recurrence:
Time: O(mn), Space: O(mn)
Example: X = "ABCBDAB", Y = "BDCAB"
LCS length = 4 (BCAB or BDAB)
#### Longest Increasing Subsequence (LIS)
Given array A[1..n], find length of longest strictly increasing subsequence.
O(n^2) DP:
O(n log n) using Patience Sorting (with binary search on tails array)
Example: A = [3, 1, 4, 1, 5, 9, 2, 6]
LIS = 4 (1, 4, 5, 9 or 1, 4, 5, 6)
#### 0/1 Knapsack
Given n items with weights w[] and values v[], and capacity W.
Choose items to maximize value without exceeding weight W.
Recurrence:
Time: O(nW), Space: O(nW)
IMPORTANT: 0/1 Knapsack is NOT polynomial in input size (W can be exponentially large) — it's pseudo-polynomial.
Fractional Knapsack: Greedy works (take fractions allowed). ONLY 0/1 Knapsack requires DP.
#### Matrix Chain Multiplication
Given matrices A1, A2, ..., An, find optimal order to minimize total multiplications.
Key: Multiplying p x q matrix with q x r matrix costs pqr multiplications.
Recurrence:
Time: O(n^3), Space: O(n^2)
#### Edit Distance (Levenshtein Distance)
Minimum operations (insert, delete, replace) to convert string X to string Y.
Recurrence:
Operations: dp[i-1][j] + 1 = delete | dp[i][j-1] + 1 = insert | dp[i-1][j-1] + 1 = replace
Time: O(mn), Space: O(mn)
#### Coin Change
Minimum coins to make amount S using coins c[1..n] (unlimited supply):
Time: O(S*n)
Number of ways to make amount S:
Solved Examples (GATE-style)
Example 1 — LCS
Q: Find LCS length of "AGGTAB" and "GXTXAYB".
LCS = "GTAB", length = 4
Example 2 — 0/1 Knapsack
Q: Items: (w=1, v=1), (w=3, v=4), (w=4, v=5), (w=5, v=7). Capacity W=7.
Take items 2 and 1: weight=4, value=5? No...
Optimal: Items (w=3,v=4) + (w=4,v=5) = weight 7, value 9
Or: (w=3,v=4) + (w=1,v=1) = weight 4, value 5. Not optimal.
Best: items with w=3 and w=4: value = 9
Example 3 — Edit Distance
Q: Edit distance between "KITTEN" and "SITTING"?
k->s (replace), i=i (same), t=t (same), t->t (same), e->i (replace), n->n (same), (add g)
Wait — standard DP gives: 3 (replace k->s, replace e->i, insert g)
PYQs (GATE CSE)
GATE 2024: For LCS of "ABCDE" and "ABEDC", what is the length?
DP table computation: LCS = "ABDC" or "ABEC", length = 4
GATE 2023: Bellman-Ford vs Dijkstra: which can handle negative weights?
Bellman-Ford handles negative weights. Dijkstra cannot.
GATE 2022: Matrix chain with dimensions 10x30, 30x5, 5x60. Find optimal cost.
Option 1: (A1 x A2) x A3 = 10x30x5 + 10x5x60 = 1500 + 3000 = 4500
Option 2: A1 x (A2 x A3) = 30x5x60 + 10x30x60 = 9000 + 18000 = 27000
Optimal: 4500
GATE 2021: In 0/1 Knapsack with n items and capacity W, time complexity of DP solution?
O(nW) — fill dp[n+1][W+1] table.
MCQ Practice
Q1. Which problem has O(n^3) DP solution?
(A) LCS (B) LIS (C) Matrix Chain Multiplication (D) Coin Change
Answer: C
Q2. 0/1 Knapsack differs from fractional Knapsack because:
(A) Greedy works for 0/1 (B) DP needed for 0/1, greedy for fractional (C) Both use DP (D) Both use greedy
Answer: B
Q3 (Hard). Minimum number of scalar multiplications to compute A1.A2.A3.A4 where dimensions are 5x4, 4x6, 6x2, 2x7?
Use matrix chain DP...
Optimal: ((A1.A2).A3).A4 = (5x4x6 + 5x6x2) + 5x2x7 = (120+60) + 70 = 250

