You are on page 1of 67

Analysis and Design of

Algorithms

Dynamic Programming
Examples
•  Matrix chain multiplication
CS3230! •  Coin changing
•  Knapsack problem

A/P Lee Wee Sun


Analysis and Design of Algorithms
Matrix Chain
Multiplication
Different parenthesization of a chain of matrix
for multiplication can have dramatic impact on
the amount of computation required.

Example: ((A1A2)A3) and (A1(A2A3)) are two


different parenthesization of the same matrix
chain multiplication A1A2A3.

Analysis and Design of Algorithms 2


Consider A1, A2, A3 of sizes 10 x 100, 100 x 5
and 5 x 50 respectively:
q  ((A1A2)A3) cost q  (A1(A2A3)) cost
10*100*5=5000 100*5*50=25000
scalar mult followed by another
followed by 10*100*50=50000
10*5*50=2500 for a total of 75000
scalar mult for total scalar
of 7500 mult. multiplications.
.3
Matrix-chain multiplication problem:
Given a chain <A1,A2,…,An> of n matrices,
where for i =1,2,…,n, the matrix Ai has
dimension pi-1 x pi, parenthesize the product
A1A2…An in a way that minimizes the number
of scalar multiplications.

Analysis and Design of Algorithms .4


Optimal substructure:
Assume optimal parenthesization splits
product AiAi+1…Aj between Ak and Ak+1,
where i ≤ k ≤ j.
Then the parenthesizations of AiAi+1…Ak and
Ak+1…Aj within the optimal parenthesization
must also be optimal.

Analysis and Design of Algorithms 5


Proof:
Otherwise, by cut and paste, we can replace
the parenthesization of AiAi+1…Ak or Ak+1…Aj
by the optimal parenthesization and improve
the optimal parenthesization of AiAi+1…Aj : a
contradiction.

Analysis and Design of Algorithms 6


Recursive solution:
Let m[i,j] be the minimum cost for computing
the matrix Ai..j. Then for computing Ai..kAk+1..j :
m[i, j] =
#
% 0 if i = j
$
% min i≤k≤ j {m[i, k]+ m[k +1, j]+ pi−1 pk p j } if i < j.
&
Analysis and Design of Algorithms 7
Proof:
If i = j, cost is 0 as no multiplication required.
Otherwise, the optimal split can occur at
positions i ≤ k < j. By the optimal substructure
property, we need the optimal
parenthesization for AiAi+1…Ak and Ak+1…Aj .
Furthermore, AiAi+1…Ak has size pi-1 x pk
while Ak+1…Aj has size pk x pj, so the optimal
cost in this case is
min i≤k≤ j {m[i, k]+ m[k +1, j]+ pi−1 pk p j }
8
MATRIX-CHAIN-ORDER(p)
n ← length[p] – 1
for i ← 1 to n ⊳ Initialization
m[i,i] ← 0
⊳ Main loop
….
⊳ Return cost and reconstruction tables
return m and s
Analysis and Design of Algorithms 9
⊳ Main loop, l is chain length
for l ← 2 to n
for i ← 1 to n – l + 1 m
6 1
j←i+l–1 j 5 2 i
4 3
3 4
m[i,j] ← ∞ 2 5
1 6
0 0 0 0 0 0
for k ← i to j – 1
q ← m[i,k] + m[k+1,j] + pi-1pkpj
if q < m[i,j] then
m[i,j] ← q
s[i,j] ← k
Analysis and Design of Algorithms 10
Overlapping subproblems:
Only Θ(n2) entries for m[i,j].

For each entry, Θ(n) computations,


so total runtime Θ(n3)

Analysis and Design of Algorithms 11


