Title: Dynamic Programming
1?? ???? ????????????
2Dynamic Programming
- ? divide-and-conquer ???, ????????????.
- ? divide-and-conquer????????????????, ?????.
3?????? (Rod cutting problem)
??????N(??)?????, ?i??????,??pi?????i??????????.
???????????????????
?
?? i 1 2 3 4 5 6 7 8 9 10
??pi 1 5 8 9 10 17 17 20 24 30
N7, 734???17 716223 ???18.
4?????? (Rod cutting problem)
- ??????????????k?,?
- Ni1i2ik
- rNpi1pik ----???
- rN max i1..n pirN-i,
- r00.
- CUT-ROD(p, n)
- if n0 return 0
- q-99999999
- for i1 to n
- q max(q, piCUT-ROD(p, n - i))
- return q
5?????? (Rod cutting problem)
- Memoized-CUT-ROD(p, n)
- let r0,..n be a new array
- for i0 to n
- ri -9999999
- return Memoized-CUT-ROD-AUX(p, n, r)
Time O(n2), why?
- Memoized-CUT-ROD-AUX(p, n, r)
- if rngt0 return rn
- if n0 q0
- else q-9999999
- for i0 to n
- q max(q, piMemoized-CUT-ROD-AUX(p,
n-i, r)) - rn q
- return q
6- BOTTOM-UP-CUT-ROD(p, n)
- let r0,..n be a new array
- r00
- for j1 to n
- q-9999999
- for i 1 to j
- qmax(q, pi rj-i)
- rjq
- return rn
- EXTENDED-BOTTOM-UP-CUT-ROD(p, n)
- let r0,..n and s0..n be a new arrays
- r00
- for j1 to n
- q-9999999
- for i 1 to j
- if q lt pi rj-i)
- q pi rj-i)
- sji
- rjq
- return r and s
- PRINT-CUT-ROD-SOLUTION(p, n)
- (r, s) EXTENDED-BOTTOM-UP-CUT-ROD(p, n)
- while n gt 0
- print sn nn - sn
?? i 1 2 3 4 5 6 7 8 9 10
??pi 1 5 8 9 10 17 17 20 24 30
ri 1 5 8 10 13 17 18 22 25 30
si 1 2 3 2 2 6 1 2 3 10
7Crazy eights puzzle
- Given a sequence of cards c0, c1,,cn-1,
e.g. 7H, 6H, 7D, 3D, 8C, JS,.. - Find the longest subsequence ci1, ci2,,
cik, (i1lt i2ltlt ik), where cij and cij1
have the same suit or rank or one has rank 8.--
match
- Let Ti be the length of the longest subsequence
- starting at ci.
- Ti 1 max Tj ci and cj have a match
and j gtn - Optimal solution max Ti.
8?????? (??)
??????? ?A1, A2, , An?, ???? Ai ???? pi?1? pi,
???? A1A2An ?????, ???? scalar ?????????.
9??????(?)
(A1(A2(A3A4)))? A1?(A2A3A4) ? A2?(A3A4) ? A3?A4
cost 13534 58934
89334 2210 15130
9078 26418
(A1(A2(A3A4))), costs 26418 (A1((A2A3)A4)),
costs 4055 ((A1A2 )( A3A4)), costs 54201
((A1(A2 A3))A4), costs 2856 ((( A1A2)A3)A4),
costs 10582
A1 ? A2 ? A3 ? A4 13 5 89 3 34
10Catalan Number
For any n, ways to fully parenthesize the
product of a chain of n1 matrices binary
trees with n nodes. permutations generated
from 1 2 n through a stack. n pairs of
fully matched parentheses. n-th Catalan Number
C(2n, n)/(n 1) ?(4n/n3/2)
11???
A1 ? A2 ? A3 ? A4
(A1(A2(A3A4))) (A1((A2A3)A4)) ((A1A2 )(
A3A4)) ((A1(A2 A3))A4) ((( A1A2)A3)A4)
12??????(??1)
- If T is an optimal solution for A1, A2, , An
T
k
T2
T1
1, , k
k1, , n
- then, T1 (resp. T2) is an optimal solution for
A1, A2, , Ak (resp. Ak1, Ak2, , An).
13?????? (??2)
- Let mi, j be the minmum number of scalar
multiplications needed to compute the product
AiAj , for 1 ? i ? j ? n. - If the optimal solution splits the product AiAj
(AiAk)?(Ak1Aj), for some k, i ? k lt j, then - mi, j mi, k mk1, j pi?1 pk pj .
Hence, we have
mi, j mini ? k lt jmi, k mk1, j pi?1
pk pj 0 if i j
14ltP0, P1,, Pngt
- MATRIX-CHAIN-ORDER(P)
- n p.length -1
- let m1..n, 1..n and s1..n-1, 2..n be new
tables - for i 1 to n mi, i0
- for l 2 to n
- for i 1 to n l 1
- ji l- 1
- mi, j ?
- for k i to j-1
- q mi, k mk1, j Pi-1PkPj
- if qltmi, j
- mi, j q si, j k
-
- return m and s
- Time O(n3)
15(No Transcript)
16?????? (??)
- Consider an example with sequence of dimensions
lt5,2,3,4,6,7,8gt
mi, j mini ? k lt jmi, k mk1, j pi?1
pk pj
1 2 3 4 5 6 1 0 30 64 132 226 348
2 0 24 72 156 268 3 0
72 198 366 4 0 168 392 5
0 336 6 0
17- Constructing an optimal solution
- Each entry si, jk records that the optimal
parenthesization of AiAi1Aj splits the product
between Ak and Ak1 - Ai..j?(A i..si..j )(A si..j1..j)
18?????? (??)
mi, j mini ? k lt jmi, k mk1, j pi?1
pk pj si, j a value of k that gives the
minimum
s 1 2 3 4 5 6 1 1 1 1 1 1 2 2 3 4 5
3 3 4 5 4 4 5 5
5
1,6
A1(((( A2A3)A4)A5) A6)
19?????? (??)
mi, j mini ? k lt jmi, k mk1, j pi?1
pk pj
- To fill the entry mi, j, it needs ?(j?i)
operations. Hence the execution time of the
algorithm is
Time ?(n3) Space ?(n2)
20- MEMOIZED-MATRIX-CHAIN(P)
- n p.length -1
- let m1..n, 1..n be a new table
- for i 1 to n
- for j i to n
- mi, j ?
- return LOOKUP-CHAIN(m, p, 1, n)
ltP0, P1,, Pngt
- Lookup-Chain(m, P, i, j)
- if mi, j lt ? return mi, j
- if ij mi, j 0
- else for ki to j-1
- q Lookup-Chain(m, P, i, k)
- Lookup-Chain(m, P, k1,
j) Pi-1PkPj - if q lt mi, j mi, j q
- return mi, j time O(n3) space
?(n2)
21??? DP ??????
- Characterize the structure of an optimal
solution. - Derive a recursive formula for computing the
values of optimal solutions. - Compute the value of an optimal solution in a
bottom-up fashion (top-down is also applicable). - Construct an optimal solution in a top-down
fashion.
22Elements of Dynamic Programming
- Optimal substructure (a problem exhibits optimal
substructure if an optimal solution to the
problem contains within it optimal solutions to
subproblems) - Overlapping subproblems
- Reconstructing an optimal solution
- Memoization
23- Given a directed graph G(V, E) and vertices u, v
?V - Unweighted shortest path Find a path from u to v
consisting the fewest edges. Such a path must be
simple (no cycle). - Optimal substructure? YES.
-
- Unweighted longest simple path Find a simple
path from u to v consisting the most edges. - Optimal substructure? NO.
- q ? r ? t is longest but q ? r
- is not the longest between q and r.
w
v
u
q
r
s
t
24Printing neatly ??
- Given a sequence of n words of lengths l1,
l2,,ln, measured in characters, want to
print it neatly on a number of lines, of which
each has at most M characters. . - If a line has words i through j, iltj, and there
is exactly one space between words, the cube of
number of extra space characters at the end of
the line is - Bi, j (M j i - (lili1lj))3.
- Want to minimize the sum over all lines (except
the last line) of the cubes of the number of
extra space at the ends of lines. - Let ci denote the minimum cost for printing
words i through n. - ci min iltjltip (cj1 Bi,j), where p is
the maximum number of words starting from i-th
word that can be fitted into a line.
25Longest Common Subsequence (??)
Given two sequences X ltx1, x2, , xmgt and Y
lty1, y2, , yngt find a maximum-length common
subsequence of X and Y.
?1Input ABCBDAB BDCABA C.S.s
AB, ABA, BCB, BCAB, BCBA Longest BCAB, BCBA,
Length 4
? 2 vintner writers
26Step 1 Characterize longest common subsequence
- Let Z lt z1, z2, , zkgt be a LCS of X ltx1,
x2, , xmgt and Y lty1, y2, , yngt. - If xm yn, then zk xm yn and ltz1, z2, ,
zk?1gt is a LCS of ltx1, x2, , xm?1gt and lty1, y2,
, yn?1gt. - If zk ? xm, then Z is a LCS of ltx1, x2, , xm?1gt
and Y. - If zk ? yn, then Z is a LCS of X and lty1, y2, ,
yn?1gt.
27Step 2 A recursive solution
- Let Ci, j be the length of an LCS of the
prefixes Xi ltx1, x2, , xigt and Yj lty1, y2,
, yjgt, for 1 ? i ? m and 1 ? j ? n. We have
Ci, j 0 if i 0, or j 0 Ci?1, j?1
1 if i , j gt 0 and xi yj max(Ci, j?1,
Ci?1, j) if i , j gt 0 and xi ? yj
28Step 3 Computing the length of an LCS
- LCS-Length(X,Y)
- m X.length
- n Y.length
- let b1..m, 1..n and c0..m, 0..n be new
tables - for i 1 to m ci, 0 0
- for j 1 to n do c0, j 0
- for i 1 to m do
- for j 1 to n do
- if xi yj
- ci,j ci-1,j-11 bi,j ?
- else if ci-1, j ? ci, j-1
- ci, j ci-1, j bi, j ?
- else ci, j ci, j-1 bi, j
? -
- return b, c
29Step 4 Constructing an LCS
- Print-LCS(b, X, i, j)
- if i0 or j0 return
- if bi,j ?
- Print-LCS(b, X, i-1, j-1)
- print xi
-
- else if bi,j ?
- Print-LCS(b, X, i-1, j)
- else Print-LCS(b, X, i, j-1)
30Longest Common Subsequence (???)
Ci, j 0 if i 0, or j 0 Ci?1, j?1
1 if i , j gt 0 and xi yj max(Ci, j?1,
Ci?1, j) if i , j gt 0 and xi ? yj
???LCS BCBA
31Optimal Polygon Triangulation
v0
v6
v1
v5
v2
v3
v4
Find a triangulation s.t. the sum of the weights
of the triangles in the triangulation is
minimized.
32Optimal Polygon Triangulation (??1)
- If T is an optimal solution for ?v0, v1, , vn?
- then, T1 (resp. T2) is an optimal solution for
?v0, v1, , vk? (resp. ?vk, vk1, , vn?), 1 ?
k lt n.
33Optimal Polygon Triangulation (??2)
- Let ti, j be the weight of an optimal
triangulation of the polygon ?vi?1, vi,, vj?,
for 1 ? i lt j ? n. - If the triangulation splits the polygon into
?vi?1, vi,, vk? and ?vk, vk1, ,vn? for some
k, then - ti, j ti, k tk1, j w(?vi?1 vk vj)
. Hence, we have
ti, j mini ? k lt jti, k tk1, j
w(?vi?1 vk vj) 0 if i j
34Optimal binary search trees
- k(k1,k2,,kn) of n distinct keys in sorted order
(k1ltk2ltlt kn) - Each key ki has a probability pi that a search
will be for ki - Some searches may fail, so we also have n1 dummy
keys d0,d1,,dn, where d0ltk1, knltdn and
kiltdiltki1 for i1,,n-1. - For each dummy key di, we have a probability qi
that a search will correspond to di .
35Optimal binary search trees??
i 0 1 2 3 4 5
pi 0.15 0.1 0.05 0.1 0.2
qi 0.05 0.1 0.05 0.05 0.05 0.1
Expected search cost 2.8
36- Goal Given a set of probabilities, want to
construct a binary search tree whose expected
search cost is smallest . - Step 1 The structure of an optimal BST.
- Consider any subtree of a BST, which must
contain contiguous keys ki,,kj for some 1?i?j?n
and must also have as its leaves the dummy keys
di-1,,dj . -
-
- Optimal-BST has the property of optimal
substructure.
37- Step 2 A recursive solution
- Find an optimal BST for the keys ki,,kj, where
j?i-1, i?1, j?n. (When ji-1, there is no actual
key, but di-1) - Define ei,j as the expected cost of searching
an optimal BST containing ki,,kj. Want to find
e1,n. - For dummy key di-1, ei,i-1qi-1 .
- For j?i, need to select a root kr among ki,,kj .
- For a subtree with keys ki,,kj, denote
38- ei,jpr(ei,r-1w(i,r-1))(er1,jw(r1,j))
- ei,r-1er1,jw(i,j)
- Thus
-
- ei,j
- Step 3 Computing the expected search cost of an
optimal BST - Use a table w1..n1,0..n for w(i,j)s .
- wi,i-1qi-1
- wi,jwi,j-1pjqj
39- OPTIMAL-BST(p,q,n)
- let e1..n1, 0..n, w1..n1, 0..n and
root1..n, 1..n be new tables - for i1 to n1
- ei,i-1qi-1
- wi,i-1qi-1
- for l1 to n
- for i1 to n-l1
- jil-1
- ei,j?
- wi,jwi,j-1pjqj
- for r i to j
- tei, r-1er1, j wi,
j - if t lt ei,j
- ei,jt
- rooti,j r
- return e and root
-
?(n3)