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

These are the Lecture Slides of Introduction to Algorithms which includes Expensive Operations, Sort Edges, Running Time, Upshot, Union, Makeset, Disjoint Set, Disjoint Set Union, Naïve Implementation etc. Key important points are: Longest Common Subsequence, Dynamic Programming, Optimal Substructure, Memory, Brute Force Methods, Same Subproblems, Application, Comparison, Subsequence, Running Time

Typology: Slides

2012/2013

Uploaded on 03/23/2013

dhruv
dhruv 🇮🇳

4.3

(12)

194 documents

1 / 32

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Algorithms
Dynamic programming
Longest Common Subsequence
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20

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