200 likes | 623 Views
m. There are 2 subsequences of X. A common subsequence Z of two sequences X and Y is a subsequence of both. Example: “ ua ” is a common subsequence of “ s u d a n ” and “ eq ua l ”. Subsequence. A subsequence of a sequence/string X = < x , x , … , x > is
E N D
m There are 2 subsequences of X . A common subsequenceZ of two sequences X and Y is a subsequence of both. Example: “ua” is a common subsequence of“sudan” and “equal”. Subsequence A subsequence of a sequence/string X = < x , x , …, x > is a sequence obtained by deleting 0 or more elements from X. 1 2 m Example:“sudan” is a subsequence of “sesquipedalian”. So is “equal”.
LCS = bcba LCS = bdab 1 2 Longest Common Subsequence Input: X = < x , x , …, x > 1 2 m Y = < y , y , …, y > 1 2 n Output: a longest common subsequence (LCS) of X and Y. Example a) X = abcbdabY = bdcaba b) X = enquiringY = sequipedalian LCS = equiin c) X = empty bottleY = nematode knowledge LCS = emt ole
m Worst-case running time: ( n 2 ) ! m 2 subsequences of X to check. Each check takes O(n) time – scanning Y for first element, then from there for next element ... Brute-force Algorithm For every subsequence of X, check if it’s a subsequence of Y.
X: x x x x 1 2 m m Y: y y y 1 2 n Case 1: x = y LCS Z: m z z n z 1 2 k z = x = y Then m n k Optimal Substructure of LCS 2 m 1 2 Otherwise, LCS has length ≥ k + 1, a contradiction.
X: x x x x 1 1 2 2 x x Y: y y y 1 2 n Either LCS of Case 2: x≠y m n x x x 1 2 m – 1 y y y 1 2 n or LCS of m y y y 1 2 n – 1 Optimal Substructure (cont’d) m m
c[i, j] = length of an LCS of < x , …, x > and < y , …, y > . 1 i 1 j 0if i = 0 or j = 0 c[i, j] = c[i1, j1] + 1 if i, j > 0 and x = y i j max(c[i, j1], c[i1, j]) if i, j > 0 and x y i j A Recursive Formula X = < x , x , …, x >, Y = < y , y , …, y > 1 2 m 1 2 n Then c[m,n] = length of an LCS of X and Y. A total of (m + 1)(n + 1) subproblems.
Branches by at most 3 at each node. Amount of work for top-down recursion:3 (m+n) Recursion Tree m = 3 and n = 4 3, 4 xy x = y 3 4 3 4 2, 3 2, 4 3, 3 1, 2 1, 3 2, 2 1, 3 1, 4 2, 32, 22, 3 3, 2 recurring subproblems Depth of the tree ≤ m+n since at each level i and/or j reduce by 1
Constructing an LCS 1. Initialize entries c[i, 0] and c[0, j] s e s q u i p e d a l i a n c[ , ] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 e 0 n 0 q 0 u m = 9 0 i 0 r 0 i 0 n 0 g 0 n = 14
Constructing an LCS (cont’d) 2. Fill out entries row by row. Save pointers in b[1..m, 1..n]. c[ , ] s e s q u i p e d a l i a n 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 e 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 n 0 q 0 u 0 i 0 r 0 i 0 n 0 g 0 c[1, 2] = 1 + c[0, 1] = 1 c[1, 7] = max(c[1, 6], c[0, 7]) = 1
LCS Length Determined c[ , ] s e s q u i p e d a l i a n 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 e 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 n 0 0 1 1 1 1 1 1 1 1 1 1 1 1 2 q 0 0 1 1 2 2 2 2 2 2 2 2 2 2 2 u 0 0 1 1 2 3 3 3 3 3 3 3 3 3 3 i 0 0 1 1 2 3 4 4 4 4 4 4 4 4 4 r 0 0 1 1 2 3 4 4 4 4 4 4 4 4 4 i 0 0 1 1 2 3 4 4 4 4 4 4 5 5 5 n 0 0 1 1 2 3 4 4 4 4 4 4 5 5 6 LCS has length 6. g 0 0 1 1 2 3 4 4 4 4 4 4 5 5 6
Reconstructing an LCS 3. Starting from the lower-right corner, follow the pointers in b[ , ]. Whenever encounters an upper-left pointer, print the labeling char. c[ , ] s e s q u i p e d a l i a n 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 e Print “e” 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 n 0 0 1 1 1 1 1 1 1 1 1 1 1 1 2 Print “q” q 0 0 1 1 2 2 2 2 2 2 2 2 2 2 2 Print “u” u 0 0 1 1 2 3 3 3 3 3 3 3 3 3 3 i Print “i” 0 0 1 1 2 3 4 4 4 4 4 4 4 4 4 r 0 0 1 1 2 3 4 4 4 4 4 4 4 4 4 i Print “i” 0 0 1 1 2 3 4 4 4 4 4 4 5 5 5 n 0 0 1 1 2 3 4 4 4 4 4 4 5 5 6 Print “n” g 0 0 1 1 2 3 4 4 4 4 4 4 5 5 6 LCS = equiin
e 1 x 1 t 1, 1 t t t 1, 2 1, 3 1, 4 e 2 t t 2, 2 t 2, 1 2, 3 t 2, 4 x 2 Assembly-Line Scheduling station S station S station S station S 1, 2 1, 3 1, 4 1, 1 assembly line 1 6 7 15 3 2 3 5 2 1 chassis enters 1 2 1 3 completed auto exits 4 assembly line 2 9 4 8 7 station S station S station S station S 2, 1 2, 2 2, 3 2, 4 Minimize the total time through the factory for one auto.
S S 1, j – 1 1, j a a 1, j – 1 1, j e + a if j = 1 1 1, 1 f (j) = 1 t 2, j – 1 min(f [j – 1] + a , f [j – 1] + t + a ), if j > 1 1 1, j a 2 2, j – 1 1, j 2, j – 1 S 2, j – 1 f* = min(f [n] + x , f [n] + x ). 1 1 2 2 Optimal Substructure f (j) : the fastest possible time to get a chassis from the starting point through station S . i i, j
The DP Solution station S station S station S station S 1, 2 1, 3 1, 4 1, 1 assembly line 1 6 7 15 3 2 3 5 2 1 chassis enters 1 2 1 3 completed auto exits 4 x assembly line 2 2 9 4 8 7 station S station S station S station S 2, 1 2, 2 2, 3 2, 4 j 1 2 3 4 j 2 3 4 f [j] 8 15 30 27 l [j] 1 1 2 from line 2 1 1 f* = 28 12 15 23 30 1 2 2 f [j] l [j] 2 2