m
6 1
j 5 2 i
4 3
3 4
2 5
1 6 matrix dimension
0 0 0 0 0 0 A1 30 x 35
A2 35 x 15
s A3 15 x 5
6 1
j 5 2 i A4 5 x 10
4 3 A5 10 x 20
3 4
2 A6 20 x 25
5
12
m
6 1
j 5 2 i
4 3
3 4
2 5
1 15750 6 matrix dimension
0 0 0 0 0 0 A1 30 x 35
A2 35 x 15
s A3 15 x 5
6 1
j 5 2 i A4 5 x 10
4 3 A5 10 x 20
3 4
2 A6 20 x 25
5
1 13
m
6 1
j 5 2 i
4 3
3 4
2 5
1 15750 2625 6 matrix dimension
0 0 0 0 0 0 A1 30 x 35
A2 35 x 15
s A3 15 x 5
6 1
j 5 2 i A4 5 x 10
4 3 A5 10 x 20
3 4
2 A6 20 x 25
5
1 2 14
m
6 1
j 5 2 i
4 3
3 4
2 5
1 15750 2625 750 6 matrix dimension
0 0 0 0 0 0 A1 30 x 35
A2 35 x 15
s A3 15 x 5
6 1
j 5 2 i A4 5 x 10
4 3 A5 10 x 20
3 4
2 A6 20 x 25
5
1 2 3 15
m
6 1
j 5 2 i
4 3
3 4
2 5
1 15750 2625 750 1000 6 matrix dimension
0 0 0 0 0 0 A1 30 x 35
A2 35 x 15
s A3 15 x 5
6 1
j 5 2 i A4 5 x 10
4 3 A5 10 x 20
3 4
2 A6 20 x 25
5
1 2 3 4 16
m
6 1
j 5 2 i
4 3
3 4
2 5
1 15750 2625 750 1000 5000 6 matrix dimension
0 0 0 0 0 0 A1 30 x 35
A2 35 x 15
s A3 15 x 5
6 1
j 5 2 i A4 5 x 10
4 3 A5 10 x 20
3 4
2 A6 20 x 25
5
1 2 3 4 5 17
m
6 1
j 5 2 i
4 3
3 4
2 7875 5
1 15750 2625 750 1000 5000 6 matrix dimension
0 0 0 0 0 0 A1 30 x 35
A2 35 x 15
s A3 15 x 5
6 1
j 5 2 i A4 5 x 10
4 3 A5 10 x 20
3 4
2 1 A6 20 x 25
5
1 2 3 4 5 18
m
6 1
j 5 2 i
4 3
3 4
2 7875 4375 5
1 15750 2625 750 1000 5000 6 matrix dimension
0 0 0 0 0 0 A1 30 x 35
A2 35 x 15
s A3 15 x 5
6 1
j 5 2 i A4 5 x 10
4 3 A5 10 x 20
3 4
2 1 3 A6 20 x 25
5
1 2 3 4 5 19
m
6 1
j 5 2 i
4 3
3 4
2 7875 4375 2500 5
1 15750 2625 750 1000 5000 6 matrix dimension
0 0 0 0 0 0 A1 30 x 35
A2 35 x 15
s A3 15 x 5
6 1
j 5 2 i A4 5 x 10
4 3 A5 10 x 20
3 4
2 1 3 3 A6 20 x 25
5
1 2 3 4 5 20
m
6 1
j 5 2 i
4 3
3 4
2 7875 4375 2500 3500 5
1 15750 2625 750 1000 5000 6 matrix dimension
0 0 0 0 0 0 A1 30 x 35
A2 35 x 15
s A3 15 x 5
6 1
j 5 2 i A4 5 x 10
4 3 A5 10 x 20
3 4
2 1 3 3 5 5 A6 20 x 25
1 2 3 4 5 21
m
6 1
j 5 2 i
4 3
3 9375 4
2 7875 4375 2500 3500 5
1 15750 2625 750 1000 5000 6 matrix dimension
0 0 0 0 0 0 A1 30 x 35
A2 35 x 15
s A3 15 x 5
6 1
j 5 2 i A4 5 x 10
4 3 A5 10 x 20
3 3 4
2 1 3 3 5 5 A6 20 x 25
1 2 3 4 5 22
m
6 1
j 5 2 i
4 3
3 9375 7125 4
2 7875 4375 2500 3500 5
1 15750 2625 750 1000 5000 6 matrix dimension
0 0 0 0 0 0 A1 30 x 35
A2 35 x 15
s A3 15 x 5
6 1
j 5 2 i A4 5 x 10
4 3 A5 10 x 20
3 3 3 4
2 1 3 3 5 5 A6 20 x 25
1 2 3 4 5 23
m
6 1
j 5 2 i
4 3
3 9375 7125 5375 4
2 7875 4375 2500 3500 5
1 15750 2625 750 1000 5000 6 matrix dimension
0 0 0 0 0 0 A1 30 x 35
A2 35 x 15
s A3 15 x 5
6 1
j 5 2 i A4 5 x 10
4 3 A5 10 x 20
3 3 3 3 4
2 1 3 3 5 5 A6 20 x 25
1 2 3 4 5 24
m
6 1
j 5 2 i
4 11875 3
3 9375 7125 5375 4
2 7875 4375 2500 3500 5
1 15750 2625 750 1000 5000 6 matrix dimension
0 0 0 0 0 0 A1 30 x 35
A2 35 x 15
s A3 15 x 5
6 1
j 5 2 i A4 5 x 10
4 3 3 A5 10 x 20
3 3 3 3 4
2 1 3 3 5 5 A6 20 x 25
1 2 3 4 5 25
m
6 1
j 5 2 i
4 11875 10500 3
3 9375 7125 5375 4
2 7875 4375 2500 3500 5
1 15750 2625 750 1000 5000 6 matrix dimension
0 0 0 0 0 0 A1 30 x 35
A2 35 x 15
s A3 15 x 5
6 1
j 5 2 i A4 5 x 10
4 3 3 3 A5 10 x 20
3 3 3 3 4
2 1 3 3 5 5 A6 20 x 25
1 2 3 4 5 26
m
6 1
j 5 15125 2 i
4 11875 10500 3
3 9375 7125 5375 4
2 7875 4375 2500 3500 5
1 15750 2625 750 1000 5000 6 matrix dimension
0 0 0 0 0 0 A1 30 x 35
A2 35 x 15
s A3 15 x 5
6 1
j 5 3 2 i A4 5 x 10
4 3 3 3 A5 10 x 20
3 3 3 3 4
2 1 3 3 5 5 A6 20 x 25
1 2 3 4 5 27
Print
Parenthesization
PRINT-OPTIMAL-PARENS(s,i,j)
if i = j then
print “A”i
else print “(“
PRINT-OPTIMAL-PARENS(s,i,s[i,j])
PRINT-OPTIMAL-PARENS(s,s[i,j]+1,j)
print “)”
Analysis and Design of Algorithms 28
Recap (Matrix chain multiplication)
Optimal substructure – leads to recursive
solution
Overlapping subproblems Θ(n2) problems
Dynamic programming – solve smaller
subproblems before larger subproblems Θ(n3)

