Dynamic Programming
Dynamic Programming
Programming
Dynamic Programming
• Dynamic programming (DP) is an algorithm design technique (like divide and
conquer) used to solve a wide variety of optimization problems such as
scheduling, travelling salesman, packaging, and inventory management, etc.
• DP solves each sub-problems just once and stores the result in a table so that it
can be repeatedly retrieved if needed again.
Time complexity =?
4
Fibonacci sequence using Recursion
F0 = 0 FIB (n)
F1 = 1 1.If (n < 2)
Fn = F(n-1)+ F(n-2) 2.then return n
3.else return FIB (n - 1) + FIB (n - 2)
It has been observed from the Fibonacci example that larger problem can
only be solved if we have solution of smaller subproblems. It means, optimal
substructure has been found.
Besides, there are many overlapping subproblems. So, We can use DP. In
DP, solution of subproblems are stored in table/array.
Two ways the solution of sub-problem can be stored so that it can be reused-
• Memoization Method
• Tabulation Method
MEMOFIB (n)
if (n < 2)
return n
if (A[n] !=-1)
return A[n]
else
return A[n] = MEMOFIB (n - 1) + MEMOFIB (n - 2)
8
Fibonacci sequence : Memoization
Memoization method
This algorithm clearly takes only O(n) time to compute Fib (n).
9
Fibonacci sequence : Tabulation Method
• This approach defines a table/array of size n+1 and fills the table/array
from its starting index i.e., 0th index to the last index. It means the base
cases are first filled and then the next higher indexes are filled.
F0 = 0, if n=0
F1=1 if n=1
Fn=F(n-1)+ F(n-2), if n>=1
There exist only n+1 unique subproblems for the Fibonacci series. So, the
number of subproblems called will be O(n+1). Thus, this algorithm takes only
O(n) time to compute Fib (n).
11
Memoization or Tabulation : Which one is better
Tabulation is often faster than memoization because it is iterative
and solving subproblems requires no overhead.
Source Destination
Source Destination
Recursive step
4
2 5
11 18
1 9
13
2 5
3 6 8
1
16
5 2
2
4 7
Recursion tree
Cost (5->8)=18 13 18 13 2 2
• The depth of the tree is equal to the number of stages, i.e., k. So, space complexity will
O(K), but at each level there may be n vertices and total there could be kn vertices in
worst case. So, time complexity will be exponential.
• If we explore the recursion tree then we could notice, there are many overlapping
subproblems. So, we can use DP to save the results in a table so that overlapping
subproblems cannot be computed in each call and it reduces the time complexity.
• In DP, only distinct subproblems are computed and their result are stored in a table. For
this problems, number of distinct sub-problems are equal to number of vertices. So, we
will define an array whose size will be equal to number of vertices to store cost from a
source vertex to the target.
DAA (CS-204) PDPM IIITDM, Jabalpur 19
Multistage Graph -Shortest Path
4
2 5
11 9 18
1
5 13
2 3 6 8
1
16
5 2
2
4 7
Recursion tree Overlapping
sub-problems
18 13 18 13 2 2
5 13 0
2 3 6 8
1 1 2 3 4 5 6 7 8
16
5 2 Compute T[7]
2
4 7
T[7] = min{( Edge cost (7, 8) + T[8])}
T[7] = min{(2+0)} =2
2 0
1 2 3 4 5 6 7 8
18 4 18 13 2 0
1 2 3 4 5 6 7 8
Compute T[1]
T[1] = min{( Edge cost (1, 2) + T[2]), (Edge cost (1, 3) + T[3]), (Edge cost (1, 4) + T[4])}
T[1] = min{(1+22), (2+18), (5+4)} =9
9 22 18 4 18 13 2 0
So, minimum cost to reach 8 from 1 will be
9. 1 2 3 4 5 6 7 8
DAA (CS-204) PDPM IIITDM, Jabalpur 24
Multistage Graph -Shortest Path-bottom up Approach
Time complexity:
Total time/ work done = Number of subproblems * time to solve each
subproblems
Number of subproblems solved to get the shortest path = n
Overall work done = number of times functions will be evaluated at each
stage
9 22 18 4 18 13 2 0
1 2 3 4 5 6 7 8
O(7) O(6) O(5) O(4) O(3) O(2) O(1) O(1)
2 4 5
1 11 9 18 Overall work done=
13 1+1+2+3+…+n-1
2 3 5 6 8
1 n-1 (n-2)/2 = n2
16
5
O(n2) = O(V2) = O(E)
2 2
4 7
0/1 Knapsack Problem
Given a set of items, each having different weight and value/ profit associated with it.
Find the set of items such that the total weight is less than or equal to a capacity of
the knapsack and the total value/ profit earned is as large as possible.
In above equation,
• wt [i] is the capacity of the object i and,
• j is the remaining/ available capacity.
There will be two possibility for each item, whether it is included in knapsack or not.
• If an object i is chosen, then profit P[i] is added and for the remaining i-1 elements,
max profit need to be identified. So, there will be a recursive call for remaining i-1
elements with remaining knapsack capacity i.e., j- wt [i].
• If i is not chosen, then profit will not increase and object will not be included in
knapsack, so there will be a recursive call for remaining i-1 elements. The choice
that gives the max profit will be selected.
• So, to solve this problem using DP will need to create a table/matrix of size
n+1 x w+1, 1 extra row and column will be created to store the base case
information (0 value). After that, table is filled one by one using the following
equation.
4, 3, 2, 1, 0
No. of items
0
0
0
Given- 0
Knapsack capacity (W) = 7 kg
Number of items (n) = 4
Step-02:
Start filling the table row wise top to bottom from left to right using the formula-
4, 3, 2, 1, 0
No. of items
0 1 1 1 1 1 1 1
0 1 1
0
0
Knapsack capacity (W) = 7 kg
Similarly, all columns of row 1 will
Step-02: be 1, since we are placing only one
item of weight 1 in the bag of
Start filling the table row wise top to bottom from left to varying capacity (j= 1, 2, … 7).
right using the formula-
Finding T[2,1] and T[2,2]
weight of item wt[i] = 3
the capacity of bag is j=1 i.e.,
wt[2]>j,
T[2,1]= T[i-1, j] = T[1, 1] =1
T[2,2]= T[1, 2] =1
4, 3, 2, 1, 0
No. of items
0 1 1 1 1 1 1 1
0 1 1 4 5
0
0
Knapsack capacity (W) = 7 kg
Finding T[2,3]
Step-02: weight of item wt[i] = 3
the capacity of bag is j=3.
Start filling the table row wise top to bottom from left to T[2,3]= max(T[1, 3-3]+ 4, T[1, 3])
right using the formula- =4
Finding T[2,4]
weight of item wt[i] = 3
the capacity of bag is j=4.
T[2,4]= max(T[1, 4-3]+ 4, T[1, 4])
=5 [Item 1 and 2 both can be
kept in bag]
4, 3, 2, 1, 0
No. of items
0 1 1 1 1 1 1 1
0 1 1 4 5 5 5 5
0 1 1 4
0
Knapsack capacity (W) = 7 kg
All remaining values of this row will be
Step-02: 5 since we have two items, and both
can be placed in bag.
Start filling the table row wise top to bottom from left to Finding T[3,1]
right using the formula- weight of item wt[i] = 4,
the capacity of bag is j=1, i.e., wt[i] >j
T[3,1]= T[2, 1]
=1 [only Item 1]
All places before T[3,3] will be 1 since
weight of item is larger than capacity.
Finding T[3,3)
T[3,3]= T[2, 3]
=4 [only Item 2]
DAA (CS-204) PDPM IIITDM, Jabalpur 36
0/1 Knapsack Problem
Weights
w0 w1 w2 w3 w4 w5 w6 w7
0 0 0 0 0 0 0 0
4, 3, 2, 1, 0
No. of items
0 1 1 1 1 1 1 1
0 1 1 4 5 5 5 5
0 1 1 4 5 6
0
Knapsack capacity (W) = 7 kg
Finding T[3,4]
Step-02: weight of item wt[i] = 4
the capacity of bag is j=4.
Start filling the table row wise top to bottom from left to T[3,4]= max(T[2, 4-4]+ 5, T[2, 4])
right using the formula- =5 [3rd object can be placed in bag]
Finding T[3,5]
weight of item wt[i] = 4
capacity of bag is j=4.
T[3,5]= max(T[2, 5-4]+ 5, T[2, 5])
=6
[3rd and 1st object can be placed in bag]
4, 3, 2, 1, 0
No. of items
0 1 1 1 1 1 1 1
0 1 1 4 5 5 5 5
0 1 1 4 5 6 6 9
0
Knapsack capacity (W) = 7 kg
Finding T[3,6]
Step-02: weight of item wt[i] = 4
capacity of bag is j=6
Start filling the table row wise top to bottom from left to T[3,6]= max(T[2, 6-4]+ 5, T[2, 6])
right using the formula- =6
Finding T[3,7]
weight of item wt[i] = 4
capacity of bag is j=7
T[3,7]= max(T[2, 7-4]+ 5, T[2, 7])
=9
[3rd and 2nd object can be placed in
bag]
DAA (CS-204) PDPM IIITDM, Jabalpur 38
0/1 Knapsack Problem
Weights
w0 w1 w2 w3 w4 w5 w6 w7
0 0 0 0 0 0 0 0
4, 3, 2, 1, 0
No. of items
0 1 1 1 1 1 1 1
0 1 1 4 5 5 5 5
0 1 1 4 5 6 6 9
0 1 1 4 5 7 8
Knapsack capacity (W) = 7 kg
Finding T[4,5]
Step-02: weight of item wt[i] = 5
capacity of bag is j=5
Start filling the table row wise top to bottom from left to T[4,5]= max(T[3, 5-5]+ 7, T[3, 5]
right using the formula- =7
Finding T[4,6]
weight of item wt[i] = 5
capacity of bag is j=6
T[4,6]= max(T[3, 6-5]+ 7, T[3, 6]
=8
[1st and 4th object can be placed in
bag]
DAA (CS-204) PDPM IIITDM, Jabalpur 39
0/1 Knapsack Problem
Weights
w0 w1 w2 w3 w4 w5 w6 w7
0 0 0 0 0 0 0 0
4, 3, 2, 1, 0
No. of items
0 1 1 1 1 1 1 1
0 1 1 4 5 5 5 5
0 1 1 4 5 6 6 9
0 1 1 4 5 7 8 9
Knapsack capacity (W) = 7 kg
Finding T[4,7]
Step-02: weight of item wt[i] = 5
capacity of bag is j=7
Start filling the table row wise top to bottom from left to T[4,7]= max(T[3, 7-5]+ 7, T[3, 7]
right using the formula- =9
[3rd and 2nd object can be placed in
bag]
0 1 1 1 1 1 1 1
0 1 1 4 5 5 5 5
0 1 1 4 5 6 6 9
0 1 1 4 5 7 8 9
Knapsack capacity (W) = 7 kg
Find the object that can be placed in the bag to maximize the profit-
• Go to last row last column i.e., T[4][7] = 9 and check if the previous row with same
column i.e., T[3][7]. If the values of T[3][7] = T[4][7], ignore 4th row and include 3rd
row’s weight and profit in the result set.
• Subtract its profit from total (9), 9-5=4. check for 4 in 2nd row. if it is present include
object’s weight and profit in result set else go to its previous row.
• As 4 is in 2nd row include it in result set and subtract its profit from the remaining
profit. Profit =4-4=0
Since, profit is now 0 means we reached to the final solution.
The bag/ result set will include item 2 and 3 and profit will 9.
Time Complexity
Time complexity for 0/1 Knapsack problem solved using DP is
O(N*W) where N denotes number of items available, and W
denotes the capacity of the knapsack
Longest Common Subsequence (LCS)
Given two strings, the task is to find the longest common subsequence (LCS)
present in the given strings in the same order.
A subsequence of a string is a new string generated from the original string
with some characters (can be none) deleted without changing the relative
order of the remaining characters. Please note, in substring, characters are
selected in contiguous manner, while in subsequence characters may not be
contiguous.
For example, "ace" is a subsequence of "abcde". LCS :- cdgi
Suppose we have string s1= “a b c d e f g h i j” and
s2= “e c d g i”
The substring “ecdgi” is also present in s1 but its characters are not in the increasing
order. Please note that matching character need not to be contiguous, but they must
in the increasing order by their index.
s1= “a b c d e f g h i j”
c is before e in string s1 so, it will not
s2= “e c d g i” be considered as a subsequence.
Longest Common Subsequence (LCS)
string s1= “a b c d e f g h i j” (size m) → For generating a subsequence, a character
may be either taken or not taken. So, for
s1= “a b c d e f g h i j” (size m) each character, there will be 2 choices. So,
s2= “e c d g i” (size n) total no. of subsequence could be = 2m
0 if i=0 or j=0
C(i, j) = 1+C(i-1, j-1) if i>0, j>0, and Xi=Yj
max[C(i, j-1), C(i-1, j)] if i>0, j>0, and Xi!=Yj
Longest Common Subsequence (LCS)
Suppose we have two string X= {A, A, A, A} and Y= {B, B, B,B}. Then following tree can be
constructed for the following recursive formula.
X= {A, A, A, A}
No match
Y= {B, B, B,B}
C(4, 4)
C(4, 3) C(3, 4)
C(1, 3) C(2, 2)
C(0, 3) C(1, 2)
Longest Common Subsequence (LCS)
The maximum depth of the tree could be m+n, since longest path may include the
worst case, that decrease n by 1 in one step and m by 1 in another step as given in the
following figure.
X= {A, A, A, A}
C(n, m)
No match
C(n-1, m) Y= {B, B, B,B}
C(n-2, m-1)
C(n-2, m-2)
.
.
.
C(0, --) or C(--, 0)
Since, the depth of the root is m+n so, total number of function will be O(2m+n/2), if
tree will be half filled
Longest Common Subsequence (LCS)
Since, the depth of the root is m+n so, total number of function will be O(2m+n/2), if tree
will be half filled. So, can we reduce time complexity using DP? DP will only applicable,
if we can find optimal substructure and overlapping subproblems.
There are many subproblems in the following tree which are overlapping. In DP, only
unique subproblems are evaluated. Let us find unique subproblems for the LCS
problems.
The unique subproblems could be nearly O(mxn), if string X contains m characters and
string Y contains n characters.
Overlapping
subproblems
Longest Common Subsequence (LCS)
There are many subproblems in the following tree which are overlapping. In DP, only unique
subproblems are evaluated. Let us find unique subproblems for the LCS problems.
C(3, 3)
C(3, 2)
C(2, 1) C(3, 0)
C(1, 3) C(2, 2)
C(1, 2) C(2, 1)
C(0, 3) C(1, 2)
Non-overlapping subproblems are- C[3,3], C[3,2], C[3,1], C[2,3], C[2,2], C[2,1], C[1,3],
C[1,2], C[1,1], C[0,3], C[0,2], C[0,1], [3,0], C[2,0], C[1,0], C[0,0] (9+7=16 sub problems
including 7 subproblems with 0 base cases).
To solve this problem using DP will require to create a table of size (m+1xn +1) [1 row and
column to store 0 (base case) ].
Longest Common Subsequence (LCS)
To find the LCS for X= “AAB” and Y =“ACA”, first a table of size (X+1) and (Y+1)
is created.
The value at any place will depends on its left diagonal, its previous column, and
previous row with same column.
For e.g.
C[1,1] = 1+c[0,0], or max (C[1,0], C[0,1])
So, the value of C[3,3] can only be computed, if we have values of C[2,2] (in case of
match) or C[2,1] and C[1,2].
The table should be filled either row wise or column wise to efficiently compute values.
0 1 2 3
0 00 01 02 03
1 10 11 12 13
2 20 21 22 23
3 30 31 32 33
Longest Common Subsequence (LCS)
To find the LCS for X= “AAB” and Y =“ACA”, first a table of size (X+1) and (Y+1)
are created.
Step 1: Fill 0 in 0th row and column since, if any of the string will null the LCS will also
be NULL.
Step 2: Compute C[1,1] using the formula
C[1,1]= 1+C[0,0], since Xi = Yi
= 1+0 =1
C[1,2] = max(C[1,1], C[0,2]), since Xi != Yi
= max(1,0) =1
0 A C A
C[1,3] = 1+C[0, 2], since Xi = Yi
0 1 2 3
= 1+0 =1
0 0 0 0 0 0
C[2,1] = 1+C[1, 0], since Xi = Yi
= 1+0 =1 0 1 1 1
A 1
C[2,2] = max(C[2,1], C[1,2]), since Xi != Yi
= max(1,1) =1 A 2 0 1 1 2
B 3 0 1 1 2
Longest Common Subsequence (LCS)
To find the LCS for X= “AAB” and Y =“ACA”, first a table of sizeof (X+1) and
(Y+1) are created.
To find the LCS, check the places where values of the cells an have been
updated, which means taken from diagonal and incremented. In this example,
values at C[1,1] and C[2,3] have been incremented. The LCS will include
characters at X[1] and X[2].
0 A C A
LCS = AA.
0 1 2 3
0 0 0 0 0 0
A 1 0 1 1 1
A 2 0 1 1 2
B 3 0 1 1 2
Longest Common Subsequence (LCS)
Find the LCS for X= “ABCBDAB” and Y =“BDCABA” in polynomial time.
0 A B C B D A B
0
B
D
C
A
Subset Sum Problem
Given an array of non-negative integers and an integer sum. We have
to tell whether there exists any subset in an array whose sum is equal
to the given integer sum.
Example:
Input: arr [] = {3, 34, 4, 12, 3, 2}, sum = 7
Output: True
Explanation: There is a subset (4, 3) with sum 7.
Subset Sum Problem
Given an array of non-negative integers and an integer sum. We have to tell
whether there exists any subset in an array whose sum is equal to the given
integer sum.
Example:
Input: arr[] = {3, 34, 4, 12, 3, 2}, sum = 7
Output: True
Explanation: There is a subset (4, 3) with sum 7.
Let us try brute force approach to solve the problem
In brute force all possible subsets are identified. In a subset, element will
either present or not present. So, there will be 2 possibilities for each
element. For n elements, the number of possible subsets could be 2n. So,
this approach will be computationally very expensive.
SS(i-1, S) S<ai
SS(i-1, S-ai) OR SS(i-1, S) S>=ai
SS(i, S) = True S=0
False i=0, S!=0
SS (5, 5)
SS (4, 4) SS (4, 5)
SS (5, 5)
SS (4, 4) SS (4, 5)
1. M1×(M2×M3)
Steps required in M2×M3 will be 10×8×5 = 400.
Dimensions of M23 will be 10×5.
Steps required in M1×M23 will be 5×10×5 = 250.
Total Steps =400+250 = 650
2. (M1×M2)×M3
Steps required in M1×M2 will be 5×10×8 = 400.
Dimensions of M12 will be 5×8.
Steps required in M12×M3 will be 5×8×5 = 200.
Total Steps = 400+200 = 600
Matrix Chain Multiplication
Matrix Chain Multiplication
A1 x A2 X A3
d0 d1 d1 d2 d2 d3
2 3 3 4 4 2
For this example, 2nd parenthesizing will be better. So, to minimize the number of
multiplication, their sequence must be identified in advance. For larger number of
matrices, a greater number of possibilities exists. So, trying all will increase time.
Matrix Chain Multiplication
In matrix chain multiplication, the number of ways matrices can be multiplied
depends on their size. If there will 3 matrices, the number of way they will be
multiplied is 2 while for 4 matrices, they can be multiplied in 5 ways. For 5 matrices,
they can be multiplied in
d0 d2 d3
d0 d1 d3
k i j k+1 k i j k+1
0 i=j
C[i,j] = min[(C[i,k] +C[k+1,j] +di-1xdkxdj)]
for i<=k<j, since splitting can
be anywhere b/w i and j
Matrix Chain Multiplication
Le us draw a recursion tree for four matrices-
A1A2A3A4. The cost can be represented by
C[1,4].
C[1,4]
Split point A1A2A3A4 Split point A1A2A3A4 Split point A1A2A3A4
C[2,2] C[3,4] C[2,3] C[4,4] C[1,1] C[2,2] C[1,1] C[2,3] C[1,2] C[3,3]
From the recursion tree, the depth in worst case will be O(n). So, if the tree will half fill, then
too it will have exponential function call O(2n) (as per the property of the complete binary tree
and this tree is bigger than a binary tree). As, the recursion tree has many overlapping
subproblems. The time complexity can be reduced using DP. DP uses bottom-up
computation and stores unique results in a table. Let us find the unique results.
Matrix Chain Multiplication
For the previous example, the number of unique solutions are-
Unique function calls No. of subproblems Size
C(1, 1), C(2, 2), C(3, 3), C(4,4) 4 1
C(1, 2), C(2, 3), C(3,4) 3 2
C(1, 3), C(2,4) 2 3
C(1,4) 1 4
Total no, of function calls/ subproblems - 1+2+3+4= 10
Suppose we have n matrices A1 A2 A3, …, An, then
Subproblems Size
No. of subproblems= 1+2+3+…+n
n 1
= n(n+1)/2 = O(n2)
n-1 2
n-2 3 Therefore, for 4 matrices, a table with 4 row
and 4 column need to be created.
. .
We create one more table to store the k
1 n value for which minimum cost has been
found.
Matrix Chain Multiplication
Find the best order to multiply following matrices- 1 2 3 4
1 0 24
A1 A2 A3 A4
d0x d1 d1x d2 d2x d3 d3x d4 2 0 16
3x2 2x4 4x2 2x5
3 0
Step 1: Fill 0 to the matrices of size 1 4 0
C[1,1], C[2,2], C[3,3], C[4,4] =0 C table
2 2
Step 3: Find the value of matrices of size 3
C[1,3] = min 3
K =1 C[1,1]+C[2,3]+d0xd1xd3, 3
1<=k<3
K =2 C[1,2]+C[3,3]+d0xd2xd3 4
= min { 0+16+3x2x2, 24+0+3x4x2}
1<=k<3 K table
= min {28, 48} = 28 (for k=1)
Matrix Chain Multiplication
Find the best order to multiply following matrices- 1 2 3 4
1 0 24 28
A1 A2 A3 A4
d0x d1 d1x d2 d2x d3 d3x d4 2 0 16 36
3x2 2x4 4x2 2x5
3 0 40
C[2,4] = min K =2 C[2,2]+C[3,4]+d1xd2xd4, 4 0
2<=k<4 K =3 C[2,3]+C[4,4]+d1xd3xd4 C table
3 1 2 0 5
3 4 4 3 4 1 0
Cost Matrix
If the salesperson starts from city 1 and visit all other cities, then the problem can be
formulated as follows: Travel 2 from 1 and continue tour from
(1, 2) + T(2, {3, 4}) 2, visit all nodes and get back to 1
Travel 3 from 1 and continue tour from
T(1, {2, 3, 4}) = (1,3) + T(3, {2, 4}) 3, visit all nodes and get back to 1
min
Travel all nodes (1,4) + T(4, {2, 3}) Travel 4 from 1 and continue tour from
and get back to 1 3, visit all nodes and get back to 1
From the above, it has been observed that main problem can be broken into smaller
subproblems. So, it has substructure.
Travelling Salesman Problem
(1, 2) + T(2, {3, 4})
T(1, {2, 3, 4}) = (1,3) + T(3, {2, 4})
min (1,4) + T(4, {2, 3})
E(1, 2) = 1, E(1, 3) = 2, E(1, 4) = 3
T(2, {3, 4}), T(3, {2, 4}), T(4, {2, 3}) are unknown.
If there will no edge b/w two vertices (vertices are not adjacent) cost will ∞.
Let us construct recursion tree for the previous example.
Travelling Salesman Problem
T(1, {2,3,4})
T(3, {4}) T(4, {3}) T(2, {4}) T(4, {2}) T(2, {3}) T(3, {2})
T(4, {Φ}) T(3, {Φ}) T(4, {Φ}) T(2, {Φ}) T(3, {Φ}) T(2, {Φ})
Overlapping subproblems
There are total 15 subproblems and out of which 3 are repeating. So, total unique
subproblems are 12, which is still very large. So, DP approach can also not be able reduce
complexity below exponential. So, it is not recommended to use DP for TSP problem.
Travelling Salesman Problem
At first level, i has only three values i.e., 2, 3, 4 and at second level i has only three
values i.e., 2, 3, 4. Third level also has the same number of values. So, we can say at
each level, i will have (n-1) values. Set S has (n-2) elements at level 2, (n-3) at level 2
and the last level has 0 elements.
Travelling Salesman Problem
The number of unique subproblems for n vertices will be equal to (n-
2) 2n , which is also exponential. The time complexity to find the
shortest path = O(n 2n).
The time complexity of the DP approach is lesser than the brute-force
approach O(n!). However, it is still very high, so it is normally not
recommended to use DP for TSP problem.