0% found this document useful (0 votes)
18 views41 pages

21 Longest Common Subsequence (LCS)

The document explains the concept of Longest Common Subsequence (LCS), highlighting the difference between subsequences and substrings. It details the problem of finding the maximum-length common subsequence between two sequences, providing a recurrence relation and examples to illustrate the solution. The document also discusses the optimal substructure of the LCS problem and outlines the recursive solution approach.

Uploaded by

avisheksarker453
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views41 pages

21 Longest Common Subsequence (LCS)

The document explains the concept of Longest Common Subsequence (LCS), highlighting the difference between subsequences and substrings. It details the problem of finding the maximum-length common subsequence between two sequences, providing a recurrence relation and examples to illustrate the solution. The document also discusses the optimal substructure of the LCS problem and outlines the recursive solution approach.

Uploaded by

avisheksarker453
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 41

Longest Common Subsequence (LCS)

Subsequence:
Subset of letters in order from left to right.

Remember:
“Subsequence” is different from “substring”. Substring
is continuous, no gap. But, subsequence can have gap.

Paris 1
Example : Subsequence
X= {A B C B D A B } Y= {B D C A B A}
 CBD is a substring of X. BBA is a subsequence of X,
but not a substring of X.
 DBBB is not a subsequence of X, because order violation.
 BDAB is both substring and subsequence of X,
but it is only a subsequence of Y

 Substrings are also subsequence,


but a subsequence may not be a substring.

Paris 2
Example : LCS
Common Subsequence:
Subsequence of both.
Longest Common Subsequence:
Same subsequence of maximum length.

X= {A B C B D A B } Y= {B D C A B A}
 X = A BCBD AB, Y = BD CA BA LCS: BCBA
(4)

 Other examples: BCAB (4), BDAB (4).


Paris 3
 Maximum possible length = 4
Longest Common Subsequence Problem

Problem:
Given two sequences X = < x1, x2, ….., xm > and
Y = < y1, y2, ….., yn > find a
maximum-length common subsequence of X and Y.

Solution (Idea):
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”

Paris 4
Notations

• X = < x1, x2, ….., xm > and Y = < y1, y2, ..., yn > : Sequences

• Xi, Yj : Prefixes of X and Y of length i and j respectively

• c[i,j] : The length of LCS of Xi and Yj

• c[m,n] : The length of LCS of X and Y

Paris 5
Optimal Substructure of an LCS

• Let X x1 , x2 ,  , xm  and Y  y1 , y2 ,  , yn  be


sequences and let Z z1 , z 2 ,  , z k  be any LCS of
X and Y .
• If x  y , then z  x  y and Z is an LCS of X and Y
m n k m n k1 m 1 n 1

• If xm  y n , then z k  xm
implies that Z is an LCS of X m  1 and Y

• If xm  y n , then z k  y n
implies that Z is an LCS of X and Yn  1
Paris 6
Optimal Substructure of an LCS (Book: Cormen; Page-392)

Paris 7
The Recurrence

 0 if i 0 or j 0

c[i, j ]  c[i  1, j  1]  1 if i, j  0 and xi  y j ,
max(c[i, j  1], c[i  1, j ]) if i, j  0 and x  y
 i j

Paris 8
LCS Recursive Solution
• We start with i = j = 0 (empty substrings of x
and y)
• Since X0 and Y0 are empty strings, their LCS is
always empty (i.e. c[0,0] = 0)
• LCS of empty string and any other string is
empty, so for every i and j: c[0, j] = c[i,0] = 0

Paris 9
LCS Recursive Solution
 When we calculate c[i,j], we consider two cases:
• First case: x[i]=y[j]: one more symbol matches, so the
length of LCS Xi and Yj equals to the length of LCS of
smaller strings Xi-1 and Yi-1 , plus 1
• Second case: x[i] != y[j]: As symbols do not match,
solution is not improved, and the length of LCS(Xi , Yj)
is the same as before (maximum of LCS (Xi, Yj-1) and
LCS (Xi-1,Yj) )
Paris 10
LCS Example

What is the Longest Common


Subsequence of X and Y?

X = ABCB
Y = BDCAB

Paris 11
LCS Example (0)
j 0 1 2 3 4 5
i Yj B D C A B
0 Xi
A
1
2 B

3 C

4 B

X = ABCB; m = |X| = 4
Y = BDCAB; n = |Y| = 5
Allocate array c[6,5]
Paris 12
LCS Example (1)
j 0 1 2 3 4 5
i Yj B D C A B
0 Xi
A
1 0
2 B
0
3 C 0
4 B 0

