Sub topic 10
LIS as a Longest Common Subsequence
This chapter assumes you know the O(n^2) LIS dynamic program from earlier chapters. It also assumes you have seen the classic longest common subsequence (LCS) table, though the code below is complete on its own. The point here is a reduction. A reduction turns a new problem into one you already know how to solve. Once LIS is written as an LCS problem, you get a second O(n^2) algorithm for it, and the link between the two problems becomes clear.
The Reduction
A longest common subsequence of two sequences x and y is the longest sequence that appears, in order but not necessarily contiguously, in both of them.
To find the strictly increasing LIS of a:
- Build
b = sorted(set(a)). This holds the distinct values ofain increasing order. - Compute
LCS(a, b).
The LCS length equals the LIS length.
Why It Works
The argument has two directions.
- Every common subsequence is strictly increasing. A common subsequence is a subsequence of
b. Becausebis strictly increasing, any subsequence of it is strictly increasing too. It is also a subsequence ofa, so it is an increasing subsequence ofa. - Every strictly increasing subsequence of
ais common. Its values are distinct and in increasing order, and each one appears inb. Values that appear in increasing order also appear in that order inb, so the subsequence is a subsequence ofb.
The two sets of sequences are therefore identical, and so their longest members have the same length.
Implementation
The LCS table below keeps only two rows, which brings memory down to O(n).
def lcs_length(x, y):
prev = [0] * (len(y) + 1)
for xi in x:
cur = [0]
for j, yj in enumerate(y):
if xi == yj:
cur.append(prev[j] + 1)
else:
cur.append(max(prev[j + 1], cur[j]))
prev = cur
return prev[-1]
def lis_via_lcs(a):
return lcs_length(a, sorted(set(a)))
def non_decreasing_via_lcs(a):
return lcs_length(a, sorted(a))
print(lis_via_lcs([10, 9, 2, 5, 3, 7, 101, 18])) # 4
print(lis_via_lcs([1, 3, 3, 2, 3])) # 3
print(non_decreasing_via_lcs([1, 3, 3, 2, 3])) # 4Strict vs Non-Decreasing
Whether you deduplicate b decides which variant you get.
| Variant | Second sequence `b` | Example on `[1, 3, 3, 2, 3]` |
|---|---|---|
| Strictly increasing | `sorted(set(a))` | 3 (`1, 2, 3`) |
| Non-decreasing | `sorted(a)` | 4 (`1, 3, 3, 3`) |
If b keeps duplicates, equal values can match more than once, so the common subsequences become non-decreasing. In the strict case, forgetting set is the most common mistake. The code gives no error, only an answer that is too large whenever a has repeated values.
Cost and When to Use It
| Method | Time | Extra space |
|---|---|---|
| Direct O(n^2) DP | O(n^2) | O(n) |
| LCS reduction (two rows) | O(n · d), where d = distinct values | O(d) |
| Tails array with binary search | O(n log n) | O(n) |
The reduction never beats the tails-array method. Its value is mostly conceptual. It shows that LIS is a special case of LCS. It also helps in practice when you already have a well-tested LCS routine, or when the values come from a small fixed alphabet, so d is small and O(n · d) is close to linear.
The relationship also runs the other way. If the second sequence has no repeated elements, as with two permutations, you can map each element to its position in one sequence and take the LIS of the result. That turns LCS into LIS and lets it run in O(n log n). This is the Hunt–Szymanski idea in its simplest form.
Exercise
For a = [4, 4, 4], what do lisvialcs and nondecreasingvia_lcs return, and why do they differ?
Show answer
lisvialcs returns 1 because b = [4], so only one 4 can be matched. nondecreasingvia_lcs returns 3 because b = [4, 4, 4], so all three 4s match.
Exercise
Given two permutations p = [3, 1, 2, 4] and q = [1, 2, 3, 4], explain how to compute LCS(p, q) using LIS, and give the result.
Show answer
Record each value's position in q: pos = {1:0, 2:1, 3:2, 4:3}. Replace each element of p with its position in q to get [2, 0, 1, 3]. The strict LIS of this array is 3 (0, 1, 3), so LCS(p, q) = 3, for example 1, 2, 4.