Design and Analysis of Algorithms · Unit 5 · 8 hrs
Dynamic Programming
Exam-focused notes for Dynamic Programming (Design and Analysis of Algorithms, CSC325): what the TU syllabus asks and how it has actually been tested, with 10 solved past questions from this unit.
Dynamic Programming in short
Dynamic programming (DP) solves a problem by breaking it into overlapping sub-problems, solving each sub-problem exactly once, and storing the result in a table so it never has to be recomputed. This unit contrasts DP with greedy and plain recursion, then applies it to five classic optimization problems, before closing with memoization, the top-down way of getting the same result.
What this unit covers
- Greedy Algorithms vs Dynamic Programming, Recursion vs Dynamic Programming, Elements of DP Strategy
- DP Algorithms: Matrix Chain Multiplication, String Editing, Zero-One Knapsack Problem, Floyd Warshwall Algorithm, Travelling Salesman Problem and their Analysis
- Memoization Strategy, Dynamic Programming vs Memoization
DP Algorithms
2082 · 10 marks
Distinguish between dynamic programming and memorization. Parenthesize the matrices A(30 × 1), B(1 × 40), C(40 × 10) and A(10 × 15), for computing matrix multiplication using dynamic programming.[10]
Show solved answerHide answer
Dynamic Programming vs Memoization & Matrix Chain Multiplication
Part 1: Dynamic Programming vs Memoization
| Feature | Dynamic Programming | Memoization |
|---|---|---|
| Approach | Bottom-up | Top-down |
| Implementation | Iterative (fills a table) | Recursive with a cache |
| Sub-problems solved | All sub-problems computed | Only the sub-problems actually needed |
| Overhead | No recursion overhead | Recursion call/return overhead |
| Storage | Results stored in a table filled in order | Results stored in a lookup table on first computation |
| Nature | A general problem-solving technique (optimal substructure + overlapping subproblems) | An optimization applied to recursion by caching results |
Key idea: Memoization is essentially the top-down (recursive) way of realizing dynamic programming; it "remembers" previously computed results so they are not recomputed.
Part 2: Matrix Chain Parenthesization
Given data
- $A = 30 \times 1$
- $B = 1 \times 40$
- $C = 40 \times 10$
- $D = 10 \times 15$ (the last matrix, written A(10 × 15) in the question)
Dimension array: $$p = [30,,1,,40,,10,,15]$$ $p_0=30,\ p_1=1,\ p_2=40,\ p_3=10,\ p_4=15,\quad n=4$
Recurrence
$$m[i][j]=\min_{i\le k<j}{m[i][k]+m[k+1][j]+p_{i-1},p_k,p_j}$$ Base case $m[i][i]=0$.
Length $l=2$
$m[1][2]=0+0+30\cdot1\cdot40=1200,\ s=1$
$m[2][3]=0+0+1\cdot40\cdot10=400,\ s=2$
$m[3][4]=0+0+40\cdot10\cdot15=6000,\ s=3$
Length $l=3$
$m[1][3]$:
- $k=1:\ 0+400+30\cdot1\cdot10=400+300=700$
- $k=2:\ 1200+0+30\cdot40\cdot10=1200+12000=13200$
$m[1][3]=700,\ s=1$
$m[2][4]$:
- $k=2:\ 0+6000+1\cdot40\cdot15=6000+600=6600$
- $k=3:\ 400+0+1\cdot10\cdot15=400+150=550$
$m[2][4]=550,\ s=3$
Length $l=4$
$m[1][4]$:
- $k=1:\ m[1][1]+m[2][4]+p_0p_1p_4=0+550+30\cdot1\cdot15=550+450=1000$
- $k=2:\ m[1][2]+m[3][4]+p_0p_2p_4=1200+6000+30\cdot40\cdot15=7200+18000=25200$
- $k=3:\ m[1][3]+m[4][4]+p_0p_3p_4=700+0+30\cdot10\cdot15=700+4500=5200$
$$m[1][4]=\min(1000,25200,5200)=1000,\quad s[1][4]=1$$
Optimal parenthesization (from s-table)
- $s[1][4]=1 \Rightarrow (A_1)(A_2A_3A_4)$
- $s[2][4]=3 \Rightarrow (A_2A_3)(A_4)$
- $s[2][3]=2 \Rightarrow (A_2)(A_3)$
Putting it together: $$\big(A,((B,C),D)\big)$$
Result
- Minimum number of scalar multiplications = $1000$
- Optimal order: $(A,((B,C),D))$
Verification of the count:
- $B\cdot C:\ 1\cdot40\cdot10=400$, result $1\times10$
- $(BC)\cdot D:\ 1\cdot10\cdot15=150$, result $1\times15$
- $A\cdot(BCD):\ 30\cdot1\cdot15=450$, result $30\times15$
- Total $=400+150+450=1000$ ✓
2082 · 5 marks
How does 0/1 Knapsack problem differ from fractional one? Find the minimum vertex cover in the following graph. [5+0]
2081 · 10 marks
Differentiate between Dynamic Programming and Memoization. Compute the Shortest Path Between Every Pair in the Following Graphs Using Floyd Warshall Algorithm. [10]
2081 · 5 marks
Define order statistics problem. Find the edit distance between 'cat' and 'car' using dynamic programming. [5]
Show solved answerHide answer
Order Statistics Problem and Edit Distance
Part 1: Order Statistics Problem
Definition:
Given a set (or array) $A$ of $n$ elements drawn from a totally ordered set, the $i$-th order statistic is the $i$-th smallest element of $A$.
- Input: An array $A$ of $n$ elements and an integer $i$ where $1 \le i \le n$.
- Output: The element $e \in A$ that is greater than exactly $i-1$ other elements of $A$.
Special cases:
- $i = 1$ → minimum element
- $i = n$ → maximum element
- $i = \lfloor (n+1)/2 \rfloor$ → median
The selection problem is the general problem of finding the $i$-th order statistic, solvable in expected linear time $O(n)$ (randomized-select) or worst-case $O(n)$ (median-of-medians).
Part 2: Edit Distance between 'cat' and 'car'
Given Data
- $X = \text{"cat"}$, length $m = 3$
- $Y = \text{"car"}$, length $n = 3$
- Operation costs: insert = delete = replace = $1$
Recurrence
$$ dp[i][j] = \begin{cases} j & i = 0 \ i & j = 0 \ dp[i-1][j-1] & X[i] = Y[j] \ 1 + \min\big(dp[i-1][j],, dp[i][j-1],, dp[i-1][j-1]\big) & X[i] \neq Y[j] \end{cases} $$
Filling the Table
Initialization: first row = $0,1,2,3$; first column = $0,1,2,3$.
Row i=1 (X='c'):
- $dp[1][1]$: c=c → $dp[0][0]=0$
- $dp[1][2]$: c≠a → $1+\min(2,0,1)=1$
- $dp[1][3]$: c≠r → $1+\min(3,1,2)=2$
Row i=2 (X='a'):
- $dp[2][1]$: a≠c → $1+\min(0,2,1)=1$
- $dp[2][2]$: a=a → $dp[1][1]=0$
- $dp[2][3]$: a≠r → $1+\min(2,0,1)=1$
Row i=3 (X='t'):
- $dp[3][1]$: t≠c → $1+\min(1,3,2)=2$
- $dp[3][2]$: t≠a → $1+\min(0,2,1)=1$
- $dp[3][3]$: t≠r → $1+\min(1,1,0)=1$
Final DP Table
| c | a | r | ||
|---|---|---|---|---|
| 0 | 1 | 2 | 3 | |
| c | 1 | 0 | 1 | 2 |
| a | 2 | 1 | 0 | 1 |
| t | 3 | 2 | 1 | 1 |
Result
$$\boxed{\text{Edit Distance} = dp[3][3] = 1}$$
Interpretation: Only one replace operation is required - substitute 't' with 'r' to turn "cat" into "car".
2079 · 5 marks
Find the edit distance between the string "ARTIFICIAL" and "NATURAL" Using dynamic programming. [5]
Greedy Algorithms vs Dynamic Programming, Recursion vs Dynamic Programming, Elements of DP Strategy
2080 · 10 marks
Write down the advantages of dynamic programming over greedy strategy. Find optimal bracketing to multiply 4 matrices of order $2, 3, 4, 2, 5$. [10]
2078 · 10 marks
Explain in brief about the Dynamic Programming Approach for algorithm design. How it differs with recursion? Explain the algorithm for solving the 0/1 Knapsack problem using the dynamic programming approach and explain its complexity.[10]
2076 · 5 marks
What do you mean by Dynamic programming strategy? Explain the element of DP. [5]
Memoization Strategy, Dynamic Programming vs Memoization
2078 · 5 marks
What do you mean by memorization strategy? Compare memorization with dynamic programming. [5]
Make Unit 5 stick
Practice CSC325 with flashcards & quizzes