Vous êtes sur la page 1sur 55

Dynamic Programming

Greed is not always good.


aruloj Chongstitvatana 2301681 Design and Analysis of Algorithm 2
:9ine
Elements of dynamic programming
Example
Matrix-chain multiplication
Memoization
More examples
Longest common subsequence
Shortest path
0/1 knapsack
aruloj Chongstitvatana 2301681 Design and Analysis of Algorithm 3
emen98 of Dynamic Programming
Optimal substructure
An optimal solution to a problem is created
from optimal solutions of subproblems.
Overlapping subproblems
The optimal solution to a subproblem is reused
in solving more than one problem.
The number of subproblems is usually a
polynomial of the input size.
a9rix-chain :9ipica9ion
aruloj Chongstitvatana 2301681 Design and Analysis of Algorithm 5
a9rix Chain :9ipica9ion
Find the order of multiplication for a chain
of matrices which use the minimum number
of multiplications.
Example:
Civen N
1
c N
2
c N
3
c N

6 size (8x1), (1x2),


(2x7), and (7xS), respectively.
N
1
c ((N
2
c N
3
)c N

) takes 88 multiplicati6ns.
(N
1
c (N
2
c N
3
)c ) N

takes 3S0 multiplicati6ns.


Which is the minimum?
aruloj Chongstitvatana 2301681 Design and Analysis of Algorithm 6
%o-ma9rix m:9ipica9ion
Let and be 2 matrices of size 5Lq and
qLr respectively and C = c .
C is of size 5Lr.
q
C
i,j
= %
i,k
L
k,j
k = 1
and are compatible.
aruloj Chongstitvatana 2301681 Design and Analysis of Algorithm 7
o many m:9ipica9ion doe8 i9
9ake 9o m:9ipy 2 ma9rice8 ?
NATR!XNULT!PLY(A, B)
c6lumns A (r6s B (
;59969inc6mpatible dimensi6ns"
9;95
69i =;6r6s A (
1669 =;6c6lumns B (
16 i, (=
69k =;6c6lumns A(
16 i, (= i, (Ai, k(LB k, (
9;95
rows A(c.olumns
(c.olumns A(
aruloj Chongstitvatana 2301681 Design and Analysis of Algorithm 8
o many m:9ipica9ion doe8 i9
9ake 9o m:9ipy a ma9rix chain ?
Let N
1
, N
2
, ., N
n
be matrices hich are c6mpatible.
Let p
i1
L p
i
be the size 6 matrix N
i
.
"ne ay t6 multiply this matrix chain is t6 ch66se s6me k
1 A k < n:
- multiply N
1
c N
2
c . c N
k
,
- multiplying N
k+1
c N
2
c . c N
n
, and
- multiplying (N
1
c N
2
c . c N
k
) c (N
k+1
c N
2
c . c N
n
).
!t takesT (1, k) +T (k +1, n)+p
0
Lp
k
Lp
n
multiplicati6ns,
here
- T (i, ) is the number 6 Ls used in multiplying (N
i
c N
i+1
c . c N

).
aruloj Chongstitvatana 2301681 Design and Analysis of Algorithm 9
M
1
M
2
M
3
M
4
M
6
M
5
M
=
5x10 10 x 3 3 x 8 8 x 8 8 x 2 2 x 6 5 x 6
M
1
M
2
M
3
M
4
M
5
M
6
M
=
5x3 3 x 8 8 x 2 2 x 6 5 x 6
M
1
M
2
M
3
M
4
M
5
M
6
M
=
5x3 3 x 2 2 x 6 5 x 6
M
1
M
2
M
3
M
4
M
5
M
6
M
=
5x2 2 x 6 5 x 6
aruloj Chongstitvatana 2301681 Design and Analysis of Algorithm 10
inim:m n:mber of m:9ipica9ion8
for a ma9rix-chain m:9ipica9ion
T6 ind the minimum number 6 multiplicati6ns in
N
1
c N
2
c . c N
n
.
Try each k, 1 A k < n ,
- ind the min. number 6 L s 6r N
1
c N
2
c . c N
k
,
- ind the min. number 6 Ls 6r N
k+1
cN
2
c . cN
n
,
- ind the number 6 Ls 6r (N
1
c N
2
c . c N
k
) c (N
k+1
c
N
2
c . c N
n
), and
- sum the three am6unts.
h66se k hich yields the smallest number.
aruloj Chongstitvatana 2301681 Design and Analysis of Algorithm 11
inding 9he op9ima ma9rix-chain
m:9ipica9ion
Let T (i, ) be the min. number 6 Ls used in
multiplying (N
i
c N
i+1
c . c N

), here i < .
1
- T (i, ) = min (T (i, k) + T (k +1, )+ p
i 1
L p
k
L p

)
k=i
T (i, ) = 0 i i = .
Optimal substructure
Overlapping subproblem:
T(23) is used in finding both T(13) and T(2).
aruloj Chongstitvatana 2301681 Design and Analysis of Algorithm 12
gori9hm: Rec:r8ive
NATR!XA!N"RDER(p, i,
minc6st =
69 k = i ;6 1
16c6st= NATR!XA!N"RDER(p, i, k+
NATR!XA!N"RDER(p, k +1, +
p |i 1]Lp |k ]Lp | ]
c6st < minc6st
;5 minc6st = c6st
9;95minc6st
aruloj Chongstitvatana 2301681 Design and Analysis of Algorithm 13
gori9hm: paren9he8i8e
Cl6bal: s |n, n] st6ring the parenthesis p6int
s |i, ] is the p6int that yields min. Ls 6r N
i
c N
i+1
c . c N

NATR!XA!N"RDER(p, i,
minc6st =
69 k = 1 ;6 1
16 c6st= NATR!XA!N"RDER(p, i, k+
NATR!XA!N"RDER(p, k +1, +
p |i 1] L p |k ] L p | ]
c6st < minc6st
;5 minc6st = c6st
s |i, ] = k
9;95minc6st
aruloj Chongstitvatana 2301681 Design and Analysis of Algorithm 1
gori9hm: 9era9ive
NATR!XA!N"RDER(p
n = length p] -
69i =;6n
16m i, i ( =
69l = ;6n > l is the chain length
1669i =;6n - l
16 = i l -
m i, (=
69k = i ;6 -
168 = m i, k(m k , (p |i-]p |k] p |]
8 < m i, (
;5 m i, (=8
s i, (=k >split p6int
9;95m and s
aruloj Chongstitvatana 2301681 Design and Analysis of Algorithm 15
9era9ion
1 2 3 5 6
1 0 16
2 0 14
3 0 70
0 105
5 0 60
6 0
0 8
1 1
2 2
3 7
5
5 3
6
aruloj Chongstitvatana 2301681 Design and Analysis of Algorithm 16
9era9ion
1 2 3 5 6
1 0 16 72
2 0 14 49
3 0 70 100
0 105 189
5 0 60
6 0
0 8
1 1
2 2
3 7
5
5 3
6
min(0+16+(8x1x7), (0+14+8x2x7))
min(0+14+(1x7x5), (0+70+1x2x5))
min(0+70+(2x5x3), (0+105+2x7x3))
min(0+60+(7x5x4), (0+105+7x3x4))
aruloj Chongstitvatana 2301681 Design and Analysis of Algorithm 17
9era9ion
1 2 3 5 6
1 0 16 72 89
2 0 14 49 64
3 0 70 100 124
0 105 189
5 0 60
6 0
0 8
1 1
2 2
3 7
5
5 3
6
min(0+49+(8x1x5), (16+70+8x2x5), (72+0+8x7x5))
min(0+100+(1x2x3), (14+105+1x7x3), (49+0+1x5x3))
min(0+189+(2x7x4), (70+60+2x5x4), (100+0+2x3x4))
aruloj Chongstitvatana 2301681 Design and Analysis of Algorithm 18
9era9ion
1 2 3 5 6
1 0 16 72 89 88
2 0 14 49 64 76
3 0 70 100 124
0 105 189
5 0 60
6 0
0 8
1 1
2 2
3 7
5
5 3
6
min(0+64+(8x1x3), (16+100+8x2x3) ), (72+105+8x7x3), (89+0+8x5x3))
min(0+124+(1x2x4), (14+189+1x7x4), (49+60+1x5x4), (64+0+1x3x4))
aruloj Chongstitvatana 2301681 Design and Analysis of Algorithm 19
9era9ion
1 2 3 5 6
1 0 16 72 89 88 108
2 0 14 49 64 76
3 0 70 100 124
0 105 189
5 0 60
6 0
0 8
1 1
2 2
3 7
5
5 3
6
min(0+76+(8x1x4), (16+124+8x2x4) ), (72+189+8x7x4 ), (89+60+8x5x4), (88+0+8x3x4))
aruloj Chongstitvatana 2301681 Design and Analysis of Algorithm 20
NATR!XA!N"RDER(p
n = length p] -
69i =;6n
16m i, i ( =
69l = ;6n > l is the chain length
1669i =;6n - l
16 = i l -
m i, (=
69k = i ;6 -
168 = m i, k(m k , (p |i-]p |k] p |]
8 < m i, (
;5 m i, (=8
s i, (=k >split p6int
9;95m and s
n
%c(n-l1)(l-1) O(n
2
)
l2
n-l-1
% c(f-i)
i1
c(n-l1)(l-1)
Compexi9y: 9era9ive
f-1
%cc(f-i)
ki
c
c
aruloj Chongstitvatana 2301681 Design and Analysis of Algorithm 21
emoiza9ion
NOT memo7ization
Use for recursive algorithm
Store known optimal solution in a table to
avoid repeatedly solve the same problem.
mprove efficiency
aruloj Chongstitvatana 2301681 Design and Analysis of Algorithm 22
gori9hm: emoize
Cl6bal: m |n, n]
NEN"!ZENTXNp
n = length (p)1
69 i = 1 ;6n
16 69 = 1 ;6 n
16m |i, ]=
9;95L""UP (p, 1, n)
L""UPp, i,
m|i, ] <
;5 9;95m |i, ]
i =
;5m |i, ] = 0
3: 69 k = 1 ;6 1
16 8= L""UPp, i, k
+ L""UPp, k +1,
+ p |i 1] Lp |k ]L p | ]
8 < m |i, ]
;5 m |i, ] = 8
9;95m |i, ]
onge89 Common 8:b8eq:ence8
aruloj Chongstitvatana 2301681 Design and Analysis of Algorithm 2
Common 8:b8eq:ence8
Given sequences X = (x
1
x
2
. x
m
) and Y = (
1

2
.
3
) .
= (
1

2
.
k
) is a 8:-80q:03.0 of X if there
is a strictly increasing sequence i
1
i
2
. i
k
of X
such that x
i
=
j
for all 1A j A k.
(A B D A ) is a subsequence of (C A B B C C D
A B A ) (sequence: 2 37 8) .
is a .4mm43 8:-80q:03.0 of X and Y if is a
subsequence of X and Y.
(A B D A ) is a common subsequence of (C A B B
C C D A B A ) and (A D C B C D C A B ).
aruloj Chongstitvatana 2301681 Design and Analysis of Algorithm 25
onge89-common-8:b8eq:ence
Probem
Given sequences X = (x
1
x
2
. x
m
) and Y = (
1

2

.
3
) .
Find a common subsequence of X and Y
which has maximum length.
( A B C D A B )
is a longest common subsequence (LCS) of
( C A B B C C D A B A ) and
( A D C B C D C A B ).
aruloj Chongstitvatana 2301681 Design and Analysis of Algorithm 26
p9ima 8:b89r:c9:re8
Let X (x
1
, x
2
, ..., x
m
) and
Y (
1

3
)be sequences and
(
1

k
)be an LCS of X and Y
f x
m
=
3
then