Analysis and Design of Algorithms 29


Analysis and Design of
Algorithms

Dynamic Programming
Examples
•  Matrix chain multiplication
CS3230! •  Coin changing
•  Knapsack problem

A/P Lee Wee Sun


Analysis and Design of Algorithms
Coin Changing

Problem: Need change for n cents. Use the


fewest coin possible. There are k
denomination d1,…,dk.
q  Always picking the largest possible coin
(greedy) does not always work

Analysis and Design of Algorithms 31


Counterexample to greedy:
q  Available denomination: 25¢, 10¢, 1¢.

25 10 1

q  Make change for 30¢


•  Greedy picks 25¢+1¢+1¢+1¢+1¢+1¢.
•  Optimal is 10¢+10¢+10¢.
Analysis and Design of Algorithms 32
Optimal substructure:
An optimal solution M[j] for j cents can be
decomposed into M[i]+1, where M[i] is the
optimal solution for some i < j.
Proof: (Cut and paste) If M[i] is not optimal,
we can reduce the cost by using the optimal
change for i.

Analysis and Design of Algorithms 33


Recursive solution:
There are k possible positions for i: n-d1, n-d2,
…, n-dk. So
⎧1 + min1≤ j ≤k {M (i − d j )}
⎪
M (i) = ⎨ 0 i=0
⎪ ∞ i<0
⎩

