# Subsequence A subsequence of a sequence/string X = is a sequence obtained by deleting 0 or more elements from X. Example: sudan is a subsequence of sesquipedalian.

## Presentation on theme: "Subsequence A subsequence of a sequence/string X = is a sequence obtained by deleting 0 or more elements from X. Example: sudan is a subsequence of sesquipedalian."— Presentation transcript:

Subsequence A subsequence of a sequence/string X = is a sequence obtained by deleting 0 or more elements from X. Example: sudan is a subsequence of sesquipedalian. 1 2 m So is equal. There are 2 subsequences of X. m A common subsequence Z of two sequences X and Y is a subsequence of both. Example: ua is a common subsequence of sudan and equal.

LCS = equiin Longest Common Subsequence 1 2 n a longest common subsequence (LCS) of X and Y. Example a) X = abcbdab Y = bdcaba b) X = enquiring Y = sequipedalian c) X = empty bottle Y = nematode knowledge LCS = emt ole Input: X = Y = 1 2 m Output: LCS = bcba LCS = bdab 1 2

Brute-force Algorithm For every subsequence of X, check if its a subsequence of Y. Worst-case running time: ( n 2 ) ! m 2 subsequences of X to check. m Each check takes O(n) time – scanning Y for first element, then from there for next element...

Optimal Substructure of LCS 1 2 2 Case 1: x = y m n z = x = y n m k Then Otherwise, LCS has length k + 1, a contradiction. zz LCS Z: z 1 2 k x m m yyy Y:Y: n 1 2 xx X:X: 1 x 2 m

Optimal Substructure (contd) Case 2: x y m n m yyy Y:Y: n 1 2 xx X:X: 1 x 2 m Either LCS of xx 1 x 2m – 1 yyy n 1 2 y or LCS of xx 1 x 2 y 1 2 y n – 1 m

A Recursive Formula X =, Y = 1 2 m 1 2 n 0 if i = 0 or j = 0 max(c[i, j 1], c[i 1, j]) if i, j > 0 and x y i j c[i, j] = c[i 1, j 1] + 1 if i, j > 0 and x = y A total of (m + 1)(n + 1) subproblems. Then c[m,n] = length of an LCS of X and Y. c[i, j] = length of an LCS of and. 1 i 1 j

Recursion Tree m = 3 and n = 4 3, 4 2, 3 2, 4 3, 3 x y x = y 3 4 Depth of the tree m+n since at each level i and/or j reduce by 1 Branches by at most 3 at each node. Amount of work for top-down recursion: 3 (m+n) 1, 2 1, 3 2, 2 1, 3 1, 4 2, 3 2, 2 2, 3 3, 2 recurring subproblems

Constructing an LCS 0 0 0 0 0 0 0 0 0 000000000000000 s e s q u i p e d a l i a n e n q u i r i n g c[, ] 1. Initialize entries c[i, 0] and c[0, j] m = 9 n = 14

Constructing an LCS (contd) 0 0 0 0 0 0 0 0 0 000000000000000 s e s q u i p e d a l i a n e n q u i r i n g 0111111111111 c[, ] 2. Fill out entries row by row. Save pointers in b[1..m, 1..n]. 1 c[1, 2] = 1 + c[0, 1] = 1c[1, 7] = max(c[1, 6], c[0, 7]) = 1

0 0 0 0 0 0 0 0 0 000000000000000 s e s q u i p e d a l i a n e n q u i r i n g 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 3 2 1 1 3 3 3 3 3 1 1 3 2 4 4 4 4 4 1 1 2 3 4 4 4 4 4 1 1 2 4 3 4 4 4 4 11111 11111 2 2 22222 33333 4 3 44444 444444 554445 44455 4 6 44556 c[, ] 1 LCS Length Determined LCS has length 6.

0 0 0 0 0 0 0 0 0 000000000000000 s e s q u i p e d a l i a n e n q u i r i n g 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 3 2 1 1 3 3 3 3 3 1 1 3 2 4 4 4 4 4 1 1 2 3 4 4 4 4 4 1 1 2 4 3 4 4 4 4 11111 11111 2 2 22222 33333 4 3 44444 444444 554445 44455 4 6 44556 c[, ] 1 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. Print n Print i Print u Print q Print e LCS = equiin

Assembly-Line Scheduling 2 67153 2 4 1 47 9 12 8 3 5 1 3 assembly line 1 assembly line 2 chassis enters completed auto exits station S 1, 1 1, 21, 41, 3 2, 12, 22, 3 2, 4 e e 2 1 x x 1 2 tt t t t t 1, 1 2, 1 t t 2, 2 1, 21, 3 2, 3 2, 4 1, 4 Minimize the total time through the factory for one auto.

Optimal Substructure f (j) : the fastest possible time to get a chassis from the starting point through station S. i i, j S a a a S S 1, j1, j – 1 2, j – 1 t 1, j1, j – 1 f (j) = 1 e + a if j = 1 11, 1 min(f [j – 1] + a, f [j – 1] + t + a ), if j > 1 11, j 2 2, j – 1 1, j f* = min(f [n] + x, f [n] + x ). 11 2 2

The DP Solution 2 67153 2 4 1 47 9 12 8 3 5 1 3 assembly line 1 assembly line 2 chassis enters completed auto exits station S 1, 1 1, 21, 41, 3 2, 12, 22, 3 2, 4 x 2 830 12 27 15 11 23302 2 21 f [j] l [j] j 1 2 3 4j 2 3 4 1 1 2 2 f* = 28 from line 2

Download ppt "Subsequence A subsequence of a sequence/string X = is a sequence obtained by deleting 0 or more elements from X. Example: sudan is a subsequence of sesquipedalian."

Similar presentations