Sub topic 16
Beyond Two Sequences: Multi-Sequence LCS and NP-Hardness
This chapter assumes you know the two-sequence LCS recurrence, the memoized solution, and the O(mn) analysis from earlier chapters. You should also be comfortable with Big-O notation and with what an NP-hard problem is in general terms.
Generalizing the Recurrence
The two-sequence recurrence extends naturally to more sequences. With three sequences a, b, and c, a subproblem becomes a triple of positions (i, j, k):
- If any position has reached the end of its sequence, the answer is 0.
- If
a[i] == b[j] == c[k], the answer is 1 plus the answer for(i+1, j+1, k+1). - Otherwise, take the maximum over the three ways to drop one symbol: advance
i, advancej, or advancek.
The argument for optimal substructure is the same as before. If all current symbols match, some optimal answer uses that match. If they do not all match, at least one of those symbols plays no part in an optimal answer, so dropping it loses nothing.
from functools import lru_cache
def lcs3_length(a, b, c):
@lru_cache(maxsize=None)
def solve(i, j, k):
if i == len(a) or j == len(b) or k == len(c):
return 0
if a[i] == b[j] == c[k]:
return 1 + solve(i + 1, j + 1, k + 1)
return max(solve(i + 1, j, k),
solve(i, j + 1, k),
solve(i, j, k + 1))
return solve(0, 0, 0)
print(lcs3_length("ABCBDAB", "BDCABA", "BCBA")) # 4
print(lcs3_length("AB", "BA", "B")) # 1The Cost Grows Exponentially in k
For k sequences of lengths n1 through nk, the table has (n1+1)(n2+1)...(nk+1) cells. Each cell takes the maximum over k neighbors. Total time is O(k · n^k) when every sequence has length n, and space is O(n^k).
| Sequences (k) | Cells when every length is 100 |
|---|---|
| 2 | about 10^4 |
| 3 | about 10^6 |
| 5 | about 10^10 |
| 10 | about 10^20 |
When k is a fixed constant, this is still polynomial time. When k is part of the input, the exponent grows with the input size. Maier proved in 1978 that LCS of an arbitrary number of sequences is NP-hard. Unless P = NP, no algorithm solves every instance in polynomial time.
A Common Mistake: Chaining Pairwise LCS
A tempting shortcut is to compute lcs(lcs(a, b), c). It runs fast, but it can return a result shorter than the true answer. Take a = "AB", b = "BA", and c = "B". The pairwise LCS of a and b has length 1 and could be either "A" or "B". With the tie-breaking rule from the traceback chapter, it comes out as "A". The LCS of "A" and "B" is empty, yet the true three-way answer is "B", with length 1.
Practical tools for many sequences, such as multiple sequence alignment in bioinformatics, accept this trade-off on purpose. They use progressive pairwise merging, guide trees, or beam search, and give up guaranteed optimality to get acceptable running time.
Exercise
How many table cells does the exact DP need for three strings of lengths 50, 40, and 30?
Show answer
(50+1)(40+1)(30+1) = 51 × 41 × 31 = 64,821 cells.
Exercise
Why does LCS for k = 3 run in polynomial time, even though multi-sequence LCS is NP-hard?
Show answer
With k fixed at 3, the O(k · n^k) cost is O(n^3), a polynomial. NP-hardness applies when k can grow with the input. Then the exponent is no longer a constant.