GateMCQSCIENTIFIC PREP LAB
PlanSign Up
ADVANCEDFrequently Tested 4 min read 22 views

Dynamic Programming Masterclass: 0/1 Knapsack, LCS & Matrix Chain Multiplication

Optimal substructure & overlapping subproblems, exact DP recurrences, time/space complexities, and pseudo-polynomial complexity of 0/1 Knapsack.

1. Greedy vs Dynamic Programming Decision Matrix

Classic ProblemGreedy Works?Optimal ParadigmTime ComplexitySpace
Fractional KnapsackYes (Sort by vi/wiv_i / w_i)GreedyO(nlog⁡n)\mathcal{O}(n \log n)O(1)\mathcal{O}(1)
0/1 KnapsackNo!Dynamic ProgrammingΘ(nW)\Theta(nW) (Pseudo-polynomial)O(W)\mathcal{O}(W)
Activity SelectionYes (Earliest finish time)GreedyO(nlog⁡n)\mathcal{O}(n \log n)O(1)\mathcal{O}(1)
Huffman Optimal Prefix CodeYes (Min-Heap merge 2 smallest)GreedyO(nlog⁡n)\mathcal{O}(n \log n)O(n)\mathcal{O}(n)
Matrix Chain MultiplicationNo!Dynamic ProgrammingΘ(n3)\Theta(n^3)Θ(n2)\Theta(n^2)
Longest Common SubsequenceNo!Dynamic ProgrammingΘ(mn)\Theta(mn)O(min⁡(m,n))\mathcal{O}(\min(m,n))
All-Pairs Shortest Path—Floyd-Warshall DPΘ(V3)\Theta(V^3)Θ(V2)\Theta(V^2)

2. Matrix Chain Multiplication Worked Insight

To multiply A1(p0×p1),A2(p1×p2),A3(p2×p3)A_1 (p_0 \times p_1), A_2 (p_1 \times p_2), A_3 (p_2 \times p_3), say A1(10×100),A2(100×5),A3(5×50)A_1(10 \times 100), A_2(100 \times 5), A_3(5 \times 50):

  • ((A1A2)A3)((A_1 A_2) A_3) costs (10⋅100⋅5)+(10⋅5⋅50)=5000+2500=7,500(10 \cdot 100 \cdot 5) + (10 \cdot 5 \cdot 50) = 5000 + 2500 = \mathbf{7,500} scalar multiplications.
  • (A1(A2A3))(A_1 (A_2 A_3)) costs (100⋅5⋅50)+(10⋅100⋅50)=25000+50000=75,000(100 \cdot 5 \cdot 50) + (10 \cdot 100 \cdot 50) = 25000 + 50000 = \mathbf{75,000} — a 10× penalty for wrong parenthesization!
Common Exam Mistake

Why is Θ(nW)\Theta(nW) for 0/1 Knapsack NOT polynomial time? Because the input size in bits to represent capacity WW is b=⌈log⁡2W⌉b = \lceil \log_2 W \rceil, so W=2bW = 2^b. Thus Θ(nW)=Θ(n⋅2b)\Theta(nW) = \Theta(n \cdot 2^b) is exponential in bit-length (pseudo-polynomial)!

Key Formulas Summary
Longest Common Subsequence (LCS) Recurrence
L[i,j]={1+L[i−1,j−1]Xi=Yjmax⁡(L[i−1,j],  L[i,j−1])Xi≠YjL[i,j] = \begin{cases} 1 + L[i-1, j-1] & X_i = Y_j \\ \max(L[i-1,j],\; L[i,j-1]) & X_i \neq Y_j \end{cases}
Note: Time: Theta(mn), Space: O(min(m, n)) with rolling 2-row optimization
Matrix Chain Multiplication (MCM)
m[i,j]=min⁡i≤k<j{m[i,k]+m[k+1,j]+pi−1pkpj}m[i,j] = \min_{i \le k < j} \{ m[i,k] + m[k+1,j] + p_{i-1} p_k p_j \}
Note: Time: Theta(n^3), Space: Theta(n^2) for n matrices
#Algorithms#Dynamic Programming#Knapsack#LCS#Matrix Chain#Floyd-Warshall

Practice Questions on This Chapter (3)

Q1GATE CS AlgorithmsMCQMEDIUM
+2M • -0.67

What is the length of the Longest Common Subsequence (LCS) of strings X="AGGTAB"X = \text{"AGGTAB"} and Y="GXTXAYB"Y = \text{"GXTXAYB"}?

⚡ Click an option above to verify instantly
Q2GATE CS 2016NATMEDIUM
+2M • 0 Neg

What is the minimum number of scalar multiplications required to multiply four matrices M1(10×20),  M2(20×50),  M3(50×1),  M4(1×100)M_1(10 \times 20),\; M_2(20 \times 50),\; M_3(50 \times 1),\; M_4(1 \times 100)?

Q3GATE CS 2019NATEASY
+2M • 0 Neg

What is the length of the Longest Common Subsequence (LCS) between strings X = "AGGTAB" and Y = "GXTXAYB"?

Previous TopicShortest Path & MST Algorithms: Dijkstra, Bellman-Ford, Prim & KruskalNext Topic Sorting Bounds, Stability, Inversions & P / NP / NP-Complete Reductions