for i = 1 to m c[i,0] = 0
Paris 13
LCS Example (2)
j 0 1 2 3 4 5
i Yj B D C A B
0 Xi 0 0 0 0 0 0
A
1 0
2 B
0
3 C 0
4 B 0

for j = 0 to n c[0,j] = 0
Paris 14
LCS Example (3)
j 0 1 2 3 4 5
i Yj B D C A B
0 Xi 0 0 0 0 0 0
A
1 0 0
2 B
0
3 C 0
4 B 0

case i=1 and j=1


A != B
but, c[0,1]>=c[1,0]
so c[1,1] = c[0,1], and b[1,1] =
Paris 15
LCS Example (4)
j 0 1 2 3 4 5
i Yj B D C A B
0 Xi 0 0 0 0 0 0
A
1 0 0 0
2 B
0
3 C 0
4 B 0

case i=1 and j=2


A != D
but, c[0,2]>=c[1,1]
so c[1,2] = c[0,2], and b[1,2] =
Paris 16
LCS Example (5)
j 0 1 2 3 4 5
i Yj B D C A B
0 Xi 0 0 0 0 0 0
A
1 0 0 0 0
2 B
0
3 C 0
4 B 0

case i=1 and j=3


A != C
but, c[0,3]>=c[1,2]
so c[1,3] = c[0,3], and b[1,3] =
Paris 17
LCS Example (6)
j 0 1 2 3 4 5
i Yj B D C A B
0 Xi 0 0 0 0 0 0
A
1 0 0 0 0 1
2 B
0
3 C 0
4 B 0

case i=1 and j=4


A=A
so c[1,4] = c[0,2]+1, and b[1,4] =

Paris 18
LCS Example (7)
j 0 1 2 3 4 5
i Yj B D C A B
0 Xi 0 0 0 0 0 0
A
1 0 0 0 0 1 1
2 B
0
3 C 0
4 B 0

case i=1 and j=5


A != B
this time c[0,5]<c[1,4]
so c[1,5] = c[1, 4], and b[1,5] =
Paris 19
LCS Example (8)
j 0 1 2 3 4 5
i Yj B D C A B
0 Xi 0 0 0 0 0 0
A
1 0 0 0 0 1 1
2 B
0 1
3 C 0
4 B 0

case i=2 and j=1


B=B
so c[2, 1] = c[1, 0]+1, and b[2, 1] =

Paris 20
LCS Example (9)
j 0 1 2 3 4 5
i Yj B D C A B
0 Xi 0 0 0 0 0 0
A
1 0 0 0 0 1 1
2 B
0 1 1
3 C 0
4 B 0

case i=2 and j=2


B != D
and c[1, 2] < c[2, 1]
so c[2, 2] = c[2, 1] and b[2, 2] =
Paris 21
LCS Example (10)
j 0 1 2 3 4 5
i Yj B D C A B
0 Xi 0 0 0 0 0 0
A
1 0 0 0 0 1 1
2 B
0 1 1 1
3 C 0
4 B 0

case i=2 and j=3


B != D
and c[1, 3] < c[2, 2]
so c[2, 3] = c[2, 2] and b[2, 3] =
Paris 22
LCS Example (11)
j 0 1 2 3 4 5
i Yj B D C A B
0 Xi 0 0 0 0 0 0
A
1 0 0 0 0 1 1
2 B
0 1 1 1 1
3 C 0
4 B 0

case i=2 and j=4


B != A
and c[1, 4] = c[2, 3]
so c[2, 4] = c[1, 4] and b[2, 2] =
Paris 23
LCS Example (12)
j 0 1 2 3 4 5
i Yj B D C A B
0 Xi 0 0 0 0 0 0
A
1 0 0 0 0 1 1
2 B
0 1 1 1 1 2
3 C 0
4 B 0

case i=2 and j=5


B=B
so c[2, 5] = c[1, 4]+1 and b[2, 5] =

Paris 24
LCS Example (13)
j 0 1 2 3 4 5
i Yj B D C A B
0 Xi 0 0 0 0 0 0
A
1 0 0 0 0 1 1
2 B
0 1 1 1 1 2
3 C 0 1
4 B 0

case i=3 and j=1


C != B
and c[2, 1] > c[3,0]
so c[3, 1] = c[2, 1] and b[3, 1] =
Paris 25
LCS Example (14)
j 0 1 2 3 4 5
i Yj B D C A B
0 Xi 0 0 0 0 0 0
A
1 0 0 0 0 1 1
2 B
0 1 1 1 1 2
3 C 0 1 1
4 B 0

case i=3 and j= 2


C != D
and c[2, 2] = c[3, 1]
so c[3, 2] = c[2, 2] and b[3, 2] =
Paris 26
LCS Example (15)
j 0 1 2 3 4 5
i Yj B D C A B
0 Xi 0 0 0 0 0 0
A
1 0 0 0 0 1 1
2 B
0 1 1 1 1 2
3 C 0 1 1 2
4 B 0

