UNIT-7 Dynamic Programming
UNIT-7 Dynamic Programming
• Goal
― Item to include in a collection so that
• The total weight is less than some given limit (capacity of bag)
• And the total value is as large as possible.
Object
2
3
4
j
i i/j 0 1 2 3 4 5
0 0
1
2
3
4
B[i,0] = B[0,0] = 0
1
Formula for 0/1 Knapsack Problem :
1) B[i,0] = B[0,j] = 0
j
i i/j 0 1 2 3 4 5
0 0
1 0
2
3
4
B[i,0] = B[1,0] = 0
1
Formula for 0/1 Knapsack Problem :
1) B[i,0] = B[0,j] = 0
j
i i/j 0 1 2 3 4 5
0 0
1 0
2 0
3
4
B[i,0] = B[2,0] = 0
1
Formula for 0/1 Knapsack Problem :
1) B[i,0] = B[0,j] = 0
j
i i/j 0 1 2 3 4 5
0 0
1 0
2 0
3 0
4
B[i,0] = B[3,0] = 0
1
Formula for 0/1 Knapsack Problem :
1) B[i,0] = B[0,j] = 0
j
i i/j 0 1 2 3 4 5
0 0
1 0
2 0
3 0
4 0
B[i,0] = B[4,0] = 0
1
Formula for 0/1 Knapsack Problem :
1) B[i,0] = B[0,j] = 0
j
i i/j 0 1 2 3 4 5
0 0 0
1 0
2 0
3 0
4 0
B[0, j] = B[0,1] = 0
1
Formula for 0/1 Knapsack Problem :
1) B[i,0] = B[0,j] = 0
j
i i/j 0 1 2 3 4 5
0 0 0 0
1 0
2 0
3 0
4 0
B[0, j] = B[0,2] = 0
1
Formula for 0/1 Knapsack Problem :
1) B[i,0] = B[0,j] = 0
j
i i/j 0 1 2 3 4 5
0 0 0 0 0
1 0
2 0
3 0
4 0
B[0, j] = B[0,3] = 0
1
Formula for 0/1 Knapsack Problem :
1) B[i,0] = B[0,j] = 0
j
i i/j 0 1 2 3 4 5
0 0 0 0 0 0
1 0
2 0
3 0
4 0
B[0, j] = B[0,4] = 0
1
Formula for 0/1 Knapsack Problem :
1) B[i,0] = B[0,j] = 0
j
i i/j 0 1 2 3 4 5
0 0 0 0 0 0 0
1 0
2 0
3 0
4 0
B[0, j] = B[0,5] = 0
1
Formula for 0/1 Knapsack Problem :
1) B[i,0] = B[0,j] = 0
i/j 0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3
1 2 0
3 0
4 0
Mr. Prayag Patel Department: IT/ICT-LJIET
0/1 Knapsack Problem
Step-2: Recursively define the value of an optimal solution
wi v i 2) B[i, j] = B[i - 1, j], if j < wi
1 2 3
3) B[i, j] = max { B[i - 1, j], vi + B[i - 1, j - wi]}, otherwise(j ≥ wi)
2 3 4
j ≥ wi ? For B[1,3] , i=1 , j=3 & w1= 2 , v1 = 3
3 4 5
3 ≥ 2 ? Yes
4 5 6
B[1,3] = max { B[1 - 1, 3], v1 + B[1 - 1, 3 – w1]}
= max { B[0, 3], 3 + B[0, 1]} i/j 0 1 2 3 4 5
= max { 0, 3 + 0]} 0 0 0 0 0 0 0
=3 1 0 0 3
1 2 0
3 0
4 0
Mr. Prayag Patel Department: IT/ICT-LJIET
0/1 Knapsack Problem
Step-2: Recursively define the value of an optimal solution
wi v i 2) B[i, j] = B[i - 1, j], if j < wi
1 2 3
3) B[i, j] = max { B[i - 1, j], vi + B[i - 1, j - wi]}, otherwise(j ≥ wi)
2 3 4
j < wi ? For B[1,4] , i=1 , j=4 & w1= 2 , v1 = 3
3 4 5
4 < 2 ? No
4 5 6
i/j 0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3
1 2 0
3 0
4 0
Mr. Prayag Patel Department: IT/ICT-LJIET
0/1 Knapsack Problem
Step-2: Recursively define the value of an optimal solution
wi v i 2) B[i, j] = B[i - 1, j], if j < wi
1 2 3
3) B[i, j] = max { B[i - 1, j], vi + B[i - 1, j - wi]}, otherwise(j ≥ wi)
2 3 4
j ≥ wi ? For B[1,4] , i=1 , j=4 & w1= 2 , v1 = 3
3 4 5
4 ≥ 2 ? Yes
4 5 6
B[1,4] = max { B[1 - 1, 4], v1 + B[1 - 1, 4 – w1]}
= max { B[0, 4], 3 + B[0, 2]} i/j 0 1 2 3 4 5
= max { 0, 3 + 0]} 0 0 0 0 0 0 0
=3 1 0 0 3 3
1 2 0
3 0
4 0
Mr. Prayag Patel Department: IT/ICT-LJIET
0/1 Knapsack Problem
Step-2: Recursively define the value of an optimal solution
wi v i 2) B[i, j] = B[i - 1, j], if j < wi
1 2 3
3) B[i, j] = max { B[i - 1, j], vi + B[i - 1, j - wi]}, otherwise(j ≥ wi)
2 3 4
j < wi ? For B[1,5] , i=1 , j=5 & w1= 2 , v1 = 3
3 4 5
5 < 2 ? No
4 5 6
i/j 0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3
1 2 0
3 0
4 0
Mr. Prayag Patel Department: IT/ICT-LJIET
0/1 Knapsack Problem
Step-2: Recursively define the value of an optimal solution
wi v i 2) B[i, j] = B[i - 1, j], if j < wi
1 2 3
3) B[i, j] = max { B[i - 1, j], vi + B[i - 1, j - wi]}, otherwise(j ≥ wi)
2 3 4
j ≥ wi ? For B[1,4] , i=1 , j=5 & w1= 2 , v1 = 3
3 4 5
5 ≥ 2 ? Yes
4 5 6
B[1,5] = max { B[1 - 1, 5], v1 + B[1 - 1, 5 – w1]}
= max { B[0, 5], 3 + B[0, 3]} i/j 0 1 2 3 4 5
= max { 0, 3 + 0]} 0 0 0 0 0 0 0
=3 1 0 0 3 3 3
1 2 0
3 0
4 0
Mr. Prayag Patel Department: IT/ICT-LJIET
0/1 Knapsack Problem
Step-2: Recursively define the value of an optimal solution
wi v i 2) B[i, j] = B[i - 1, j], if j < wi
1 2 3
3) B[i, j] = max { B[i - 1, j], vi + B[i - 1, j - wi]}, otherwise(j ≥ wi)
2 3 4
j < wi ? For B[2,1] , i=2 , j=1 & w2= 3 , v2 = 4
3 4 5
1 < 3 ? Yes
4 5 6
i/j 0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3 3
2 0 0 3 4 4 7
3 0 0 3 4 5 7
4 0 0 3 4 5 7
Total Profit
Total Profit + =
Mr. Prayag Patel Department: IT/ICT-LJIET
0/1 Knapsack Problem
wi vi
Capacity
1 2 3
2 3 4
3 4 5 i/k 0 1 2 3 4 5
Object
4 5 6 0 0
1
2
3
4
Object
4 5 6 0 0 0
1
2
3
4
Object
4 5 6 0 0 0 0
1
2
3
4
Object
4 5 6 0 0 0 0 0
1
2
3
4
Object
4 5 6 0 0 0 0 0 0
1
2
3
4
Object
4 5 6 0 0 0 0 0 0 0
1
2
3
4
Object
4 5 6
1 0
2
3
4
Object
4 5 6
1 0 0
2
3
4
Object
4 5 6
1 0 0 3
2
3
4
Object
4 5 6
1 0 0 3 3
2
3
4
Object
4 5 6
1 0 0 3 3 3
2
3
4
Object
2
3
4
Object
2 0
3
4
Object
2 0 0
3
4
Object
2 0 0 3
3
4
Object
2 0 0 3 4
3
4
Object
2 0 0 3 4 4
3
4
Object
2 0 0 3 4 4 7
3
4
Object
3 0
4
Object
3 0 0
4
Object
3 0 0 3
4
Object
3 0 0 3 4
4
Object
3 0 0 3 4 5
4
Object
3 0 0 3 4 5 7
4
Object
4 0
Object
4 0 0
Object
4 0 0 3
Object
4 0 0 3 4
Object
4 0 0 3 4 5
Object
4 0 0 3 4 5 7
Total Profit + =
Mr. Prayag Patel Department: IT/ICT-LJIET
TOPIC : LONGEST COMMON SUBSEQUENCE
0 • if i=0 or j=0
1
max(c[i,j-1],c[i-1,j]) • if i,j>0 and Ai ≠ Bj
1st Sequence
A 1
B 2
1 C 3
B 4
D 5
1st Sequence
A 1 0
B 2 0
1 C 3 0
B 4 0
D 5 0
1st Sequence
A 1 0 0 0 0 1 1 1
B 2 0 1 1 1 1 2 2
1 C 3 0 1 1 2 2 2 2
B 4 0 1 1 2 2 3 3
D 5 0 1 2 2 2 3 3
1st Sequence
A 1 0 0 0 0 1 1 1
B 2 0 1 1 1 1 2 2
1 C 3 0 1 1 2 2 2 2
B 4 0 1 1 2 2 3 3
D 5 0 1 2 2 2 3 3
1st Sequence
A 1 0 0 0 0 1 1 1
B 2 0 1 1 1 1 2 2
1 C 3 0 1 1 2 2 2 2
B 4 0 1 1 2 2 3 3
D 5 0 1 2 2 2 3 3
1 2 3 4 5
A[ i ] M L N O M
B[ j ] L M O M
0 • if i=0 or j=0
1
max(c[i,j-1],c[i-1,j]) • if i,j>0 and Ai ≠ Bj
1st Sequence
L 2
N 3
1
O 4
M 5
C[i,0] = C[0,0] = 0 j L M O M
i i/j 0 1 2 3 4
0 0
M 1
L 2
N 3
O 4
M 5
1
C[i,0] = C[1,0] = 0 j L M O M
i i/j 0 1 2 3 4
0 0
M 1 0
L 2
N 3
O 4
M 5
1
C[i,0] = C[2,0] = 0 j L M O M
i i/j 0 1 2 3 4
0 0
M 1 0
L 2 0
N 3
O 4
M 5
1
C[i,0] = C[3,0] = 0 j L M O M
i i/j 0 1 2 3 4
0 0
M 1 0
L 2 0
N 3 0
O 4
M 5
1
C[i,0] = C[4,0] = 0 j L M O M
i i/j 0 1 2 3 4
0 0
M 1 0
L 2 0
N 3 0
O 4 0
M 5
1
C[i,0] = C[5,0] = 0 j L M O M
i i/j 0 1 2 3 4
0 0
M 1 0
L 2 0
N 3 0
O 4 0
M 5 0
1
C[0,j] = C[0,1] = 0 j L M O M
i i/j 0 1 2 3 4
0 0 0
M 1 0
L 2 0
N 3 0
O 4 0
M 5 0
1
C[0,j] = C[0,2] = 0 j L M O M
i i/j 0 1 2 3 4
0 0 0 0
M 1 0
L 2 0
N 3 0
O 4 0
M 5 0
1
C[0,j] = C[0,3] = 0 j L M O M
i i/j 0 1 2 3 4
0 0 0 0 0
M 1 0
L 2 0
N 3 0
O 4 0
M 5 0
1
C[0,j] = C[0,4] = 0 j L M O M
i i/j 0 1 2 3 4
0 0 0 0 0 0
M 1 0
L 2 0
N 3 0
O 4 0
M 5 1 0
1) A = X Y Z Y T X Y & B = Y T Z X Y X
2) A = a b b a c d c b a & B = b c d b b c a a c
3) A = 1 0 0 1 0 1 1 0 1 1 0 1 & B = 0 1 1 0
Assistant Professor
IT-ICT Department, LJIET
[email protected]
Mr. Prayag Patel
Matrix Chain Multiplication
▪ Problem Definition
➢ Input:
― N matrices A1, A2, A3,………… An of dimensions P1 x P2 ,
P2 x P3 , P3 x P4 ,………… Pn x Pn+1 respectively.
➢ Goal:
― To compute the matrix product A1 x A2 x A3 x…………x An
➢ Problem:
― In what order should A1, A2, A3,………… An be multiplied
so that it would take the minimum number of
computations to derive the product.
A B C
PxQ QxR PxR
Mr. Prayag Patel Department: IT/ICT-LJIET
Matrix Chain Multiplication
▪ Problem Definition
A B C
2x3 3x4 2x4
PxQ QxR PxR
A B C
5x4 4x6 6x2
X C
5x6 6x2
A B C
5x4 4x6 6x2
A X
5x4 4x2
➢ Possible multiplication are:
2) A (BC) = (4 * 6 * 2) + (5 * 4 * 2)
= 48 + 40
= 88
Mr. Prayag Patel Department: IT/ICT-LJIET
Matrix Chain Multiplication
▪ Problem Definition
▪ Example: Three matrices A, B and C
A B C
5x4 4x6 6x2
A B C D
2x4 4x2 2x3 3x1
➢ Possible multiplication are:
multiplication required
Total number of scaler
1) A(B(CD)) = (2*3*1) + (4*2*1) + (2*4*1) = 6 + 8 + 8 = 22
2) A((BC)D) = (4*2*3) + (4*3*1) + (2*4*1) = 24 + 12 + 8 = 44
3) (AB)(CD) = (2*4*2) + (2*3*1) + (2*2*1) = 16 + 6 + 4 = 26
4) ((AB)C)D = (2*4*2) + (2*2*3) + (2*3*1) = 16 + 12 + 6 = 34
5) (A(BC))D = (4*2*3) + (2*4*3) + (2*3*1) = 24 + 24 + 6 = 54
0 • if i = j
m[i,j] =
min(m[i,k]+m[k+1,j]+ pi-1pkpj) • if i < j
With i≤ k ≤j-1
1
Matrix Dimension
A1 5x4
Here consider
A2 4x6
p0 = 5
A3 6x2 p1 = 4
A4 2x7 p2 = 6
p3 = 2
p4 = 7
1
m[1,1] = 0
m[i, j] = 0 • if i = j
0
Mr. Prayag Patel Department: IT/ICT-LJIET
Matrix Chain Multiplication
Step-2: Recursively define the value of an optimal solution
Matrix Dimension
j 4 1
A1 5x4
3 2 i
A2 4x6
2 3
A3 6x2
1 4
A4 2x7 0 0
m[2,2] = 0
m[i, j] = 0 • if i = j
0
Mr. Prayag Patel Department: IT/ICT-LJIET
Matrix Chain Multiplication
Step-2: Recursively define the value of an optimal solution
Matrix Dimension
5x4
4 1 i
A1
j 3 2
A2 4x6
2 3
A3 6x2
1 4
A4 2x7 0 0 0
m[3, 3] = 0
m[i, j] = 0 • if i = j
0
Mr. Prayag Patel Department: IT/ICT-LJIET
Matrix Chain Multiplication
Step-2: Recursively define the value of an optimal solution
Matrix Dimension
5x4
4 1 i
A1
j 3 2
A2 4x6
2 3
A3 6x2
1 4
A4 2x7 0 0 0 0
m[4, 4] = 0
m[i, j] = 0 • if i = j
0
Mr. Prayag Patel Department: IT/ICT-LJIET
Matrix Chain Multiplication
Step-2: Recursively define the value of an optimal solution
Matrix Dimension
A1 5x4 p0 = 5
j 4 1 i
p1 = 4 3 2
A2 4x6
p2 = 6 3
A3 6x2 2
p3 = 2 1 4
A4 2x7 p4 = 7
0 0 0 0
m[1, 2] = min(m[1,1]+m[2,2]+ p0p1p2) i=1&j=2
= min(0 + 0 + 5*4*6) i ≤ k ≤ j-1
= 120 1 ≤ k ≤ 1 So k = 1
m[i, j] = min(m[i,k]+m[k+1,j]+ pi-1pkpj) • if i < j With i≤ k ≤j-1