Analysis and Design of Algorithms 34


Memoized Solution
NUM-COINS(i)
⊳ Terminal cases
Define an array M[0,n].
if i <0 then
Initialize M[0]=0,
return ∞
other elements to ∞.
if M[i] ≠ ∞ then
return M[i]
else
⊳ Solve subproblems
….
return M[i]
⊳ Solve subproblems
for j ←1 to k
currCoins = NUM-COINS(i-dj)+1
if M[i] > currCoins then
M[i] = currCoins

Analysis and Design of Algorithms 36


Overlapping subproblems:
Only Θ(n) entries for M[i].

For each entry, Θ(k) computation to


check k subproblems, so total runtime Θ(nk)

Analysis and Design of Algorithms 37


Example: n = 9,
denominations 3, 5, 7
NUM-COINS(9)

Analysis and Design of Algorithms 38


Example: n = 9,
denominations 3, 5, 7
NUM-COINS(9)
9-3 9-7
9-5
NUM-COINS(6) NUM-COINS(4) NUM-COINS(2)

Analysis and Design of Algorithms 39


Example: n = 9,
denominations 3, 5, 7
NUM-COINS(9)
9-3 9-7
9-5
NUM-COINS(6) NUM-COINS(4) NUM-COINS(2)
6-3 6-7
6-5

NUM-COINS(3) NUM-COINS(1) ∞

Analysis and Design of Algorithms 40


Example: n = 9,
denominations 3, 5, 7
NUM-COINS(9)
9-3 9-7
9-5
NUM-COINS(6) NUM-COINS(4) NUM-COINS(2)
6-3 6-7
6-5

NUM-COINS(3) NUM-COINS(1) ∞
3-3

0 3-5, 3-7

Analysis and Design of Algorithms 41


Example: n = 9,
denominations 3, 5, 7
NUM-COINS(9)
9-3 9-7
9-5
NUM-COINS(6) NUM-COINS(4) NUM-COINS(2)
6-3 6-7
6-5

NUM-COINS(3) NUM-COINS(1) ∞
1-3, 1-5, 1-7
3-3

0 3-5, 3-7

Analysis and Design of Algorithms 42


Example: n = 9,
denominations 3, 5, 7
NUM-COINS(9)
9-3 9-7
9-5
NUM-COINS(6) NUM-COINS(4) NUM-COINS(2)
6-3 6-7 4-3 4-5,4-7
6-5

NUM-COINS(3) NUM-COINS(1) ∞
1-3, 1-5, 1-7
3-3

0 3-5, 3-7

Analysis and Design of Algorithms 43


Example: n = 9,
denominations 3, 5, 7
NUM-COINS(9)
9-3 9-7
9-5
NUM-COINS(6) NUM-COINS(4) NUM-COINS(2)
6-3 6-7 4-3 4-5,4-7 2-3, 2-5, 2-7
6-5

NUM-COINS(3) NUM-COINS(1) ∞
1-3, 1-5, 1-7
3-3

0 3-5, 3-7

Analysis and Design of Algorithms 44


Example: n = 9,
denominations 3, 5, 7
NUM-COINS(9)
9-3 9-7
9-5
NUM-COINS(6) NUM-COINS(4) NUM-COINS(2)
6-3 6-7 4-3 4-5,4-7 2-3, 2-5, 2-7
6-5

NUM-COINS(3) NUM-COINS(1) ∞
1-3, 1-5, 1-7
3-3

0 3-5, 3-7 Instances 5,7,8