case i=3 and j= 3


C=C
so c[3, 3] = c[2, 2]+1 and b[3, 3] =

Paris 27
LCS Example (16)
j 0 1 2 3 4 5
i Yj B D C A B
0 Xi 0 0 0 0 0 0
A
1 0 0 0 0 1 1
2 B
0 1 1 1 1 2
3 C 0 1 1 2 2
4 B 0

case i=3 and j= 4


C != A
c[2, 4] < c[3, 3]
so c[3, 4] = c[3, 3] and b[3, 3] =
Paris 28
LCS Example (17)
j 0 1 2 3 4 5
i Yj B D C A B
0 Xi 0 0 0 0 0 0
A
1 0 0 0 0 1 1
2 B
0 1 1 1 1 2
3 C 0 1 1 2 2 2
4 B 0

case i=3 and j= 5


C != B
c[2, 5] = c[3, 4]
so c[3, 5] = c[2, 5] and b[3, 5] =
Paris 29
LCS Example (18)
j 0 1 2 3 4 5
i Yj B D C A B
0 Xi 0 0 0 0 0 0
A
1 0 0 0 0 1 1
2 B
0 1 1 1 1 2
3 C 0 1 1 2 2 2
4 B 0 1

case i=4 and j=1


B=B
so c[4, 1] = c[3, 0]+1 and b[4, 1] =

Paris 30
LCS Example (19)
j 0 1 2 3 4 5
i Yj B D C A B
0 Xi 0 0 0 0 0 0
A
1 0 0 0 0 1 1
2 B
0 1 1 1 1 2
3 C 0 1 1 2 2 2
4 B 0 1 1

case i=4 and j=2


B != D
c[3, 2] = c[4, 1]
so c[4, 2] = c[3, 2] and b[4, 2] =
Paris 31
LCS Example (20)
j 0 1 2 3 4 5
i Yj B D C A B
0 Xi 0 0 0 0 0 0
A
1 0 0 0 0 1 1
2 B
0 1 1 1 1 2
3 C 0 1 1 2 2 2
4 B 0 1 1 2

case i=4 and j= 3


B != C
c[3, 3] > c[4, 2]
so c[4, 3] = c[3, 3] and b[4, 3] =
Paris 32
LCS Example (21)
j 0 1 2 3 4 5
i Yj B D C A B
0 Xi 0 0 0 0 0 0
A
1 0 0 0 0 1 1
2 B
0 1 1 1 1 2
3 C 0 1 1 2 2 2
4 B 0 1 1 2 2

case i=4 and j=4


B != A
c[3, 4] = c[4, 3]
so c[4, 4] = c[3, 4] and b[3, 5] =
Paris 33
LCS Example (22)
j 0 1 2 3 4 5
i Yj B D C A B
0 Xi 0 0 0 0 0 0
A
1 0 0 0 0 1 1
2 B
0 1 1 1 1 2
3 C 0 1 1 2 2 2
4 B 0 1 1 2 2 3

case i=4 and j=5


B= B
so c[4, 5] = c[3, 4]+1 and b[4, 5] =

Paris 34
LCS-Length(X, Y)
m = length(X), n = length(Y)
Algorithm: LCS
for i = 1 to m
c[i, 0] = 0
for j = 0 to n
c[0, j] = 0
for i = 1 to m
for j = 1 to n
if ( xi = = yj )
c[i, j] = c[i - 1, j - 1] + 1
bi, j  " "
else if c[i - 1, j]>=c[i, j - 1]
c[i, j] = c[i - 1, j]
bi, j  "  "
else c[i, j] = c[i, j - 1]
bi, j  "  "
Paris 35
return c and b
Finding LCS
j 0 1 2 3 4 5
i Yj B D C A B
0 Xi 0 0 0 0 0 0
A
1 0 0 0 0 1 1
2 B
0 1 1 1 1 2
3 C 0 1 1 2 2 2
4 B 0 1 1 2 2 3

Paris 36
Finding LCS (2)
j 0 1 2 3 4 5
i Yj B D C A B
0 Xi 0 0 0 0 0 0
A
1 0 0 0 0 1 1
2 B
0 1 1 1 1 2
3 C 0 1 1 2 2 2
4 B 0 1 1 2 2 3
LCS (reversed order): B C B
LCS (straight order): B C B
Paris 37
Algorithm: Finding LCS

Paris 38
Practice Example (Book: Cormen; Page-395)

Paris
LCS : BCBA Length : 4 39
Thank You

Paris 40
Stay Safe

Paris 41

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy