Subsequences
Suppose you havea sequence
X = < x1,x2,…,xm>
of elements over a finite set S.
A sequence Z = < z1,z2,…,zk> over S is called a subsequence of
X if and only if it can be obtained from X by deleting
elements.
Put differently, there exist indices i1<i2 <…<ik such that
za = xia
for all a in the range 1<= a <= k.
2
3.
Common Subsequences
Suppose thatX and Y are two sequences
over a set S.
We say that Z is a common subsequence
of X and Y if and only if
• Z is a subsequence of X
• Z is a subsequence of Y
3
4.
The Longest Common
SubsequenceProblem
Given two sequences X and Y over a set S,
the longest common subsequence problem
asks to find a common subsequence of X
and Y that is of maximal length.
4
5.
5
Naïve Solution
Let Xbe a sequence of length m,
and Y a sequence of length n.
Check for every subsequence of X whether it is a
subsequence of Y, and return the longest common
subsequence found.
There are 2m
subsequences of X. Testing a sequences
whether or not it is a subsequence of Y takes O(n) time.
Thus, the naïve algorithm would take O(n2m
) time.
Prefix
Let X =< x1,x2,…,xm> be a sequence.
We denote by Xi the sequence
Xi = < x1,x2,…,xi>
and call it the ith
prefix of X.
8
9.
LCS Notation
Let Xand Y be sequences.
We denote by LCS(X, Y) the set of
longest common subsequences of X and
Y.
9
10.
Optimal Substructure
Let X= < x1,x2,…,xm>
and Y = < y1,y2,…,yn> be two sequences.
Let Z = < z1,z2,…,zk> is any LCS of X and Y.
a) If xm = yn then certainly xm = yn = zk
and Zk-1 is in LCS(Xm-1 , Yn-1)
10
11.
Optimal Substructure (2)
LetX = < x1,x2,…,xm>
and Y = < y1,y2,…,yn> be two sequences.
Let Z = < z1,z2,…,zk> is any LCS of X and Y.
b) If xm <> yn then xm <> zk implies that Z is
in LCS(Xm-1 , Y)
c)If xm <> yn then yn <> zk implies that Z is
in LCS(X, Yn-1)
11
12.
Overlapping Subproblems
If xm= yn then we solve the subproblem
to find an element in LCS(Xm-1 , Yn-1 ) and
append xm
If xm <> yn then we solve the two
subproblems of finding elements in
LCS(Xm-1 , Yn-1 ) and LCS(Xm-1 , Yn-1 ) and
choose the longer one.
12
13.
Recursive Solution
Let Xand Y be sequences.
Let c[i,j] be the length of an element in LCS(Xi, Yj).
c[i,j] =
13
14.
Dynamic Programming Solution
Tocompute length of an element in LCS(X,Y) with
X of length m and Y of length n, we do the
following:
•Initialize first row and first column of the array
c with 0.
•Calculate c[1,j] for 1 <= j <= n, c[2,j] for 1 <= j
<= n …
•Return c[m,n]
•Complexity O(mn).
14
15.
Dynamic Programming Solution(2)
How can we get an actual longest common
subsequence?
Store in addition to the array c an array
b pointing to the optimal subproblem
chosen when computing c[i,j].
15
16.
16
Example
yj B DC A
xj 0 0 0 0 0
A 0 0 0 0 1
B 0 1 1 1 1
C 0 1 1 2 2
B 0 1 1 2 2
Start at b[m,n]. Follow the arrows. Each
diagonal array gives one element of the LCS.
19
LCS(X,Y)
for i 1 to m do
for j 1 to n do
if xi
= yj
c[i, j] c[i-1, j-1]+1
b[i, j] “D”
else
if c[i-1, j] c[i, j-1]
c[i, j] c[i-1, j]
b[i, j] “U”
else
c[i, j] c[i, j-1]
b[i, j] “L”
return c and b
20.
Greedy Algorithms
There existsa greedy solution to this
problem that can be advantageous when
the size of the alphabet S is small.
20