Sub topic 2
Stating the Longest Common Subsequence Problem
This chapter assumes you know basic Python (lists, loops, functions) and the meaning of a subsequence from the previous chapter. It turns the informal idea into a precise problem, so that every later algorithm solves the same thing.
The Formal Statement
You are given two sequences:
X = x1 x2 ... xmof lengthmY = y1 y2 ... ynof lengthn
A sequence Z is a common subsequence of X and Y if Z is a subsequence of X and also a subsequence of Y. The longest common subsequence (LCS) problem asks for a common subsequence of maximum length.
There are really two related questions:
| Question | Output | Unique? |
|---|---|---|
| LCS length | A single integer | Yes |
| LCS itself | One sequence achieving that length | No, several may exist |
Most algorithms compute the length first and recover a sequence afterward. Keep the two apart in your head. Many bugs come from code that claims to return "the" LCS when several are equally valid.
A Running Example
This tutorial uses one pair throughout:
X = "ABCBDAB"(m = 7)Y = "BDCABA"(n = 6)
The LCS length is 4. At least three different strings achieve it: "BCBA", "BDAB", and "BCAB". No common subsequence of length 5 exists.
The matched positions must increase in both strings. They do not need to be adjacent. That second point is what separates this problem from longest common substring, which a later chapter covers.
Checking a Candidate
Verifying that a candidate is a common subsequence takes linear time, even though finding the longest one is the hard part. The checker below consumes an iterator, so each match must come after the previous one.
def is_subsequence(z, s):
it = iter(s)
return all(ch in it for ch in z)
def is_common_subsequence(z, a, b):
return is_subsequence(z, a) and is_subsequence(z, b)
X, Y = "ABCBDAB", "BDCABA"
for cand in ["BCBA", "BDAB", "BCAB", "ABBA", "BCBAB"]:
print(cand, is_common_subsequence(cand, X, Y))
# BCBA True
# BDAB True
# BCAB True
# ABBA False (not in X: no A comes after the second B... check it yourself)
# BCBAB FalseRun it and confirm each line. Being able to verify answers cheaply is useful when you test your own implementations later.
Properties Worth Stating Up Front
- Bounds:
0 <= LCS(X, Y) <= min(m, n). - Empty input: if either sequence is empty, the LCS length is 0.
- Symmetry:
LCS(X, Y)has the same length asLCS(Y, X). - Identity:
LCS(X, X) = X. - Element type: nothing requires characters. Any elements you can compare for equality work, such as lines of a file, tokens, or DNA bases. Diff tools compare whole lines.
Common Mistakes When Stating the Problem
- Requiring the matched elements to be contiguous. That describes substrings.
- Returning a set of shared characters. Order matters, and repeated elements count separately.
- Testing only that
zis in each string with Python'sinoperator on strings. That checks for a substring, not a subsequence.
Exercise
For X = "AGGTAB" and Y = "GXTXAYB", give the LCS length and one LCS, then state the upper bound on the LCS length that follows from the sequence lengths alone.
Show answer
The LCS length is 4, for example "GTAB". The upper bound from lengths alone is min(6, 7) = 6.
Exercise
Why does "ABBA" in "ABCBDAB" return False in Python, and does that settle whether "ABBA" is a subsequence of "ABCBDAB"?
Show answer
The in operator on strings tests for a contiguous substring, so it says nothing about subsequences. Using is_subsequence, "ABBA" is a subsequence of "ABCBDAB" (A at position 1, B at 2, B at 4, A at 6). It is not a subsequence of "BDCABA", because only one B comes after that string's first A, so it is not a common subsequence.