never solved!
Analysis and Design of Algorithms 45
Dynamic
Programming
NUM-COINS-DP(n)
for i ← 0 to n
M[i] ← ∞
M[0] ← 0 Runtime Θ(nk)
for i ← 1 to n All subproblems solved.
for j ←1 to k
if (i-dj ≥ 0) and (M[i-dj]+1 < M[i]) then
M[i] ← M[i-d[j]] +1 46
Recap (Coin Changing)
•  Greedy algorithm does not give optimal
solution to coin changing
•  Satisfies optimal substructure property –
recursive solution
•  Overlapping subproblems, Θ(n) problems
•  Memoized solution – some subproblems do
not need to be solved, Θ(n) runtime
Analysis and Design of Algorithms 47
Analysis and Design of
Algorithms

Dynamic Programming
Examples
•  Matrix chain multiplication
CS3230! •  Coin changing
•  Knapsack problem

A/P Lee Wee Sun


Analysis and Design of Algorithms
Knapsack Problem

What is the
maximum
value you
can get?

$15

Analysis and Design of Algorithms 49


KNAPSACK
Input:
(w1, v1), (w2, v2), …, (wn, vn), and W
Output: A subset S ⊆ {1, 2, …, n} that
maximizes
∑i ∈ S vi such that ∑i ∈ S wi ≤ W

Analysis and Design of Algorithms 50


Simple Slow Solution

KNAPSACK({v1, v2, …, vn}, {w1, w2, …, wn}, W)


maxV = 0
for every subset S ⊆ {1, 2, …, n}
if ∑i ∈ S wi ≤ W and ∑i ∈ S vi > maxV then
maxV = ∑i ∈ S vi
return maxV

Analysis and Design of Algorithms 51


Running time:
There are 2n possible subsets S of {1, 2, …, n}
It takes O(n) time to compute ∑i ∈ S wi and
∑i∈S vi
Hence, the running time is O(n2n)

Decision version of KNAPSACK is NP-complete!


Any efficient algorithm?
Analysis and Design of Algorithms 52
Dynamic
Programming
Problem: (w1, v1), …, (wn, vn), W

v1 v2 vn-1 vn
w1 w2 …. wn-1 wn

Is there optimal substructure?

Analysis and Design of Algorithms 53


Case 1: Item n (the last one) is taken
Have optimal solution to subproblem
defined by (w1, v1), …, (wn-1, vn-1), W-wn
v1 v2 vn-1 vn
w1 w2 …. wn-1 wn

W-wn
Otherwise, by cut and paste
argument, we can get a better solution
Case 2: Item n (the last one) is not taken
Have optimal solution to subproblem
defined by (w1, v1), …, (wn-1, vn-1), W
v1 v2 vn-1
w1 w2 …. wn-1

W
Otherwise, by cut and paste
argument, we can get a better solution
Recursive solution:
Let m[i,w] be the maximum value that can be
obtained using a subset of items in {1,2,..,i}
with total weight no more than w. Then
# 0 if i = 0 or w = 0
%
m[i, w] = $ max{m[i −1, w − wi ]+ vi , m[i −1, w]} if wi ≤ w
%
& m[i −1, w] otherwise

Analysis and Design of Algorithms 56


KNAPSACK(v, w, W)
for j ← 0 to W ⊳ Initialization
m[0, j] ← 0
for i ← 1 to n ⊳ Initialization
m[i, 0] ← 0
⊳ Recursive cases

return m[n, W]
57
#
% 0 if i = 0 or w = 0
m[i, w] = $ max{m[i −1, w − wi ]+ vi , m[i −1, w]} if wi ≤ w
%
& m[i −1, w] otherwise

⊳ Recursive cases
for i ← 1 to n Runtime: Θ(nW)
for j ← 0 to W
if j ≥ w[i] then
m[i, j] ← max{m[i-1, j-w[i]]+ v[i],m[i-1, j]}
else
m[i, j] ← m[i-1, j]
58
v[1..5] = {4, 2, 10, 1, 2}
w[1..5] = {12, 1, 4, 1, 2}
W = 15
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1 0
2 0
3 0
4 0
5 0
59
#
% 0 if i = 0 or w = 0
m[i, w] = $ max{m[i −1, w − wi ]+ vi , m[i −1, w]} if wi ≤ w
%
& m[i −1, w] otherwise

