DynamicProgramming Part2 FEUP
DynamicProgramming Part2 FEUP
Design of Algorithms
Dynamic Programming
Application Examples:
Combinatorial Arrangements, Matrix-Matrix
Multiplication, 0/1 Knapsack, Longest Common
Sub-Sequence and Coin Change
Copyright 2024, Pedro C. Diniz, all rights reserved.
Students enrolled in the DA class at Faculdade de Engenharia da Universidade do Porto (FEUP)
have explicit permission to make copies of these materials for their personal use.
Outline
• Application Examples of Dynamic Programming
– Combinatorial Arrangements
– Multiplication of Matrices
– 0/1 Knapsack Problem
– Longest Common Sub-sequence (LCS)
– Coin Change
• Dynamic Programming Features
ì1 if k = 0 or k = n
æ n ö ïïæ n - 1ö æ n - 1ö
çç ÷÷ = íçç ÷÷ + çç ÷÷ if 0 < k < n
è k ø ïè k - 1ø è k ø
ïî0 otherwise
• Comments:
– Artificial problem
– Ilustrates the elimination of the repeated computation of
sub-problems
– (((AxB)xC)xD):
• 13x5x89 + 13x89x3 + 13x3x34 = 10582 products
– ((AxB)x(CxD)):
• 13x5x89 + 89x3x34 + 13x89x34 = 54201 products
– ((Ax(BxC))xD):
• 5x89x3 + 13x5x3 + 13x3x34 = 2856 products!
– (Ax((BxC)xD)):
• 5x89x3 + 5x3x34 + 13x5x34 = 4055 products
– (Ax(Bx(CxD)))
• 89x3x34 + 5x89x34 + 13x5x34 = 26418 products
1 æ 2n ö
çç ÷÷ = Wç 4 n 2 ö÷
æ 3
P(n) = C (n - 1) C ( n) = n
n +1 è n ø è ø
– Conclusion:
• Optimal solution for the placement of parenthesis is composed by
optimal solution for sub-problems
ì0 i= j
m[i, j ] = í
îmin i £ k < j { m[i, k ] + m[k + 1, j ] + pi -1 pk p j } i < j
Definition of s[i,j]:
• Problem:
– Recursive solution requires exponential time
– Repeated solution of sub-problems
• Solution:
– Constructive solution (bottom-up)
– Execution Time: Q(n 3 )
1 2 3 4 5 6
A1 30 x 35
A2 35 x 15 6 15,125 10,500 5,375 3,500 5,000 0
A6 20 x 25 j
3 7,875 2,625 0
2 15,750 0
1 0
i
A1 30 x 35 1 2 3 4 5 6
A2 35 x 15
A3 15 x 5 6 15,125 10,500 5,375 3,500 5,000 0
j
3 7,875 2,625 0
2 15,750 0
1 0
i
A1 30 x 35 1 2 3 4 5 6
A2 35 x 15
A3 15 x 5 6 15,125 10,500 5,375 3,500 5,000 0
j
3 7,875 2,625 0
2 15,750 0
1 0
i
A1 30 x 35 1 2 3 4 5 6
A2 35 x 15
A3 15 x 5 6 15,125 10,500 5,375 3,500 5,000 0
j
3 7,875 2,625 0
2 15,750 0
1 0
m = length[p]-1;
for i = 1 to n do
m[i,i] = 0;
for l = 2 to n do
for i = 1 to n - l + 1 do
for j = 1 to i + l - 1 do
m[i,j] = ∞;
for k = i to j - 1 do
q = m[i,k]+m[k+1,j]+pi-1pkpj;
if (q < m[i,j]) then
m[i,j] = q;
s[i,j] = k;
end
end
end Finding the best k
end
Analysis and Synthesis of Algorithms 19
0/1 Knapsack Problem
• Problem Definition:
– Given n objects (1,…,n) and a knapsack
– Each object has value vi and weight wi
– Knapsack weight cannot exceed W
– Objective:
• maximize the value in the knapsack while meeting its weight limit
• Formalization:
– xi = 1 if object i is included in the knapsack; 0 otherwise
n
max åxv
i =1
i i
n
s.t. åx w
i =1
i i £W
• Problem:
v1 = 8, w1 = 6
v2 = 5, w2 = 5
v3 = 5, w3 = 5
W = 10
2 0 0 0 0 0 5 8 8 8 8 8
3 0 0 0 0 0 5 5 5 5 5 10
2 0 0 0 0 0 5 8 8 8 8 8
3 0 0 0 0 0 5 5 5 5 5 10
• Approach:
– If xn = ym, find LCS W for Xn-1 and Ym-1 then
Add xn = ym to W to obtain Z
– If xn ¹ ym, find LCS’s for Xn-1 and Y and for X and Ym-1 then
Select the longest sub-sequence of two sub-problems
• c[i,j]:
– Length of the LCS for the sequences Xi and Yj
• Recurrence:
ì0 if i = 0 or j = 0
ï
c[i, j ] = íc[i - 1, j - 1] + 1 if i, j > 0 and xi = y j
ïmax (c[i - 1, j ], c[i, j - 1]) if i, j > 0 and xi ¹ y j
î
¬
¬
B 0
C 0
B 0
D 0
A 0
B 0
¬
¬
B 0 1 1 1 1 2 2
¬
¬ ¬ ¬
C 1 1 2 2 2 2
¬
0 ¬
1
¬ 1 2 2 3 3
¬
B 0 ¬
1 2 2 2 3 3
¬
D 0
1 2 2 3 3 4
¬
¬
A 0
2 2 3 4
¬
1 4
¬
B 0
¬
¬
B 0 1 1 1 1 2 2
¬
¬ ¬ ¬
C 1 1 2 2 2 2
¬
0 ¬
1
¬ 1 2 2 3 3
¬
B 0 ¬
1 2 2 2 3 3
¬
D 0
1 2 2 3 3 4
¬
¬
A 0
2 2 3 4
¬
1 4
¬
B 0
¬
¬
B 0 1 1 1 1 2 2
¬
¬ ¬ ¬
C 1 1 2 2 2 2
¬
0 ¬
1
¬ 1 2 2 3 3
¬
B 0 ¬
1 2 2 2 3 3
¬
D 0
1 2 2 3 3 4
¬
¬
A 0
2 2 3 4
¬
1 4
¬
B 0
¬
¬
B 0 1 1 1 1 2 2
¬
¬ ¬ ¬
C 1 1 2 2 2 2
¬
0 ¬
1
¬ 1 2 2 3 3
¬
B 0 ¬
1 2 2 2 3 3
¬
D 0
1 2 2 3 3 4
¬
¬
A 0
2 2 3 4
¬
1 4
¬
B 0
• TimeComplexity: Q(n × m )
Analysis and Synthesis of Algorithms 50
Coin Change
• Given a set of coins, from denominations c1,…,cn,
with values d1,…,dn, compute the lowest number of
coins such that its sums is N
– Assume: unlimited number of coins of each denomination
• Formulation:
c[i,0] = 0 1 £ i £ n
c[i, j ] = +¥ i = 0 or j < 0 do not include coin i
• Example: c[i,0] = 0 1 £ i £ n
0 1 15 20 25 30 35 40
0 0 0 0 0 0 0 0
• Example: c[i,0] = 0 1 £ i £ n
j 0 1 15 20 25 30 35 40
i
0 0 0 0 0 0 0 0
0 0 1 1 0 0 1 1 0
• Example: c[i,0] = 0 1 £ i £ n
– v1 = 1; v2 = 5; v3 = 20; v4 = 25 c[i, j ] = +¥ i = 0 or j < 0
– N= 40 c[i, j ] = min(c[i - 1, j ], 1 + c[i, j - d i ])
j 0 1 15 20 25 30 35 40
i
0 0 0 0 0 0 0 0
0 0 1 1 0 0 1 1 0
2 0 0 1 0 2 1 1 0
• Implementation:
– Using a sparse Matrix Representation
• Use of Memoization