DAA E-Content - Module 5 Dynamic Programming
DAA E-Content - Module 5 Dynamic Programming
Module-V: Dynamic Programming Dr. A K Yadav Design and Analysis of Algorithm 1/74
School of Computer Science and Engineering
Contents
Dynamic programming 3
Coin Change Problem 9
Longest Common Subsequence 14
Traveling Salesman Problem 26
Multi stage graph 27
Floyd Warshall algorithm 28
0-1 Knapsack Problem 36
Matrix Chain Multiplication(MCM) 43
Optimal binary search trees 53
Binomial Coefficient 73
Module-V: Dynamic Programming Dr. A K Yadav Design and Analysis of Algorithm 2/74
School of Computer Science and Engineering
Dynamic programming
▶ Solves problems by combining the solutions to sub-problems
▶ Sub-problems are overlapping
▶ Doesn’t solve overlapping sub-problems again and again
▶ Behave like Divide and Conquer if sub-problems are not
overlapping
▶ Used in optimization problems
▶ Can be used either top-down with memoization or bottom-up
method
Module-V: Dynamic Programming Dr. A K Yadav Design and Analysis of Algorithm 3/74
School of Computer Science and Engineering
Module-V: Dynamic Programming Dr. A K Yadav Design and Analysis of Algorithm 4/74
School of Computer Science and Engineering
Module-V: Dynamic Programming Dr. A K Yadav Design and Analysis of Algorithm 5/74
School of Computer Science and Engineering
Memoization
▶ Memoization is mainly storing the value in the form of Memo
or in some tabular method
▶ Whenever we need some calculation of the sub-problem then
we first check the memo
▶ If solution of the sub-problems is already available in memo
then we use that solution and not solve the sub-problem again
▶ If the solution of the sub-problem is not in memo then we
solve the sub-problem and note the result in the form of the
memo for next call
Module-V: Dynamic Programming Dr. A K Yadav Design and Analysis of Algorithm 6/74
School of Computer Science and Engineering
Module-V: Dynamic Programming Dr. A K Yadav Design and Analysis of Algorithm 7/74
School of Computer Science and Engineering
Module-V: Dynamic Programming Dr. A K Yadav Design and Analysis of Algorithm 8/74
School of Computer Science and Engineering
Module-V: Dynamic Programming Dr. A K Yadav Design and Analysis of Algorithm 9/74
School of Computer Science and Engineering
Module-V: Dynamic Programming Dr. A K Yadav Design and Analysis of Algorithm 11/74
School of Computer Science and Engineering
// Returns total distinct ways to make sum using n coins
//of different denominations
int count(vector<int>& coins, int n, int sum)
{
// 2d dp array where n is the number of coin
// denominations and sum is the target sum
vector<vector<int>> dp(n+1, vector<int>(sum+1,0));
// Represents the base case where the target sum is 0
// and there is only one way to make change:
// by not selecting any coin
dp[0][0] = 1;
for (int i = 1; i <= n; i++)
{
for (int j = 0; j <= sum; j++)
{
// Add the number of ways to make change without
// using the current coin,
Module-V: Dynamic Programming Dr. A K Yadav Design and Analysis of Algorithm 11/74
School of Computer Science and Engineering
dp[i][j] += dp[i - 1][j];
if ((j - coins[i - 1]) >= 0)
{
// Add the number of ways to make change
// using the current coin
dp[i][j] += dp[i][j - coins[i - 1]];
}
}
}
return dp[n][sum];
}
// Driver Code
int main()
{
vector<int> coins{ 1, 2, 3 };
int n = 3;
int sum = 5;
Module-V: Dynamic Programming Dr. A K Yadav Design and Analysis of Algorithm 11/74
School of Computer Science and Engineering
cout << count(coins, n, sum);
return 0;
}
Module-V: Dynamic Programming Dr. A K Yadav Design and Analysis of Algorithm 12/74
School of Computer Science and Engineering
Module-V: Dynamic Programming Dr. A K Yadav Design and Analysis of Algorithm 12/74
School of Computer Science and Engineering
Module-V: Dynamic Programming Dr. A K Yadav Design and Analysis of Algorithm 13/74
School of Computer Science and Engineering
Module-V: Dynamic Programming Dr. A K Yadav Design and Analysis of Algorithm 14/74
School of Computer Science and Engineering
Module-V: Dynamic Programming Dr. A K Yadav Design and Analysis of Algorithm 15/74
School of Computer Science and Engineering
Module-V: Dynamic Programming Dr. A K Yadav Design and Analysis of Algorithm 16/74
School of Computer Science and Engineering
Bottom-up Approach
LCS-LENGTH(X, Y)
1. m = length(X )
2. n = length(Y )
3. Let c[0 . . . m, 0 . . . n] and b[1 . . . m, 1 . . . n] two new tables
4. for i = 1 to m
5. c[i, 0] = 0//Y is empty
6. for j = 1 to n
7. c[0, j] = 0//X is empty
8. for i = 1 to m
9. for j = 1 to n
10. if xi = yj
11. c[i, j] = c[i − 1, j − 1] + 1
Module-V: Dynamic Programming Dr. A K Yadav Design and Analysis of Algorithm 17/74
School of Computer Science and Engineering
12. b[i, j] =↖
13. else if c[i − 1, j] ≥ c[i, j − 1]
14. c[i, j] = c[i − 1, j]
15. b[i, j] =↑
16. else
17. c[i, j] = c[i, j − 1]
18. b[i, j] =→
19. return c, b
Length of the LCS is c[m,n]
Complexity of the algorithm is Θ(mn)
Module-V: Dynamic Programming Dr. A K Yadav Design and Analysis of Algorithm 18/74
School of Computer Science and Engineering
Top-down Approach:
MEMOIZED-LCS(X , Y , m, n)
1. Let c[0 . . . m, 0 . . . n]
2. for i = 0 to m
3. for j = 0 to n
4. c[i, j] = 0
5. return LCS(X , Y , c, b, m, n)
LCS(X , Y , c, b, i, j)
1. if c[i, j] > 0
2. return c[i, j]
3. if xi = yj
4. c[i, j] = LCS(X , Y , c, b, i − 1, j − 1] + 1
5. b[i, j] =↖
Module-V: Dynamic Programming Dr. A K Yadav Design and Analysis of Algorithm 19/74
School of Computer Science and Engineering
Module-V: Dynamic Programming Dr. A K Yadav Design and Analysis of Algorithm 20/74
School of Computer Science and Engineering
5. print xi
6. else if b[i, j] =↑
7. PRINT-LCS(b, X , i − 1, j)
8. else
9. PRINT-LCS(b, X , i, j − 1)
Complexity of the algorithm is Θ(m + n)
Module-V: Dynamic Programming Dr. A K Yadav Design and Analysis of Algorithm 22/74
School of Computer Science and Engineering
Module-V: Dynamic Programming Dr. A K Yadav Design and Analysis of Algorithm 23/74
School of Computer Science and Engineering
Module-V: Dynamic Programming Dr. A K Yadav Design and Analysis of Algorithm 24/74
School of Computer Science and Engineering
Module-V: Dynamic Programming Dr. A K Yadav Design and Analysis of Algorithm 26/74
School of Computer Science and Engineering
▶ But if it is part of the path then we can divide the whole path
p into two paths: p1 - from i to k and p2 - from k to j
▶ Intermediate nodes of p1 and p2 will be from {1, . . . , k − 1}
because now k is a source in one path and destination in
other but not the intermediate node.
(
δ(i, j) if k is not part of p
δ(i, j) =
δ(i, k) + δ(k, j) if k is part of p
Module-V: Dynamic Programming Dr. A K Yadav Design and Analysis of Algorithm 27/74
School of Computer Science and Engineering
Module-V: Dynamic Programming Dr. A K Yadav Design and Analysis of Algorithm 28/74
School of Computer Science and Engineering
NIL if i = j, k = 0
NIL
if wij = ∞, k = 0
πijk = i if wij < ∞, k = 0
πijk−1 dijk−1 ≤ dikk−1 + dkjk−1
if
k−1
dijk−1 > dikk−1 + dkjk−1
πkj if
Module-V: Dynamic Programming Dr. A K Yadav Design and Analysis of Algorithm 29/74
School of Computer Science and Engineering
Module-V: Dynamic Programming Dr. A K Yadav Design and Analysis of Algorithm 30/74
School of Computer Science and Engineering
9. dijk = dijk−1
10. πijk = πijk−1
11. else
12. dijk = dikk−1 + dkjk−1
k−1
13. πijk = πkj
14. return D n , Πn
Module-V: Dynamic Programming Dr. A K Yadav Design and Analysis of Algorithm 31/74
School of Computer Science and Engineering
Bottom-up Approach-II
FLOYD-WARSHALL(W,A,n)
1. D = W
2. Π = A
3. for k = 1 to n
4. for i = 1 to n
5. for j = 1 to n
6. if dij ≤ dik + dkj
7. dij = dij
8. πij = πij
9. else
10. dij = dik + dkj
11. πij = πkj
12. return D, Π
Module-V: Dynamic Programming Dr. A K Yadav Design and Analysis of Algorithm 32/74
School of Computer Science and Engineering
Module-V: Dynamic Programming Dr. A K Yadav Design and Analysis of Algorithm 33/74
School of Computer Science and Engineering
n
X
under the constraints xi wi ≤ W , and xi ∈ {0, 1}
i=1
▶ In bounded Knapsack Problem we can pick upto ci copies of
xi item:
n
X
Maximize vi xi
i=1
n
X
under the constraints xi wi ≤ W , and xi ∈ {0 . . . ci }
i=1
Module-V: Dynamic Programming Dr. A K Yadav Design and Analysis of Algorithm 34/74
School of Computer Science and Engineering
n
X
under the constraints xi wi ≤ W , and xi ≥ 0
i=1
Module-V: Dynamic Programming Dr. A K Yadav Design and Analysis of Algorithm 35/74
School of Computer Science and Engineering
Module-V: Dynamic Programming Dr. A K Yadav Design and Analysis of Algorithm 36/74
School of Computer Science and Engineering
Bottom-up Approach:
OPTIMAL-0-1KP(w,v,n,W)
1. Let m[0 . . . n, 0 . . . W ]
2. for i = 0 to n
3. for j = 0 to W
4. if i = 0 or j = 0 //no item or nil bag capacity
5. m[i, j] = 0
6. else if w [i] ≤ j// i th item can be accommodated in bag
7. if (m[i − 1, j − w [i]] + v [i]) > m[i − 1, j]
8. m[i, j] = m[i − 1, j − w [i]] + v [i]//pick the item
9. else
10. m[i, j] = m[i − 1, j]//don’t pick the item
11. return m
Module-V: Dynamic Programming Dr. A K Yadav Design and Analysis of Algorithm 37/74
School of Computer Science and Engineering
Top-down Approach:
Mamoized-0-1KP(w,v,n,W)
1. Let m[0 . . . n, 0 . . . W ]
2. for i = 0 to n
3. for j = 0 to W
4. m[i, j] = 0
5. return knapsack(m, w , v , n, W )
knapsack(m, w , v , i, j)
1. if m[i, j] > 0
2. return m[i, j]
3. if i = 0 or j = 0 //no item or nil bag capacity
4. return 0
5. if w [i] < j
Module-V: Dynamic Programming Dr. A K Yadav Design and Analysis of Algorithm 38/74
School of Computer Science and Engineering
Module-V: Dynamic Programming Dr. A K Yadav Design and Analysis of Algorithm 39/74
School of Computer Science and Engineering
Module-V: Dynamic Programming Dr. A K Yadav Design and Analysis of Algorithm 40/74
School of Computer Science and Engineering
Module-V: Dynamic Programming Dr. A K Yadav Design and Analysis of Algorithm 41/74
School of Computer Science and Engineering
Module-V: Dynamic Programming Dr. A K Yadav Design and Analysis of Algorithm 42/74
School of Computer Science and Engineering
Module-V: Dynamic Programming Dr. A K Yadav Design and Analysis of Algorithm 44/74
School of Computer Science and Engineering
Module-V: Dynamic Programming Dr. A K Yadav Design and Analysis of Algorithm 45/74
School of Computer Science and Engineering
Bottom-up Approach:
MATRIX-CHAIN-ORDER(p,n)
1. Let m[1 . . . n, 1 . . . n] and s[1 . . . n − 1, 2 . . . n]
2. for i = 1 to n
3. m[i, i] = 0
4. for l = 2 to n // l is the chain length
5. for i = 1 to n − l + 1
6. j =i +l −1
7. m[i, j] = ∞
8. for k = i to j − 1
9. q = m[i, k] + m[k + 1, j] + pi−1 × pk × pj
10. if q < m[i, j]
11. m[i, j] = q
12. s[i, j] = k
13. return m, s
Module-V: Dynamic Programming Dr. A K Yadav Design and Analysis of Algorithm 46/74
School of Computer Science and Engineering
Top-down Approach:
MEMOIZED-MATRIX-CHAIN(p,n)
1. Let m[1 . . . n, 1 . . . n]
2. for i = 1 to n
3. for j = i to n
4. m[i, i] = ∞
5. return LOOKUP-CHAIN(m,s,p,1,n)
LOOKUP-CHAIN(m,s,p,i,j)
1. if m[i, j] < ∞
2. return m[i, j]
3. if i = j
4. m[i, j] = 0
5. else for k = i to j − 1
Module-V: Dynamic Programming Dr. A K Yadav Design and Analysis of Algorithm 47/74
School of Computer Science and Engineering
6. q =LOOKUP-CHAIN(m,s,p,i,k)
+LOOKUP-CHAIN(m,s,p,k+1,j)+pi−1 pk pj
7. if q < m[i, j]
8. m[i, j] = q
9. s[i, j] = k
10. return m[i, j]
Module-V: Dynamic Programming Dr. A K Yadav Design and Analysis of Algorithm 48/74
School of Computer Science and Engineering
Module-V: Dynamic Programming Dr. A K Yadav Design and Analysis of Algorithm 51/74
School of Computer Science and Engineering
Module-V: Dynamic Programming Dr. A K Yadav Design and Analysis of Algorithm 52/74
School of Computer Science and Engineering
▶ if kr is the root of the tree then two subtree will be: one
subtree having keys from k1 to kr −1 and dummy keys d0 to
dr −1 and other subtree having keys from kr +1 to kn and
dummy keys dr to dn
▶ if we search any keys from ki to kj then dummy keys will be
di−1 to dj for 1 ≤ i ≤ j ≤ n
▶ If j = i − 1 then there is no key but only one dummy key di−1
▶ Let
j
X j
X
w [i, j] = pk + qk
k=i k=i−1
Module-V: Dynamic Programming Dr. A K Yadav Design and Analysis of Algorithm 53/74
School of Computer Science and Engineering
Module-V: Dynamic Programming Dr. A K Yadav Design and Analysis of Algorithm 54/74
School of Computer Science and Engineering
n
X n
X n
X n
X
E [1, n] = depth(kk ).pk + depth(dk ).qk + pk + qk
k=1 i=0 k=1 k=0
n
X n
X
E [1, n] = depth(kk ).pk + depth(dk ).qk + 1
k=1 i=0
Module-V: Dynamic Programming Dr. A K Yadav Design and Analysis of Algorithm 55/74
School of Computer Science and Engineering
j
X j
X j
X j
X
E [i, j] = depth(kk ).pk + depth(dk ).qk + pk + qk
k=i k=i−1 k=i k=i−1
j
X j
X
E [i, j] = depth(kk ).pk + depth(dk ).qk + w [i, j]
k=i k=i−1
Module-V: Dynamic Programming Dr. A K Yadav Design and Analysis of Algorithm 56/74
School of Computer Science and Engineering
Module-V: Dynamic Programming Dr. A K Yadav Design and Analysis of Algorithm 57/74
School of Computer Science and Engineering
▶ Now the total cost of the tree will be sum of cost of left
subtree having keys from ki to kr −1 with dummy keys from
di−1 to dr −1 plus cost of right subtree having keys from kr +1
to kj with dummy keys from dr to dj plus cost of key kr
▶ di is the dummy key between the keys ki and ki+1
▶ If we choose ki as the root then there is no key (keys from ki
to ki−1 ) on left subtree but only one dummy key di−1
▶ If we choose kj as the root then there is no key (keys from
kj+1 to kj ) on right subtree but only one dummy key dj
▶ By taking all key from ki to kj as root one by one, we can find
the optimal cost of the tree
Module-V: Dynamic Programming Dr. A K Yadav Design and Analysis of Algorithm 58/74
School of Computer Science and Engineering
Module-V: Dynamic Programming Dr. A K Yadav Design and Analysis of Algorithm 59/74
School of Computer Science and Engineering
e[r + 1, j] = e[r + 1, j] + w [r + 1, j]
Module-V: Dynamic Programming Dr. A K Yadav Design and Analysis of Algorithm 60/74
School of Computer Science and Engineering
Module-V: Dynamic Programming Dr. A K Yadav Design and Analysis of Algorithm 61/74
School of Computer Science and Engineering
Module-V: Dynamic Programming Dr. A K Yadav Design and Analysis of Algorithm 62/74
School of Computer Science and Engineering
Bottom-up Approach:
OPTIMAL-BST(p,q,n)
1. Let e[1 . . . n + 1, 0 . . . n], w [1 . . . n + 1, 0 . . . n] and
root[1 . . . n, 1 . . . n]
2. for i = 1 to n + 1 //when no keys, will be only one dummy key
3. w [i, i − 1] = qi−1
4. e[i, i − 1] = qi−1
5. for l = 1 to n // l number of keys in the subtree
6. for i = 1 to n − l + 1
7. j =i +l −1
8. e[i, j] = ∞
9. w [i, j] = w [i, j − 1] + pj + qj
10. for r = i to j
Module-V: Dynamic Programming Dr. A K Yadav Design and Analysis of Algorithm 63/74
School of Computer Science and Engineering
Module-V: Dynamic Programming Dr. A K Yadav Design and Analysis of Algorithm 64/74
School of Computer Science and Engineering
Top-down Approach:
MEMOIZED-OPTIMAL-BST(p,q,n)
1. Let e[1 . . . n + 1, 0 . . . n], w [1 . . . n + 1, 0 . . . n] and
root[1 . . . n, 1 . . . n]
2. for i = 1 to n + 1
3. for j = i − 1 to n
4. e[i, j] = ∞
5. if j = i − 1
6. w [i, j] = qi−1
7. else
8. w [i, j] = w [i, j − 1] + pj + qj
9. return LOOKUP-OBST(e,w,root,p,q,1,n)
Module-V: Dynamic Programming Dr. A K Yadav Design and Analysis of Algorithm 65/74
School of Computer Science and Engineering
LOOKUP-OBST(e,w,root,p,q,i,j)
1. if e[i, j] < ∞
2. return e[i, j]
3. if j = i − 1
4. e[i, j] = qi−1
5. else for r = i to j
6. q =LOOKUP-OBST(e,w,root,p,q,i,r-1)
+LOOKUP-OBST(e,w,root,p,q,r+1,j)+w [i, j]
7. if q < e[i, j]
8. e[i, j] = q
9. root[i, j] = r
10. return e[i, j]
Module-V: Dynamic Programming Dr. A K Yadav Design and Analysis of Algorithm 66/74
School of Computer Science and Engineering
PRINT-OBST(root,i,j,r,child)
1. if i ≤ j
2. c = root[i, j]
3. print kc is child of kr
4. PRINT-OBST(root, i, r − 1, c, ”left”)
5. PRINT-OBST(root, r + 1, j, c, ”right”)
Complexity of the OBST is O(n3 )
Module-V: Dynamic Programming Dr. A K Yadav Design and Analysis of Algorithm 68/74
School of Computer Science and Engineering
Module-V: Dynamic Programming Dr. A K Yadav Design and Analysis of Algorithm 69/74
School of Computer Science and Engineering
Binomial Coefficient
▶ (x + a)n can be expended as
n C x n a 0 +n C x n−1 a 1 + · · · +n C x n−k a k + . . .n C x 0 a n
0 1 k n
▶ Coefficient of the term x n−k ak is know as Binomial
Coefficient C (n, k) =n Ck
n!
▶ C (n, k) = n−k!k!
▶ C (n, k) = C (n − 1, k − 1) + C (n − 1, k) if n > k > 0
n−1!
▶ ⇒ n−k!k−1! n−1!
+ n−k−1!k!
n−1! k
▶ ⇒ n−k!k−1! n−1! n−k
k + n−k−1!k! n−k
(n−1)!k (n−1)!(n−k)
▶ ⇒ n−k!k! + n−k!k!
(n−1)!(k+n−k)
▶ ⇒ n−k!k!
▶ ⇒ n!
n−k!k!
▶ ⇒ C (n, k)
▶ C (n, 0) = C (n, n) = 1
Module-V: Dynamic Programming Dr. A K Yadav Design and Analysis of Algorithm 70/74
School of Computer Science and Engineering
Module-V: Dynamic Programming Dr. A K Yadav Design and Analysis of Algorithm 71/74
School of Computer Science and Engineering
Top-down Approach:
Mamoized-BC(c,n,k)
1. Let c[0 . . . n, 0 . . . k]
2. for i = 0 to n
3. for j = 0 to i
4. c[i, j] = 0
5. return BC (c, n, k)
BC (c, i, j)
1. if c[i, j] > 0
2. return c[i, j]
3. if i = j or j = 0
4. return 1
5. return c[i, j] = BC (c, i − 1, j − 1) + BC (c, i − 1, j]
Complexity of the Knapsack is O(nk)
Module-V: Dynamic Programming Dr. A K Yadav Design and Analysis of Algorithm 72/74
School of Computer Science and Engineering
Module-V: Dynamic Programming Dr. A K Yadav Design and Analysis of Algorithm 73/74
School of Computer Science and Engineering
Thank you
Please send your feedback or any queries to
[email protected]
Module-V: Dynamic Programming Dr. A K Yadav Design and Analysis of Algorithm 74/74