Dynamic Programming in Algorithms
Dynamic Programming in Algorithms
October 1, 2025
Module-IV: Dynamic Programming Dr. A K Yadav Associate Professor Design and Analysis of Algorithms 1/74
Department of Computer Science and Engineering
Contents
Dynamic programming 3
Optimal binary search trees 9
0-1 Knapsack Problem 29
Traveling Salesman Problem 36
Coin Change Problem 37
Longest Common Subsequence 42
Multi stage graph 54
Floyd Warshall algorithm 55
Matrix Chain Multiplication(MCM) 63
Binomial Coefficient 73
Module-IV: Dynamic Programming Dr. A K Yadav Associate Professor Design and Analysis of Algorithms 2/74
Department of Computer Science and Engineering
Dynamic programming
I Solves problems by combining the solutions to sub-problems
I Sub-problems are overlapping
I Doesn’t solve overlapping sub-problems again and again
I Behave like Divide and Conquer if sub-problems are not
overlapping
I Used in optimization problems
I Can be used either top-down with memoization or bottom-up
method
Module-IV: Dynamic Programming Dr. A K Yadav Associate Professor Design and Analysis of Algorithms 3/74
Department of Computer Science and Engineering
Module-IV: Dynamic Programming Dr. A K Yadav Associate Professor Design and Analysis of Algorithms 4/74
Department of Computer Science and Engineering
Module-IV: Dynamic Programming Dr. A K Yadav Associate Professor Design and Analysis of Algorithms 5/74
Department of Computer Science and Engineering
Memoization
I Memoization is mainly storing the value in the form of Memo
or in some tabular method
I Whenever we need some calculation of the sub-problem then
we first check the memo
I If solution of the sub-problems is already available in memo
then we use that solution and not solve the sub-problem again
I 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-IV: Dynamic Programming Dr. A K Yadav Associate Professor Design and Analysis of Algorithms 6/74
Department of Computer Science and Engineering
Module-IV: Dynamic Programming Dr. A K Yadav Associate Professor Design and Analysis of Algorithms 7/74
Department of Computer Science and Engineering
Module-IV: Dynamic Programming Dr. A K Yadav Associate Professor Design and Analysis of Algorithms 8/74
Department of Computer Science and Engineering
Module-IV: Dynamic Programming Dr. A K Yadav Associate Professor Design and Analysis of Algorithms 10/74
Department of Computer Science and Engineering
Module-IV: Dynamic Programming Dr. A K Yadav Associate Professor Design and Analysis of Algorithms 11/74
Department of Computer Science and Engineering
I 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
I if we search any keys from ki to kj then dummy keys will be
di−1 to dj for 1 ≤ i ≤ j ≤ n
I If j = i − 1 then there is no key but only one dummy key di−1
I Let
j
X j
X
w [i, j] = pk + qk
k=i k=i−1
Module-IV: Dynamic Programming Dr. A K Yadav Associate Professor Design and Analysis of Algorithms 12/74
Department of Computer Science and Engineering
Module-IV: Dynamic Programming Dr. A K Yadav Associate Professor Design and Analysis of Algorithms 13/74
Department 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-IV: Dynamic Programming Dr. A K Yadav Associate Professor Design and Analysis of Algorithms 14/74
Department 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-IV: Dynamic Programming Dr. A K Yadav Associate Professor Design and Analysis of Algorithms 15/74
Department of Computer Science and Engineering
Module-IV: Dynamic Programming Dr. A K Yadav Associate Professor Design and Analysis of Algorithms 16/74
Department of Computer Science and Engineering
I 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
I di is the dummy key between the keys ki and ki+1
I 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
I 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
I By taking all key from ki to kj as root one by one, we can find
the optimal cost of the tree
Module-IV: Dynamic Programming Dr. A K Yadav Associate Professor Design and Analysis of Algorithms 17/74
Department of Computer Science and Engineering
Module-IV: Dynamic Programming Dr. A K Yadav Associate Professor Design and Analysis of Algorithms 18/74
Department of Computer Science and Engineering
e[r + 1, j] = e[r + 1, j] + w [r + 1, j]
Module-IV: Dynamic Programming Dr. A K Yadav Associate Professor Design and Analysis of Algorithms 19/74
Department of Computer Science and Engineering
Module-IV: Dynamic Programming Dr. A K Yadav Associate Professor Design and Analysis of Algorithms 20/74
Department of Computer Science and Engineering
Module-IV: Dynamic Programming Dr. A K Yadav Associate Professor Design and Analysis of Algorithms 21/74
Department 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-IV: Dynamic Programming Dr. A K Yadav Associate Professor Design and Analysis of Algorithms 22/74
Department of Computer Science and Engineering
Module-IV: Dynamic Programming Dr. A K Yadav Associate Professor Design and Analysis of Algorithms 23/74
Department 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-IV: Dynamic Programming Dr. A K Yadav Associate Professor Design and Analysis of Algorithms 24/74
Department 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-IV: Dynamic Programming Dr. A K Yadav Associate Professor Design and Analysis of Algorithms 25/74
Department 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-IV: Dynamic Programming Dr. A K Yadav Associate Professor Design and Analysis of Algorithms 27/74
Department of Computer Science and Engineering
Module-IV: Dynamic Programming Dr. A K Yadav Associate Professor Design and Analysis of Algorithms 28/74
Department of Computer Science and Engineering
Module-IV: Dynamic Programming Dr. A K Yadav Associate Professor Design and Analysis of Algorithms 29/74
Department of Computer Science and Engineering
n
X
under the constraints xi wi ≤ W , and xi ∈ {0, 1}
i=1
I 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-IV: Dynamic Programming Dr. A K Yadav Associate Professor Design and Analysis of Algorithms 30/74
Department of Computer Science and Engineering
n
X
under the constraints xi wi ≤ W , and xi ≥ 0
i=1
Module-IV: Dynamic Programming Dr. A K Yadav Associate Professor Design and Analysis of Algorithms 31/74
Department of Computer Science and Engineering
Module-IV: Dynamic Programming Dr. A K Yadav Associate Professor Design and Analysis of Algorithms 32/74
Department 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-IV: Dynamic Programming Dr. A K Yadav Associate Professor Design and Analysis of Algorithms 33/74
Department 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-IV: Dynamic Programming Dr. A K Yadav Associate Professor Design and Analysis of Algorithms 34/74
Department of Computer Science and Engineering
Module-IV: Dynamic Programming Dr. A K Yadav Associate Professor Design and Analysis of Algorithms 35/74
Department of Computer Science and Engineering
Module-IV: Dynamic Programming Dr. A K Yadav Associate Professor Design and Analysis of Algorithms 36/74
Department of Computer Science and Engineering
Module-IV: Dynamic Programming Dr. A K Yadav Associate Professor Design and Analysis of Algorithms 37/74
Department of Computer Science and Engineering
Module-IV: Dynamic Programming Dr. A K Yadav Associate Professor Design and Analysis of Algorithms 39/74
Department 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-IV: Dynamic Programming Dr. A K Yadav Associate Professor Design and Analysis of Algorithms 39/74
Department 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-IV: Dynamic Programming Dr. A K Yadav Associate Professor Design and Analysis of Algorithms 39/74
Department of Computer Science and Engineering
cout << count(coins, n, sum);
return 0;
}
Module-IV: Dynamic Programming Dr. A K Yadav Associate Professor Design and Analysis of Algorithms 40/74
Department of Computer Science and Engineering
Module-IV: Dynamic Programming Dr. A K Yadav Associate Professor Design and Analysis of Algorithms 40/74
Department of Computer Science and Engineering
Module-IV: Dynamic Programming Dr. A K Yadav Associate Professor Design and Analysis of Algorithms 41/74
Department of Computer Science and Engineering
Module-IV: Dynamic Programming Dr. A K Yadav Associate Professor Design and Analysis of Algorithms 42/74
Department of Computer Science and Engineering
Module-IV: Dynamic Programming Dr. A K Yadav Associate Professor Design and Analysis of Algorithms 43/74
Department of Computer Science and Engineering
Module-IV: Dynamic Programming Dr. A K Yadav Associate Professor Design and Analysis of Algorithms 44/74
Department 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-IV: Dynamic Programming Dr. A K Yadav Associate Professor Design and Analysis of Algorithms 45/74
Department 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-IV: Dynamic Programming Dr. A K Yadav Associate Professor Design and Analysis of Algorithms 46/74
Department 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-IV: Dynamic Programming Dr. A K Yadav Associate Professor Design and Analysis of Algorithms 47/74
Department of Computer Science and Engineering
Module-IV: Dynamic Programming Dr. A K Yadav Associate Professor Design and Analysis of Algorithms 48/74
Department 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-IV: Dynamic Programming Dr. A K Yadav Associate Professor Design and Analysis of Algorithms 50/74
Department of Computer Science and Engineering
Module-IV: Dynamic Programming Dr. A K Yadav Associate Professor Design and Analysis of Algorithms 51/74
Department of Computer Science and Engineering
Module-IV: Dynamic Programming Dr. A K Yadav Associate Professor Design and Analysis of Algorithms 53/74
Department of Computer Science and Engineering
I 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
I 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-IV: Dynamic Programming Dr. A K Yadav Associate Professor Design and Analysis of Algorithms 54/74
Department of Computer Science and Engineering
Module-IV: Dynamic Programming Dr. A K Yadav Associate Professor Design and Analysis of Algorithms 55/74
Department 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-IV: Dynamic Programming Dr. A K Yadav Associate Professor Design and Analysis of Algorithms 56/74
Department of Computer Science and Engineering
Module-IV: Dynamic Programming Dr. A K Yadav Associate Professor Design and Analysis of Algorithms 57/74
Department 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-IV: Dynamic Programming Dr. A K Yadav Associate Professor Design and Analysis of Algorithms 58/74
Department 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-IV: Dynamic Programming Dr. A K Yadav Associate Professor Design and Analysis of Algorithms 59/74
Department of Computer Science and Engineering
Module-IV: Dynamic Programming Dr. A K Yadav Associate Professor Design and Analysis of Algorithms 60/74
Department of Computer Science and Engineering
Module-IV: Dynamic Programming Dr. A K Yadav Associate Professor Design and Analysis of Algorithms 61/74
Department of Computer Science and Engineering
Module-IV: Dynamic Programming Dr. A K Yadav Associate Professor Design and Analysis of Algorithms 62/74
Department of Computer Science and Engineering
Module-IV: Dynamic Programming Dr. A K Yadav Associate Professor Design and Analysis of Algorithms 64/74
Department of Computer Science and Engineering
Module-IV: Dynamic Programming Dr. A K Yadav Associate Professor Design and Analysis of Algorithms 65/74
Department 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-IV: Dynamic Programming Dr. A K Yadav Associate Professor Design and Analysis of Algorithms 66/74
Department 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-IV: Dynamic Programming Dr. A K Yadav Associate Professor Design and Analysis of Algorithms 67/74
Department 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-IV: Dynamic Programming Dr. A K Yadav Associate Professor Design and Analysis of Algorithms 68/74
Department of Computer Science and Engineering
Binomial Coefficient
I (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
I Coefficient of the term x n−k ak is know as Binomial
Coefficient C (n, k) =n Ck
n!
I C (n, k) = n−k!k!
I C (n, k) = C (n − 1, k − 1) + C (n − 1, k) if n > k > 0
n−1!
I ⇒ n−k!k−1! n−1!
+ n−k−1!k!
n−1! k
I ⇒ n−k!k−1! n−1! n−k
k + n−k−1!k! n−k
(n−1)!k (n−1)!(n−k)
I ⇒ n−k!k! + n−k!k!
(n−1)!(k+n−k)
I ⇒ n−k!k!
I ⇒ n!
n−k!k!
I ⇒ C (n, k)
I C (n, 0) = C (n, n) = 1
Module-IV: Dynamic Programming Dr. A K Yadav Associate Professor Design and Analysis of Algorithms 70/74
Department of Computer Science and Engineering
Module-IV: Dynamic Programming Dr. A K Yadav Associate Professor Design and Analysis of Algorithms 71/74
Department 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-IV: Dynamic Programming Dr. A K Yadav Associate Professor Design and Analysis of Algorithms 72/74
Department of Computer Science and Engineering
Module-IV: Dynamic Programming Dr. A K Yadav Associate Professor Design and Analysis of Algorithms 73/74
Department of Computer Science and Engineering
Thank you
Please send your feedback or any queries to [Link]@[Link]
Module-IV: Dynamic Programming Dr. A K Yadav Associate Professor Design and Analysis of Algorithms 74/74