Docsity

Longest Common Subsequence - Introduction to Algorithms - Lecture Slides, Slides of Computer Science

Partial preview of the text

Download Longest Common Subsequence - Introduction to Algorithms - Lecture Slides and more Slides Computer Science in PDF only on Docsity!

Algorithms

Dynamic programming

Longest Common Subsequence

Dynamic programming

• It is used, when the solution can be

recursively described in terms of solutions

to subproblems ( optimal substructure )

• Algorithm finds solutions to subproblems

and stores them in memory for later use

• More efficient than “ brute-force methods ”,

which solve the same subproblems over

and over again

LCS Algorithm

• if |X| = m, |Y| = n, then there are 2m

subsequences of x; we must compare each

with Y (n comparisons)

• So the running time of the brute-force

algorithm is O(n 2m^ )

• Notice that the LCS problem has optimal

substructure : solutions of subproblems are

parts of the final solution.

• Subproblems: “find LCS of pairs of prefixes

of X and Y”

LCS Algorithm

• First we’ll find the length of LCS. Later we’ll

modify the algorithm to find LCS itself.

• Define X i , Yj to be the prefixes of X and Y of

length i and j respectively

• Define c[i,j] to be the length of LCS of X i and

Yj

• Then the length of LCS of X and Y will be

c[m,n]

max( [ , 1 ], [ 1 , ]) otherwise

[ 1 , 1 ] 1 if [ ] [ ],

[ , ]

c i j c i j

c i j x i y j

c i j

LCS recursive solution

• When we calculate c[i,j], we consider two

cases:

• First case: x[i]=y[j] : one more symbol in

strings X and Y matches, so the length of LCS

X i and Y j equals to the length of LCS of

smaller strings X i-1 and Y i-1 , plus 1

max( [ , 1 ], [ 1 , ]) otherwise

[ 1 , 1 ] 1 if [ ] [ ],

[ , ]

c i j c i j

c i j x i y j

c i j

LCS recursive solution

• Second case: x[i] != y[j]

• As symbols don’t match, our solution is not

improved, and the length of LCS(Xi , Y j ) is the

same as before (i.e. maximum of LCS(Xi , Y j-1 )

and LCS(X i-1 ,Y j )

max( [ , 1 ], [ 1 , ]) otherwise

[ 1 , 1 ] 1 if [ ] [ ],

[ , ]

c i j c i j

c i j x i y j

c i j

Why not just take the length of LCS(Xi-1 , Y j-1 )?

LCS Example

We’ll see how LCS algorithm works on the

following example:

• X = ABCB

• Y = BDCAB

LCS(X, Y) = BCB

X = A B C B

Y = B D C A B

What is the Longest Common Subsequence

of X and Y?

LCS Example (0)

j 0 1 2 3 4 5

i

Xi

A

B
C
B

Yj B D C A B

X = ABCB; m = |X| = 4

Y = BDCAB; n = |Y| = 5

Allocate array c[5,4]

ABCB

BDCAB

LCS Example (2)

j 0 1 2 3 4 5

i

Xi

A

B
C
B

Yj B D C A B

0

0

if ( X (^) i == Y (^) j ) c[i,j] = c[i-1,j-1] + 1 else c[i,j] = max( c[i-1,j], c[i,j-1] )

ABCB

BDCAB

LCS Example (3)

j 0 1 2 3 4 5

i

Xi

A

B
C
B

Yj B D C A B

0

0

if ( X (^) i == Y (^) j ) c[i,j] = c[i-1,j-1] + 1 else c[i,j] = max( c[i-1,j], c[i,j-1] )

ABCB

BDCAB

LCS Example (5)

j 0 1 2 3 4 5

i

Xi

A

B
C
B

Yj B D C A B

0

0

if ( X (^) i == Y (^) j ) c[i,j] = c[i-1,j-1] + 1 else c[i,j] = max( c[i-1,j], c[i,j-1] )

ABCB

BDCAB

LCS Example (6)

j 0 1 2 3 4 5

i

Xi

A

B
C
B

Yj B D C A B

0

0

if ( X (^) i == Y (^) j ) c[i,j] = c[i-1,j-1] + 1 else c[i,j] = max( c[i-1,j], c[i,j-1] )

ABCB

BDCAB

LCS Example (8)

j 0 1 2 3 4 5

i

Xi

A

B
C
B

Yj B D C A B

0

0

if ( X (^) i == Y (^) j ) c[i,j] = c[i-1,j-1] + 1 else c[i,j] = max( c[i-1,j], c[i,j-1] )

ABCB

BDCAB

LCS Example (10)

j 0 1 2 3 4 5

i

Xi

A

B
C
B

Yj B D C A B

0

0

if ( X (^) i == Y (^) j ) c[i,j] = c[i-1,j-1] + 1 else c[i,j] = max( c[i-1,j], c[i,j-1] )

ABCB

BDCAB