Lecture Notes
Lecture Notes
X =ABAC DABAB
ABA?
Longest common subsequence
(LCS)
• For a sequence X = x1,x2, …, xn, a
subsequence is a subset of the sequence
defined by a set of increasing indices (i1, i2, …,
ik) where 1 ≤ i1 < i2 < … < ik ≤ n
X =ABAC DABAB
ABA
Longest common subsequence
(LCS)
• For a sequence X = x1,x2, …, xn, a
subsequence is a subset of the sequence
defined by a set of increasing indices (i1, i2, …,
ik) where 1 ≤ i1 < i2 < … < ik ≤ n
X =ABAC DABAB
ACA?
Longest common subsequence
(LCS)
• For a sequence X = x1,x2, …, xn, a
subsequence is a subset of the sequence
defined by a set of increasing indices (i1, i2, …,
ik) where 1 ≤ i1 < i2 < … < ik ≤ n
X =ABAC DABAB
ACA
Longest common subsequence
(LCS)
• For a sequence X = x1,x2, …, xn, a
subsequence is a subset of the sequence
defined by a set of increasing indices (i1, i2, …,
ik) where 1 ≤ i1 < i2 < … < ik ≤ n
X =ABAC DABAB
DCA?
Longest common subsequence
(LCS)
• For a sequence X = x1,x2, …, xn, a
subsequence is a subset of the sequence
defined by a set of increasing indices (i1, i2, …,
ik) where 1 ≤ i1 < i2 < … < ik ≤ n
X =ABAC DABAB
DCA
Longest common subsequence
(LCS)
• For a sequence X = x1,x2, …, xn, a
subsequence is a subset of the sequence
defined by a set of increasing indices (i1, i2, …,
ik) where 1 ≤ i1 < i2 < … < ik ≤ n
X =ABAC DABAB
AADAA?
Longest common subsequence
(LCS)
• For a sequence X = x1,x2, …, xn, a
subsequence is a subset of the sequence
defined by a set of increasing indices (i1, i2, …,
ik) where 1 ≤ i1 < i2 < … < ik ≤ n
X =ABAC DABAB
AADAA
LCS problem
X =AB C B DAB
Y=BDCABA
LCS problem
X =AB C B DAB
The sequences{B, D, A, B}
of length 4 are the LCS of
Y=B D CABA X and Y, since there is no
common subsequence of
length 5 or greater.
LCS problem
Application:
comparison of two DNA strings
Y=BDCABA
Step 1: Define the problem with
respect to subproblems
X =AB C B DA?
Y=BDCAB?
Y=BDCAB?
Y=BDCABA
If they’re different
Y=BDCABA
If they’re different
X=ABCBDAB
?
Y=BDCABA
If they’re different
Step 1: Define the problem with
respect to subproblems
X =ABC B DAB
Y=BDCABA
1 LCS( X m 1 , Yn 1 ) if xm yn
LCS( X , Y )
max( LCS( X m1 , Y ), LCS( X , Yn 1 ) otherwise
Step 2: Build the solution from
the bottom up
LCS( X m 1 , Yn 1 ) 1 if xm yn
LCS( X , Y )
max( LCS( X m1 , Y ), LCS( X , Yn 1 ) otherwise
LCS(Xj, Yk)
LCS(Xi, Yj)
1 c[i 1, j 1] if xi y j
c[i, j ]
max(c[i 1, j ], c[i, j 1] otherwise
LCS recursive solution
c[i 1, j 1] 1 if xi yj,
c[i, j ]
max(c[i, j 1], c[i 1, j ]) otherwise
c[i 1, j 1] 1 if xi yj,
c[i, j ]
max(c[i, j 1], c[i 1, j ]) otherwise
c[i 1, j 1] 1 if xi yj,
c[i, j ]
max(c[i, j 1], c[i 1, j ]) otherwise
• Second case: xi ≠ yj
• As symbols don’t match, our solution is not
improved, and the length of LCS(Xi , Yj) is the same
as before (i.e. maximum of LCS(Xi, Yj-1) and
LCS(Xi-1,Yj)
for i = 1 to m c[i,0] = 0
for j = 1 to n c[0,j] = 0
ABCB
LCS Example (2) BDCAB
j 0 1 2 3 4 5
i yj B D C A B
0 xi 0 0 0 0 0 0
1 A 0 0
2 B 0
3 C 0
4 B 0
if ( Xi == Yj )
c[i,j] = c[i-1,j-1] + 1
else c[i,j] = max( c[i-1,j], c[i,j-1] )
ABCB
LCS Example (3) BDCAB
j 0 1 2 3 4 5
i yj B D C A B
0 xi 0 0 0 0 0 0
1 A 0 0 0 0
2 B 0
3 C 0
4 B 0
if ( Xi == Yj )
c[i,j] = c[i-1,j-1] + 1
else c[i,j] = max( c[i-1,j], c[i,j-1] )
ABCB
LCS Example (4) BDCAB
j 0 1 2 3 4 5
i yj B D C A B
0 xi 0 0 0 0 0 0
1 A 0 0 0 0 1
2 B 0
3 C 0
4 B 0
if ( Xi == Yj )
c[i,j] = c[i-1,j-1] + 1
else c[i,j] = max( c[i-1,j], c[i,j-1] )
ABCB
LCS Example (5) BDCAB
j 0 1 2 3 4 5
i yj B D C A B
0 xi 0 0 0 0 0 0
1 A 0 0 0 0 1 1
2 B 0
3 C 0
4 B 0
if ( Xi == Yj )
c[i,j] = c[i-1,j-1] + 1
else c[i,j] = max( c[i-1,j], c[i,j-1] )
ABCB
LCS Example (6) BDCAB
j 0 1 2 3 4 5
i yj B D C A B
0 xi 0 0 0 0 0 0
1 A 0 0 0 0 1 1
2 B 0 1
3 C 0
4 B 0
if ( Xi == Yj )
c[i,j] = c[i-1,j-1] + 1
else c[i,j] = max( c[i-1,j], c[i,j-1] )
ABCB
LCS Example (7) BDCAB
j 0 1 2 3 4 5
i yj B D C A B
0 xi 0 0 0 0 0 0
1 A 0 0 0 0 1 1
2 B 0 1 1 1 1
3 C 0
4 B 0
if ( Xi == Yj )
c[i,j] = c[i-1,j-1] + 1
else c[i,j] = max( c[i-1,j], c[i,j-1] )
ABCB
LCS Example (8) BDCAB
j 0 1 2 3 4 5
i yj B D C A B
0 xi 0 0 0 0 0 0
1 A 0 0 0 0 1 1
2 B 0 1 1 1 1 2
3 C 0
4 B 0
if ( Xi == Yj )
c[i,j] = c[i-1,j-1] + 1
else c[i,j] = max( c[i-1,j], c[i,j-1] )
ABCB
LCS Example (10) BDCAB
j 0 1 2 3 4 5
i yj B D C A B
0 xi 0 0 0 0 0 0
1 A 0 0 0 0 1 1
2 B 0 1 1 1 1 2
3 C 0 1 1
4 B 0
if ( Xi == Yj )
c[i,j] = c[i-1,j-1] + 1
else c[i,j] = max( c[i-1,j], c[i,j-1] )
ABCB
LCS Example (11) BDCAB
j 0 1 2 3 4 5
i yj B D C A B
0 xi 0 0 0 0 0 0
1 A 0 0 0 0 1 1
2 B 0 1 1 1 1 2
3 C 0 1 1 2
4 B 0
if ( Xi == Yj )
c[i,j] = c[i-1,j-1] + 1
else c[i,j] = max( c[i-1,j], c[i,j-1] )
ABCB
LCS Example (12) BDCAB
j 0 1 2 3 4 5
i yj B D C A B
0 xi 0 0 0 0 0 0
1 A 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
if ( Xi == Yj )
c[i,j] = c[i-1,j-1] + 1
else c[i,j] = max( c[i-1,j], c[i,j-1] )
ABCB
LCS Example (13) BDCAB
j 0 1 2 3 4 5
i yj B D C A B
0 xi 0 0 0 0 0 0
1 A 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
if ( Xi == Yj )
c[i,j] = c[i-1,j-1] + 1
else c[i,j] = max( c[i-1,j], c[i,j-1] )
ABCB
LCS Example (14) BDCAB
j 0 1 2 3 4 5
i yj B D C A B
0 xi 0 0 0 0 0 0
1 A 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
if ( Xi == Yj )
c[i,j] = c[i-1,j-1] + 1
else c[i,j] = max( c[i-1,j], c[i,j-1] )
ABCB
LCS Example (15) BDCAB
j 0 1 2 3 4 5
i yj B D C A B
0 xi 0 0 0 0 0 0
1 A 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
if ( Xi == Yj )
c[i,j] = c[i-1,j-1] + 1
else c[i,j] = max( c[i-1,j], c[i,j-1] )
LCS Algorithm Running Time
• Remember that
c[i 1, j 1] 1 if x[i] y[ j ],
c[i, j ]
max( c[i, j 1], c[i 1, j ]) otherwise
• So we can start from c[m,n] and go backwards
• Whenever c[i,j] = c[i-1, j-1]+1, remember x[i]
(because x[i] is a part of LCS)
• When i=0 or j=0 (i.e. we reached the beginning),
output remembered letters in reverse order
Finding LCS
j 0 1 2 3 4 5
i yj B D C A B
0 xi 0 0 0 0 0 0
1 A 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
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
1 A 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
(this string turned out to be a palindrome)
Complexity Analysis