Dynamic Programming - SKKUmodsim.skku.ac.kr/bbs/Course/Data/AL/LectureNote/alg... · 2006. 5....
Transcript of Dynamic Programming - SKKUmodsim.skku.ac.kr/bbs/Course/Data/AL/LectureNote/alg... · 2006. 5....
Dynamic Programming
Introduction to Algorithms October 25, 2004 L12.2© 2001–4 by Charles E. Leiserson
Dynamic programmingDesign technique, like divide-and-conquer.
Example: Longest Common Subsequence (LCS)• Given two sequences x[1 . . m] and y[1 . . n], find
a longest subsequence common to them both.
Introduction to Algorithms October 25, 2004 L12.3© 2001–4 by Charles E. Leiserson
Dynamic programmingDesign technique, like divide-and-conquer.
Example: Longest Common Subsequence (LCS)• Given two sequences x[1 . . m] and y[1 . . n], find
a longest subsequence common to them both.“a” not “the”
Introduction to Algorithms October 25, 2004 L12.4© 2001–4 by Charles E. Leiserson
Dynamic programmingDesign technique, like divide-and-conquer.
Example: Longest Common Subsequence (LCS)• Given two sequences x[1 . . m] and y[1 . . n], find
a longest subsequence common to them both.
x: A B C B D A B
y: B D C A B A
“a” not “the”
Introduction to Algorithms October 25, 2004 L12.5© 2001–4 by Charles E. Leiserson
Dynamic programmingDesign technique, like divide-and-conquer.
Example: Longest Common Subsequence (LCS)• Given two sequences x[1 . . m] and y[1 . . n], find
a longest subsequence common to them both.
x: A B C B D A B
y: B D C A B A
“a” not “the”
BCBA = LCS(x, y)
functional notation, but not a function
Introduction to Algorithms October 25, 2004 L12.6© 2001–4 by Charles E. Leiserson
Brute-force LCS algorithm
Check every subsequence of x[1 . . m] to see if it is also a subsequence of y[1 . . n].
Introduction to Algorithms October 25, 2004 L12.7© 2001–4 by Charles E. Leiserson
Brute-force LCS algorithm
Check every subsequence of x[1 . . m] to see if it is also a subsequence of y[1 . . n].
Analysis• Checking = O(n) time per subsequence.• 2m subsequences of x (each bit-vector of
length m determines a distinct subsequence of x).
Worst-case running time = O(n2m)= exponential time.
Introduction to Algorithms October 25, 2004 L12.8© 2001–4 by Charles E. Leiserson
Towards a better algorithmSimplification:1. Look at the length of a longest-common
subsequence. 2. Extend the algorithm to find the LCS itself.
Introduction to Algorithms October 25, 2004 L12.9© 2001–4 by Charles E. Leiserson
Towards a better algorithmSimplification:1. Look at the length of a longest-common
subsequence. 2. Extend the algorithm to find the LCS itself.
Notation: Denote the length of a sequence sby | s |.
Introduction to Algorithms October 25, 2004 L12.10© 2001–4 by Charles E. Leiserson
Towards a better algorithmSimplification:1. Look at the length of a longest-common
subsequence. 2. Extend the algorithm to find the LCS itself.
Strategy: Consider prefixes of x and y.• Define c[i, j] = | LCS(x[1 . . i], y[1 . . j]) |.• Then, c[m, n] = | LCS(x, y) |.
Notation: Denote the length of a sequence sby | s |.
Introduction to Algorithms October 25, 2004 L12.11© 2001–4 by Charles E. Leiserson
Recursive formulationTheorem.
c[i, j] =c[i–1, j–1] + 1 if x[i] = y[j],max{c[i–1, j], c[i, j–1]} otherwise.
Introduction to Algorithms October 25, 2004 L12.12© 2001–4 by Charles E. Leiserson
Recursive formulationTheorem.
c[i, j] =c[i–1, j–1] + 1 if x[i] = y[j],max{c[i–1, j], c[i, j–1]} otherwise.
Proof. Case x[i] = y[ j]:
L1 2 i m
L1 2 j n
x:
y:=
Introduction to Algorithms October 25, 2004 L12.13© 2001–4 by Charles E. Leiserson
Recursive formulationTheorem.
c[i, j] =c[i–1, j–1] + 1 if x[i] = y[j],max{c[i–1, j], c[i, j–1]} otherwise.
Let z[1 . . k] = LCS(x[1 . . i], y[1 . . j]), where c[i, j] = k. Then, z[k] = x[i], or else z could be extended. Thus, z[1 . . k–1] is CS of x[1 . . i–1] and y[1 . . j–1].
Proof. Case x[i] = y[ j]:
L1 2 i m
L1 2 j n
x:
y:=
Introduction to Algorithms October 25, 2004 L12.14© 2001–4 by Charles E. Leiserson
Proof (continued)Claim: z[1 . . k–1] = LCS(x[1 . . i–1], y[1 . . j–1]).
Suppose w is a longer CS of x[1 . . i–1] andy[1 . . j–1], that is, |w | > k–1. Then, cut and paste: w || z[k] (w concatenated with z[k]) is a common subsequence of x[1 . . i] and y[1 . . j]with |w || z[k] | > k. Contradiction, proving the claim.
Introduction to Algorithms October 25, 2004 L12.15© 2001–4 by Charles E. Leiserson
Proof (continued)Claim: z[1 . . k–1] = LCS(x[1 . . i–1], y[1 . . j–1]).
Suppose w is a longer CS of x[1 . . i–1] andy[1 . . j–1], that is, |w | > k–1. Then, cut and paste: w || z[k] (w concatenated with z[k]) is a common subsequence of x[1 . . i] and y[1 . . j]with |w || z[k] | > k. Contradiction, proving the claim.
Thus, c[i–1, j–1] = k–1, which implies that c[i, j] = c[i–1, j–1] + 1.Other cases are similar.
Introduction to Algorithms October 25, 2004 L12.16© 2001–4 by Charles E. Leiserson
Dynamic-programming hallmark #1
Optimal substructureAn optimal solution to a problem
(instance) contains optimal solutions to subproblems.
Introduction to Algorithms October 25, 2004 L12.17© 2001–4 by Charles E. Leiserson
Dynamic-programming hallmark #1
Optimal substructureAn optimal solution to a problem
(instance) contains optimal solutions to subproblems.
If z = LCS(x, y), then any prefix of z is an LCS of a prefix of x and a prefix of y.
Introduction to Algorithms October 25, 2004 L12.18© 2001–4 by Charles E. Leiserson
Recursive algorithm for LCS
LCS(x, y, i, j)if x[i] = y[ j]
then c[i, j] ← LCS(x, y, i–1, j–1) + 1else c[i, j] ← max{LCS(x, y, i–1, j),
LCS(x, y, i, j–1)}
Introduction to Algorithms October 25, 2004 L12.19© 2001–4 by Charles E. Leiserson
Recursive algorithm for LCS
LCS(x, y, i, j)if x[i] = y[ j]
then c[i, j] ← LCS(x, y, i–1, j–1) + 1else c[i, j] ← max{LCS(x, y, i–1, j),
LCS(x, y, i, j–1)}Worst-case: x[i] ≠ y[ j], in which case the algorithm evaluates two subproblems, each with only one parameter decremented.
Introduction to Algorithms October 25, 2004 L12.20© 2001–4 by Charles E. Leiserson
Recursion treem = 3, n = 4: 3,43,4
2,42,4
1,41,4
3,33,3
3,23,22,32,3
1,31,3 2,22,2
2,32,3
1,31,3 2,22,2
Introduction to Algorithms October 25, 2004 L12.21© 2001–4 by Charles E. Leiserson
Recursion treem = 3, n = 4: 3,43,4
2,42,4
1,41,4
3,33,3
3,23,22,32,3
1,31,3 2,22,2
Height = m + n ⇒ work potentially exponential.
2,32,3
1,31,3 2,22,2
m+n
Introduction to Algorithms October 25, 2004 L12.22© 2001–4 by Charles E. Leiserson
same subproblem
,but we’re solving subproblems already solved!
Recursion treem = 3, n = 4: 3,43,4
2,42,4
1,41,4
3,33,3
3,23,22,32,3
1,31,3 2,22,2
Height = m + n ⇒ work potentially exponential.
2,32,3
1,31,3 2,22,2
m+n
Introduction to Algorithms October 25, 2004 L12.23© 2001–4 by Charles E. Leiserson
Dynamic-programming hallmark #2
Overlapping subproblemsA recursive solution contains a
“small” number of distinct subproblems repeated many times.
Introduction to Algorithms October 25, 2004 L12.24© 2001–4 by Charles E. Leiserson
Dynamic-programming hallmark #2
Overlapping subproblemsA recursive solution contains a
“small” number of distinct subproblems repeated many times.
The number of distinct LCS subproblems for two strings of lengths m and n is only mn.
Introduction to Algorithms October 25, 2004 L12.25© 2001–4 by Charles E. Leiserson
Memoization algorithmMemoization: After computing a solution to a subproblem, store it in a table. Subsequent calls check the table to avoid redoing work.
Introduction to Algorithms October 25, 2004 L12.26© 2001–4 by Charles E. Leiserson
Memoization algorithmMemoization: After computing a solution to a subproblem, store it in a table. Subsequent calls check the table to avoid redoing work.LCS(x, y, i, j)
if c[i, j] = NILthen if x[i] = y[j]
then c[i, j] ← LCS(x, y, i–1, j–1) + 1else c[i, j] ← max{LCS(x, y, i–1, j),
LCS(x, y, i, j–1)}
same as before
Introduction to Algorithms October 25, 2004 L12.27© 2001–4 by Charles E. Leiserson
Memoization algorithmMemoization: After computing a solution to a subproblem, store it in a table. Subsequent calls check the table to avoid redoing work.
Time = Θ(mn) = constant work per table entry.Space = Θ(mn).
LCS(x, y, i, j)if c[i, j] = NIL
then if x[i] = y[j]then c[i, j] ← LCS(x, y, i–1, j–1) + 1else c[i, j] ← max{LCS(x, y, i–1, j),
LCS(x, y, i, j–1)}
same as before
Introduction to Algorithms October 25, 2004 L12.28© 2001–4 by Charles E. Leiserson
00 00 00 00 00
00 00 11 11 1100 00 00
11 11 11
00 00 11 11 11 22 22D 22
00 00 11 22 22 22 22C 22
00 11 11 22 22 22 33A 33
00 11 22 22 33 33 33B 44
00 11 22 22 33 33
A
Dynamic-programming algorithm
IDEA:Compute the table bottom-up.
A B C B D B
B
A 44 44
Introduction to Algorithms October 25, 2004 L12.29© 2001–4 by Charles E. Leiserson
00 00 00 00 00
00 00 11 11 1100 00 00
11 11 11
00 00 11 11 11 22 22D 22
00 00 11 22 22 22 22C 22
00 11 11 22 22 22 33A 33
00 11 22 22 33 33 33B 44
00 11 22 22 33 33
A
Dynamic-programming algorithm
IDEA:Compute the table bottom-up.
A B C B D B
B
A 44 44
Time = Θ(mn).
Introduction to Algorithms October 25, 2004 L12.30© 2001–4 by Charles E. Leiserson
00 00 00 00 00 00 00 00
00 00 11 11 11 11 11 11
00 00 11 11 11 22 22D 22
00 00 11 22 22 22 22C 22
00 11 11 22 22 22 33A 33
00 11 22 22 33 33 33B 44
00 11 22 22 33 33
A
Dynamic-programming algorithm
IDEA:Compute the table bottom-up.
A B C B D B
B
A 44 44
Time = Θ(mn).Reconstruct LCS by tracing backwards.
0A
4
0B
B1
C
C
2B
B
3
A
A
D1
A2
D
3
B
4
Introduction to Algorithms October 25, 2004 L12.31© 2001–4 by Charles E. Leiserson
00 00 00 00 00 00 00 00
00 00 11 11 11 11 11 11
00 00 11 11 11 22 22D 22
00 00 11 22 22 22 22C 22
00 11 11 22 22 22 33A 33
00 11 22 22 33 33 33B 44
00 11 22 22 33 33
A
Dynamic-programming algorithm
IDEA:Compute the table bottom-up.
A B C B D B
B
A 44 44
Time = Θ(mn).Reconstruct LCS by tracing backwards.
0A
4
0B
B1
C
C
2B
B
3
A
A
D1
A2
D
3
B
4Space = Θ(mn).Exercise:O(min{m, n}).
LCS-LENGTH(X,Y)m ← length[X]n ← length[y]for i ← 1to m
do c[i,0] ← 0for j ←0 to n
do c[0,j] ← 0for ← 1 to m
do for j ← 1 to ndo if x[i] = y[j]
then c[i, j] ← c[i-1, j-1] + 1b[i, j] ← “↖”
else if c[i-1, j]≥c[i, j-1]then c[i, j] ← c[i-1, j]
b[i, j] ← “↑”else c[i, j] ← c[i, j-1]
b[i,j] ← “←”return c and b
Computing the length of an LCS
Constructing an LCS
PRINT-LCS(b, X, i, j)if i = 0 or j = 0
then returnif b[i, j] = “↖”
then PRINT-LCS(b, X, i-1, j-1)print x[i]
elseif b[i, j] = “↑”then PRINT-LCS(b, X, i-1, j)
else PRINT-LCS(b, X, i, j-1)
Assembly-line scheduling
Manufacturing problem to find the fastest way through a factory.
• Produces automobiles in a factory that has two assembly lines.• An automobile chassis enters each assembly line and parts are added to it at the stations. • Each assembly line: n stations, numbered j = 1,2, . . . , n .• Denote jth station on line i (i = 1, 2) by Si,j . • The jth station on line 1 (S1,j) performs the same function as the jth station on line 2
(S2, j).• The time required at each station varies, even between stations at the same position on two different lines.- denote time required at Si,j by ai,j- entry time ei- exit time xi- transfer time at Si,j is ti,j .
chassis enters
Completed autoexits
a1,n−1
a2,n−1assembly
line2
assembly line1
…
station S2,1 station S2,2 station S2,nstation S2,4station S2,3 station S2,n-1
station S1,1 station S1,2 station S1,nstation S,1,4station S1,3 station S1,n-1
a1,na1, 4a1, 2a1, 1 a1, 3
a2, 3 a2, 4 a2, n
t1, 1
t2, 1
t1,n−1
t2,n−1
x 1
x 2
t1, 2
t2, 3t2, 2
t1, 3
e1
e2
a2, 1 a2, 2
Assembly-line scheduling
Brute force way of minimizing the time through the factory.
• There are 2n possible ways to choose stations.
• View the set of stations used in line 1 as a subset of {1, 2, . . . , n}. - Number of all the subsets (power set) is 2n.- Which is infeasible for large n.
Optimal substructureof the fastest way through the factory.
• An optimal solution to a problem contains within it an optimalsolution to subproblems.
That is, the fastest way through station Si,j contains within it the fastest way through either S1,j-1 or S2,j-1).
Assembly-line scheduling
Recursive solution of the fastest way through the factory.
• Define the value of an optimal solution recursively in terms of theoptimal solutions to subproblems.
Assembly-line scheduling
)][,][min( 2211* xnfxnff ++=
l i[j]: line number, 1 or 2, whose station Si, j-1 is used in a fastest way through Si, j .
l*: line number whose station n is used .
⎩⎨⎧
≥++−+−=+=
⎩⎨⎧
≥++−+−=+=
−
−
2if)]1[,]1[min(1if][
2if)]1[,]1[min(,1if][
,21,11,22
1,222
,11,22,11
1,111
jatjfajfjaejf
jatjfajfjaejf
jjj
jjj
Fast time to get a chassis all the way through the factory.
chassis enters
Completed autoexits
assembly line2
assembly line1 8
5
station S2,1 station S2,2 station S2,6station S2,4station S2,3 station S2,5
station S1,1 station S1,2 station S1,6station S,1,4station S1,3 station S1,5
4497 3
8 5 6 4 7
2
2
4
1
3
2
3
21
1
2
4
3
2
9 18 20 24 32 35
12 16 22 25 30 37
1 2 1 1 2
1 2 1 2 2
f1 [j ]
f2 [j ]
l1 [j ]
j 1 2 3 4 5 6
f * = 38l2 [j ]
j 1 2 3 4 5
l * =1
Overlapping subproblemsof recursive solution of the fastest way through the factory.
• A recursive solution contains a “small” number of distinctsubproblems repeated many times.
• The recursive algorithm: running time is proportional to 2n.- let ri(j) : the number of references made to fi[j] in recursive algorithm.
r1(n) = r2(n) = 1r1(j) = r2(j) = r1(j + 1) + r2(j + 1) for j = 1, 2, . . . , n-1ri(j) = 2n-j
- Thus, f1[1] alone is referenced 2n-1 times.- The total number of references to all fi[j] values is Θ(2n).
Assembly-line scheduling
• Computing the fastest times.
• We can do much better if we compute the fi[j] values in a differentorder from the recursive way.- f1[j] depends only on f1[j-1] and f2[j-1].- compute fi[j] in order of increasing station numbers j. - The time it takes is Θ(n).
Assembly-line scheduling
FASTEST-WAY (a, t, e, x, n)f1[1] ← e1+ a1,1f2[1] ← e2+ a2,1for j ← 2 to n
do if f1[ j – 1 ]+ a1, j ≤ f2[ j – 1 ] + t2, j-1+ a1, jthen f1[ j ] ← f1[ j – 1 ] + a1, j
l1[ j ] ← 1else f1[ j ] ← f2[ j – 1 ] + t2, j-1 + a1, j
l1[ j ] ← 2if f2[ j – 1 ] + a2, j ≤ f1[ j – 1 ] + t1, j-1 + a2, j
then f2[ j ] ← f2[ j – 1 ] + a2, jl2[ j ] ← 2
else f2[ j ] ← f1[ j – 1 ] + + t1, j-1 + a2, jl2[ j ] ← 1
If f1[n] + x1 ≤ f2[n] + x2then f* ← f1[n] + x1
l* ← 1else f* ← f2[n] + x2
l* ← 2
Assembly-line scheduling
1","""][
2","""
*
−←
←
←
jijli
njni
li
istationlineprint
stationlineprint
dodowntofor
PRINT-STATIONS procedure produce the output
line 1, station 6line 2, station 5line 2, station 4line 1, station 3line 2, station 2line 1, station 1
Assembly-line scheduling
• Divide-and-conquer algorithms partition the problem into independent subproblems, solve the subproblems recursively, and then combine their solutions to solve the original problem.
• Dynamic programming is applicable when the subproblems are not independent, that is, when subproblems share subsubproblems.
• Applicable condition of dynamic programming - optimal substructure- overlapping subproblems
Dynamic Programming