Worked Examples: Course Review
Practice these representative problems to check your understanding of each paradigm.
1. Asymptotic Analysis
❓ Compare the following: $n!$ vs $2^n$ vs $n^3$.
💡 $n^3 = O(2^n)$ and $2^n = O(n!)$. Factorial grows faster than exponential, which grows faster than polynomial.
2. Master Theorem
❓ Solve $T(n) = 2T(n/2) + n$.
💡 $a=2, b=2, f(n)=n$. $n^{\log_b a} = n^{\log_2 2} = n^1$. Since $f(n) = \Theta(n)$, this is Case 2. $T(n) = \Theta(n \lg n)$.
3. Greedy Choice
❓ Why does a greedy approach work for fractional knapsack but not 0/1?
💡 Fractional allows local optimization per unit weight (Greedy). 0/1 requires considering all combinations because items are indivisible (needs DP).
4. Dynamic Programming (LCS)
❓ What is the LCS of "ABCBDAB" and "BDCABA"?
💡 Length is 4. One possible LCS is "BDAB". Use the table $c[i,j] = c[i-1,j-1]+1$ if match, else $\max(c[i,j-1], c[i-1,j])$.
5. Graph Pathfinding
❓ When would you choose Bellman-Ford over Dijkstra?
💡 When the graph contains negative edge weights or when you need to detect negative weight cycles.
6. NP-Completeness
❓ If $P = NP$, how does it affect cryptography?
💡 If $P=NP$, most encryption schemes (like RSA) could be broken easily because "hard" problems like factoring would have polynomial solutions.