Unit-4 Dynamic Programming: Dr. Gopi Sanghani
Unit-4 Dynamic Programming: Dr. Gopi Sanghani
GTU # 3150703
Unit-4
Dynamic Programming
Greedy Approach
A, B A, C A, D C, D B, D B, C
¿ 𝟏 𝒊𝒇 𝒌 =𝟎 𝒐𝒓 𝒌 =𝒏
() 𝒌
{
𝒏 = ¿ 𝒏 − 𝟏 + 𝒏 − 𝟏 𝒊𝒇 𝟎<𝒌 < 𝒏
(
𝒌 −𝟏 𝒌 )(
¿ 𝟎 𝑶𝒕𝒉𝒆𝒓𝒘𝒊𝒔𝒆
)
function C(n, k)
if k=0 or k=n then return 1
else return C(n-1, k-1) + C(n-1, k)
To generate table 𝒄[𝒊][𝒋] use following steps:
Step-1: Make 𝑓𝑜𝑟
Repeat step-2 to step-4 for the remaining matrix values
Optimal
Step-2: If then Sub-structure
Step-3: If then
Step-4: Otherwise
𝑗
Amount
0 1 2 3 4 5 6 7 8
𝒊=𝟏 0 1 2 3 4 5 6 7 8
𝒊=𝟐 0 1 2 3 1 2
𝒊=𝟑 0
min ( 𝑐[1][5],
𝑚𝑖𝑛(𝑐
1+𝑐[2][0])=min
[1][4 ], 1+𝑐 (4,1+0)=min (4,1)=1
[2][1])=𝑚𝑖𝑛(5,1+1)=𝑚𝑖𝑛(5,2)=2
Dr. Gopi Sanghani #3150703 (ADA) Unit 4 – Dynamic Programming 14
Make Change Problem – Dynamic Programming Solution
Denominations: . Make a change of Rs. .
Step-1:Make
Step-2: If then here
Step-3: If then
Step-4: Otherwise
𝑗
Amount
0 1 2 3 4 5 6 7 8
𝒊=𝟏 0 1 2 3 4 5 6 7 8
𝒊=𝟐 0 1 2 3 1 2 3 4 2
𝒊=𝟑 0 1 2 3 1 2 1 2 2
To generate table use following steps:
Step-1: Make
Step-2: if then
Step-3: if then
1. and
Object
11 66 18
18 22
22 28
28
11 22 55 66 77
if then
else
𝒋 Knapsack Capacity
weight & value 0 1 2 3 4 5 6 7 8 9 10 11
0 1 1 1 1
0 1 6 7 7
0 1 6 7 7
0 1 6 7 7
0 1 6 7 7
So,
So,
if then
else
𝒋 Knapsack Capacity
weight & value 0 1 2 3 4 5 6 7 8 9 10 11
0 1 1 1 1 1 1 1 1 1 1 1
0 1 6 7 7 7 7 7 7 7 7 7
0 1 6 7 7 18 19 24 25 25 25 25
0 1 6 7 7 18 22 24 28 29 29 40
0 1 6 7 7 18 22 28 29 34 35 40
𝑀𝑖𝑛(18+3
, 16+1+3)=min ( 21 ,20 )=¿ 20 ¿
Dr. Gopi Sanghani #3150703 (ADA) Unit 4 – Dynamic Programming 32
Assembly Line Scheduling – Dynamic Programming Solution
Step: 1 Initialization 3 𝟑𝟓
4
1 2 3 4
1
D0 = For node 3
2
3
4
1 2 3 4
1
D0 = For node 4
2
3
4
So,
Where, and
1
Dimension of
Dimension of
Dimension of
10582
Dr. Gopi Sanghani #3150703 (ADA) Unit 4 – Dynamic Programming 48
Matrix Chain Multiplication
Now, we want to calculate the product of more than two matrices. Matrix multiplication is
associative.
The product of four matrices can be fully parenthesized in distinct ways.
1
Dimension of
2
3
Dimension of
4
5 Dimension of
54201
Dr. Gopi Sanghani #3150703 (ADA) Unit 4 – Dynamic Programming 49
Matrix Chain Multiplication using Dynamic Programming
Matrix table stores the cost of multiplications.
To generate use following steps:
Step-1: if then
Step-2: if then
Step-3: if then
Step-2:
Step-1: if if then
then
[3][4 ]=𝑃012∗∗𝑃So,
𝑀 [1][2]=𝑃
[2][3]=𝑃 𝑃123∗∗𝑃𝑃234=5
=6∗∗462∗
=4 ∗∗2=48
6=120
7=84
1 2 3 4
1 0 120
2 -- 0 48
3 -- -- 0 84
4 -- -- -- 0
1 2 3 4
1 0 120 88
2 -- 0 48
3 -- -- 0 84
4 -- -- -- 0
1 2 3 4
1 0 120 88
2 -- 0 48 104
3 -- -- 0 84
4 -- -- -- 0
1 2 3 4
1 0 120 88 158
2 -- 0 48 104
3 -- -- 0 84
4 -- -- -- 0
A B C D
1 2 3 4
1 0 120 88 158
2 -- 0 48 104
3 -- -- 0 84
4 -- -- -- 0
A B C D
1 2 3 4
1 0 120 88 158
2 -- 0 48 104
3 -- -- 0 84
4 -- -- -- 0
A B C D
158
0 0 0 0 0 0 0 0
0 0 1 1 1 1 1 1
0 0 1 1 1 2 2 2
0 0 1 2 2 2 2 2
0 1 1 2 2 2 3 3
0 1 2 2 3 3 3 4
0 1 2 2 3 3 4 4
Dr. Gopi Sanghani #3150703 (ADA) Unit 4 – Dynamic Programming 61
LCS – Dynamic Programming Solution
else max(,
0 0 0 0 0 0 0 0
0 0 1 1 1 1 1 1
0 0 1 1 1 2 2 2
0 0 1 2 2 2 2 2
0 1 1 2 2 2 3 3
0 1 2 2 3 3 3 4
0 1 2 2 3 3 4 4
Dr. Gopi Sanghani #3150703 (ADA) Unit 4 – Dynamic Programming 62
LCS – Dynamic Programming Solution
else max(,
0 0 0 0 0 0 0 0
0 0 1 1 1 1 1 1
0 0 1 1 1 2 2 2
0 0 1 2 2 2 2 2
0 1 1 2 2 2 3 3
0 1 2 2 3 3 3 4
0 1 2 2 3 3 4 4
Dr. Gopi Sanghani #3150703 (ADA) Unit 4 – Dynamic Programming 63
LCS – Dynamic Programming Solution
and and LCS =
0 0 0 0 0 0 0 0
0 0 1 1 1 1 1 1
0 0 1 1 1 2 2 2
0 0 1 2 2 2 2 2
0 1 1 2 2 2 3 3
0 1 2 2 3 3 3 4
0 1 2 2 3 3 4 4
Dr. Gopi Sanghani #3150703 (ADA) Unit 4 – Dynamic Programming 64
Thank You!