k
x
m
=
3
and
k-1
is an LCS of X
m-1
and Y
3-1

f x
m
=
3
then

k
=x
m
implies that is an LCS of X
m-1
and Y
f x
m
=
3
then

k
=
3
implies that is an LCS of X and Y
3-1

aruloj Chongstitvatana 2301681 Design and Analysis of Algorithm 27


Rec:r8ive 8o:9ion
Let X (x
1
, x
2
, . . . , x
m
) and Y (
1

3
)
be sequences and LCS(i j) denote the longest
common subsequence of (x
1
, x
2
, . . . , x
i
) and (
1

j
)
if x
m
=
3
LCS(m 3) = LCS(m-13-1)+ x
m

if x
m
=
3
LCS(m 3) = best(LCS(m3-1)

LCS(m-13))
aruloj Chongstitvatana 2301681 Design and Analysis of Algorithm 28
gori9hm: Rec:r8ive
LCS(X Y m 3)
if m A 0 47 3 A 0
90n 709:7n(c)
080 if X m( = Y 3(
90n 709:7n LCS(X Y m-1 3-1) || X m(
080 5 = LCS(X Y m 3-1)
q = LCS(X Y m-1 3)
if length(5)>length(q)
90n 709:7n 5
080 709:7n q
aruloj Chongstitvatana 2301681 Design and Analysis of Algorithm 29
gori9hm: emoize
M-LCS(X Y m 3)
if m A 0 47 3 A 0
90n 709:7n(c)
080 if lcsm 3( = undefined
90n if X m( = Y 3( > never been calculated before
90n lcsm 3(=
M-LCS(X Y m-1 3-1) || X m(
709:7n lcsm 3(
080 5 = M-LCS(X Y m 3-1)
q = M-LCS(X Y m-1 3)
if length(5)>length(q)
90n lcsm 3(= 5
080 lcsm 3(= q
709:7n lcsm 3(
aruloj Chongstitvatana 2301681 Design and Analysis of Algorithm 30
9era9ion
C A B B C C D A B A
A
D
C
B
C
D
C
A
B
aruloj Chongstitvatana 2301681 Design and Analysis of Algorithm 31
9era9ion
C A B B C C D A B A
A c A A A A A A A A A
D
C
B
C
D
C
A
B
aruloj Chongstitvatana 2301681 Design and Analysis of Algorithm 32
9era9ion
C A B B C C D A B A
A c A A A A A A A A A
D c A A A A A AD AD AD AD
C
B
C
D
C
A
B
aruloj Chongstitvatana 2301681 Design and Analysis of Algorithm 33
9era9ion
C A B B C C D A B A
A c A A A A A A A A A
D c A A A A A AD AD AD AD
C
C A A A AC AC AC AC AC AC
B
C
D
C
A
B
aruloj Chongstitvatana 2301681 Design and Analysis of Algorithm 3
9era9ion
C A B B C C D A B A
A c A A A A A A A A A
D c A A A A A AD AD AD AD
C
C A A A AC AC AC AC AC AC
B C A AB AB AB AB AB AB ACB ACB
C
C A AB AB ABC ABC ABC ABC ABC ABC
D C A AB AB ABC ABC
ABCD ABCD ABCD ABCD
C
C A AB AB ABC ABCC ABCD ABCD ABCD ABCD
A C CA AB AB ABC
ABCC ABCD ABCDA ABCDA ABCDA
B
C CA CAB AB ABC ABCC ABCD ABCDA ABCDAB ABCDAB
aruloj Chongstitvatana 2301681 Design and Analysis of Algorithm 35
gori9hm: 9era9ion
!TL&(X, Y )
m = length X (
n = length Y (
69i = 0 ;6m 16lcs i , 0( = c
69 = 0 ;6n 16lcs 0, ( = c
69i = 1 ;6m
1669 = 1 ;6n
16x |i ] = y | ]
;5lcs i, ( = lcs i-1, -1( ]] x |i ]
3:length(lcs i - 1, () > length(lcs i, - 1()
;5lcs i, ( = lcs i -1, (
3: lcs i, ( = lcs i, - 1(
9;95lcs m, n(
O(nc m)
8hor9e89 Pa9h8
aruloj Chongstitvatana 2301681 Design and Analysis of Algorithm 37
Probem
Given a graph G (J, E), and two nodes p and q in J,
Iind the shortest path between p and q.
Assume that a graph is represented as an adjacency
matrix.
0 -1 2

0 -6 2


0 5 8

0 1

2

0
1
2
3

5
2
-1
1
8
-6
5 2
2
aruloj Chongstitvatana 2301681 Design and Analysis of Algorithm 38
xampe
1
2
3

5
2
-1
1
8
-6
5 2
2
10
8 2 1
aruloj Chongstitvatana 2301681 Design and Analysis of Algorithm 39
8hor9e89 Pa9h:
p9ima 8:b89r:c9:re
et G be a graph,

if
be the length oI edge (i, f), where 1A i, f An,
and
/
(k)
if
be the length oI the shortest path between
nodes i and f, Ior 1 A i, f, k A n, without passing
through any nodes numbered greater than k.
(k)

if
iI k 0
/
if
,
(k-1) (k-1) (k-1)
' min(/
if
, /
ik
/
kf
) iI k > 1
aruloj Chongstitvatana 2301681 Design and Analysis of Algorithm 0
Proof
Prove by induction.
Basis: et k0.
/
(k)
if
/
0
if
is the length oI the shortest path
between nodes i and f, without passing through
any node. That is, /
0
if

if
.
et m be any integer such that 0mA n.
Induction hypothesis: For k m,
(k)

if
iI k 0
/
if
,
(k-1) (k-1) (k-1)
' min(/
if
, /
ik
/
kf
) iI k > 1.
aruloj Chongstitvatana 2301681 Design and Analysis of Algorithm 1
Proof
Induction step: Prove that
(m) (m-1) (m-1) (m-1)
/
if
min(/
if
, /
im
/
mf
).
i
1
.
m-1
2
f
m
iI the shortest path does not pass through any node
numbered greater than m-1
aruloj Chongstitvatana 2301681 Design and Analysis of Algorithm 2
Proof
Induction step:
i
1
.
m-1
2
f
m
iI the shortest path passes through node m,
but not any node number greater than m.
aruloj Chongstitvatana 2301681 Design and Analysis of Algorithm 3
Rec:rrence
(k)

if
iI k 0
/
if
,
(k-1) (k-1) (k-1)
' min(/
if
, /
ik
/
kf
) iI k > 1
aruloj Chongstitvatana 2301681 Design and Analysis of Algorithm
gori9hm: Rec:r8ion
STP (i j, k)
if i = j d i, j, k (=0
if ( d i, j k( is undefined ,nd k=0 )
d4 d i j k( = STP(i j k-1)
d1 = STP(i, k, k-1)
d2 = STP(k, j , k-1)
if (d i, j ,k( > d1+d2 )
90n d i, j k( = d1+ d2
709:7n d i, j k(
aruloj Chongstitvatana 2301681 Design and Analysis of Algorithm 5
gori9hm: 9era9ion
FLOYD-WARSHALL (W)
3 = r48(
d-- 0( =
f47 k = 1 94 3
d4 f47 i = 1 94 3
d4 f47 j = 1 94 3
d4 d i, j, k( = d i, j, k-1(
if d i, j, k( > d i, k, k-1( + d k, j, k-1(
d ijk( = d i, k, k-1( + d k, j, k-1(
709:7n d
aruloj Chongstitvatana 2301681 Design and Analysis of Algorithm 6
gori9hm: red:ced 89orage
FLOYD-WARSHALL (W)
3 = r48(
d =
f47 k = 1 94 3
d4 f47 i = 1 94 3
d4 f47 j = 1 94 3
d4 d i, j( = min (d i, j( d i, k( + d k, j()
709:7n d
aruloj Chongstitvatana 2301681 Design and Analysis of Algorithm 7
f47 k = 1 94 3
f47 i = 1 94 3
f47 j = 1 94 3
d i, j( = min (d i, j(
d i, k( + d k, j()
8ampe R:n
1 2 3 5
1 0 -1 2

2

0 -6 2

3

0 5 8


0 1
5

2

0
1
2
3

5
2
-1
1
8
-6
5 2
2
-7 1
-
-2 1
3
2
1
-1
6 8
Knap8ack
aruloj Chongstitvatana 2301681 Design and Analysis of Algorithm 9
Probem
Given a knapsack with capacity , and a set oI
objects i, 1 A i A n, such that object i has value
v
i
and weight
i
.
Find the maximum value oI objects which can
be Iitted in the knapsack.
1
W:4
V:4
2
W:5
V:3
3
W:5
V:7
5
W:7
V:8
4
W:2
V:3
napsack
Capacity: 13
1
W:4
V:4
5
W:7
V:8
4
W:2
V:3
1
W:4
V:4
3
W:5
V:7
4
W:2
V:3
aruloj Chongstitvatana 2301681 Design and Analysis of Algorithm 50
Knap8ack:
p9ima 8:b89r:c9:re
et m|k, s| be the maximum value oI objects i,
Ior 1Ai Ak, that can be Iitted in a knapsack with
capacity s.
m|k, 0| 0.
m|0, s| 0.
II k,s = 0:
m|k, s| max(m|k-1, s|, m|k-1, s-
k
|v
k
) iI s >
k
.
m|k, s| m|k-1, s| iI s
k
.
aruloj Chongstitvatana 2301681 Design and Analysis of Algorithm 51
Rec:rrence
et m|k, s| be the maximum value oI objects i,
Ior 1Ai Ak, that can be Iitted in a knapsack with
capacity s.
m|k, 0| 0.
m|0, s| 0.
II k,s = 0:
m|k, s| max(m|k-1, s|, m|k-1, s-
k
|v
k
) iI s >
k
.
m|k, s| m|k-1, s| iI s
k
.
aruloj Chongstitvatana 2301681 Design and Analysis of Algorithm 52
gori9hm: rec:r8ive
Globals:
; i ( and i ( store values and weight of object i .
mk(8( stores the maximum values of objects that can be
stored in a knapsack of size 8.
sack(k 8)
if k=0 47 8=0 90n 709:7n 0
if mk 8( is undefined
90n if 8 > k(
90n mk 8( =
max(sack(k-18) sack(k-1 8-k()+;k()
080 mk 8( = sack(k-18)
709:7n mk 8(
aruloj Chongstitvatana 2301681 Design and Analysis of Algorithm 53
gori9hm: 9era9ive
Let 3 be the number of objects
be the capacity of the knapsack and
mi j ( be the maximum value of objects 1.i which can
be fitted in a knapsack of capacity j.
f47 j =1 94
d4 m0 j (=0
f47 i =1 94 3
d4 mi 0(=0
f47 j =1 94
d4 if j > i (
90n mi j ( = max(mi-1j ( mi-1 j i ( ( + ; i ()
080 mi j ( = mi-1j (
aruloj Chongstitvatana 2301681 Design and Analysis of Algorithm 5
9era9ion:
8ampe R:n
0 1 2 3 5
0 0 0 0 0 0 0
1 0 0 0 0 0 0
2 0 0 0 0 3 3
3 0 0 0 0 3 3
0
5 0 7 7 7
6 0 7 7 7
7 0 7 10 10
8 0 7 10 10
9 0 7 11 11 11
10 0 7 11 11 11
11 0 7 11 1 1
12 0 7 11 1 15
13 0 7 11 1 15
1
W:4
V:4
2
W:5
V:3
3
W:5
V:7
5
W:7
V:8
4
W:2
V:3
f47 j =1 94
d4 m0 j (=0
f47 i =1 94 3
d4 mi 0(=0
f47 j =1 94
d4 if j > i (
90n mi j ( = max(mi-1j (
mi-1 j -i ( ( + ;i ()
080 mi j ( = mi-1j (
aruloj Chongstitvatana 2301681 Design and Analysis of Algorithm 55
sack(k 8)
if k=0 47 8=0 90n 709:7n 0
if mk 8( is undefined
90n if 8 > k(
90n mk 8( =
max(sack(k-18)
sack(k-1 8- k()+; k()
080 mk 8( = sack(k-18)
709:7n mk 8(
emoize:
8ampe R:n
0 1 2 3 5
0 0 0 0 0 0 0
1 0 0 0
2 0
3 0 0
0
5 0
6 0 7 7
7 0
8 0
9 0
10 0
11 0 7 11
12 0
13 0 7 11 1 15
1
W:4
V:4
2
W:5
V:3
3
W:5
V:7
5
W:7
V:8
4
W:2
V:3

Vous aimerez peut-être aussi