v[1..5] = {4, 2, 10, 1, 2}


w[1..5] = {12, 1, 4, 1, 2}
W = 15
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 0 0 0 0 4
2 0
3 0
4 0
5 0
Analysis and Design of Algorithms 60
#
% 0 if i = 0 or w = 0
m[i, w] = $ max{m[i −1, w − wi ]+ vi , m[i −1, w]} if wi ≤ w
%
& m[i −1, w] otherwise

v[1..5] = {4, 2, 10, 1, 2}


w[1..5] = {12, 1, 4, 1, 2}
W = 15
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 0 0 0 0 4 4 4 4
2 0 2 2 2 2 2 2 2 2 2 2 2 4 6 6 6
3 0 2 2 2 10 12
4 0
5 0
Analysis and Design of Algorithms 61
#
% 0 if i = 0 or w = 0
m[i, w] = $ max{m[i −1, w − wi ]+ vi , m[i −1, w]} if wi ≤ w
%
& m[i −1, w] otherwise

v[1..5] = {4, 2, 10, 1, 2}


w[1..5] = {12, 1, 4, 1, 2}
W = 15
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 0 0 0 0 4 4 4 4
2 0 2 2 2 2 2 2 2 2 2 2 2 4 6 6 6
3 0 2 2 2 10 12 12 12 12 12 12 12 12 12 12 12
4 0 2 3 3 10 12
5 0
Analysis and Design of Algorithms 62
#
% 0 if i = 0 or w = 0
m[i, w] = $ max{m[i −1, w − wi ]+ vi , m[i −1, w]} if wi ≤ w
%
& m[i −1, w] otherwise

v[1..5] = {4, 2, 10, 1, 2}


w[1..5] = {12, 1, 4, 1, 2}
W = 15
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 0 0 0 0 4 4 4 4
2 0 2 2 2 2 2 2 2 2 2 2 2 4 6 6 6
3 0 2 2 2 10 12 12 12 12 12 12 12 12 12 12 12
4 0 2 3 3 10 12 13 13 13 13 13 13 13 13 13 13
5 0 2 3 4 10 12 13 14 15 15 15 15 15 15 15 15
Analysis and Design of Algorithms 63
PRINT-SET(n,ω)
if ω ≥ w[i] and
m[i,ω] = m(i‐1, ω‐w[i])+v[i] then
return PRINT-SET(i‐1, ω – w[i])∪{i}
else v[1..5] = {4, 2, 10, 1, 2}
w[1..5] = {12, 1, 4, 1, 2}
return PRINT-SET(i‐1, ω) W = 15

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 0 0 0 0 4 4 4 4
✔ 2 0 2 2 2 2 2 2 2 2 2 2 2 4 6 6 6
✔ 3 0 2 2 2 10 12 12 12 12 12 12 12 12 12 12 12
✔ 4 0 2 3 3 10 12 13 13 13 13 13 13 13 13 13 13
✔ 5 0 2 3 4 10 12 13 14 15 15 15 15 15 15 15 15
Analysis and Design of Algorithms 64
Is KNAPSACK in P?

Recall that the decision version of KNAPSACK


is NP-complete
Hence the optimization version of KNAPSACK
is NP-hard

Runtime of dynamic programming is Θ(nW)


What’s happening?

Analysis and Design of Algorithms 65


The runtime is actually Θ(n2l) where l is the
length of the binary encoding of W

Algorithms that are polynomial with respect


to the numeric value but exponential in the
length of the input are called pseudo-
polynomial.

Analysis and Design of Algorithms 66


Recap (Knapsack)
•  Optimal substructure and recursive solution
in terms of set of items from 1 to i
•  Θ(nW) subproblems, dynamic programming
with runtime Θ(nW)
•  Pseudo-polynomial time algorithm for NP-
hard problem.

Analysis and Design of Algorithms 67

You might also like