Sub topic 12
LCS and Insert/Delete Edit Distance
This chapter assumes you know the LCS recurrence, the bottom-up DP table, and the two-row version that computes LCS length in O(min(m, n)) space. Here you will see that LCS length directly gives the minimum number of insertions and deletions needed to turn one sequence into another.
The Insert/Delete Edit Model
An edit script is a list of operations that turns sequence a into sequence b. In this chapter only two operations exist:
- Delete one element of
a. - Insert one element of
b.
Substitution is not allowed. The indel distance is the length of the shortest such script.
Why the Distance Is m + n − 2·LCS
Take any common subsequence of length L. You can edit a into b by keeping those L elements in place, deleting the other m − L elements of a, and inserting the n − L elements of b that are missing. That script has m + n − 2L operations.
The reverse also holds. In any insert/delete script, the elements of a that are never deleted end up in b in the same order. They form a common subsequence. So a shorter script would imply a longer common subsequence. The minimum script therefore comes from a longest common subsequence:
| Quantity | Formula |
|---|---|
| Deletions | m − LCS |
| Insertions | n − LCS |
| Indel distance | m + n − 2·LCS |
The Same Fact, Cell by Cell
Let D(i, j) be the indel distance between the first i elements of a and the first j elements of b. It follows its own recurrence:
- If
a[i-1] == b[j-1], then D(i, j) = D(i−1, j−1). - Otherwise, D(i, j) = 1 + min(D(i−1, j), D(i, j−1)).
If you substitute D(i, j) = i + j − 2·L(i, j) into the LCS recurrence, you get exactly these two cases. A max over L becomes a min over D. Both tables carry the same information.
The code below computes the distance both ways and checks that the results agree.
def lcs_length_two_rows(a, b):
if len(b) > len(a):
a, b = b, a
prev = [0] * (len(b) + 1)
for x in a:
curr = [0] * (len(b) + 1)
for j, y in enumerate(b, 1):
if x == y:
curr[j] = prev[j - 1] + 1
else:
curr[j] = max(prev[j], curr[j - 1])
prev = curr
return prev[-1]
def indel_distance(a, b):
return len(a) + len(b) - 2 * lcs_length_two_rows(a, b)
def indel_distance_direct(a, b):
m, n = len(a), len(b)
d = [[0] * (n + 1) for _ in range(m + 1)]
for i in range(m + 1):
d[i][0] = i
for j in range(n + 1):
d[0][j] = j
for i in range(1, m + 1):
for j in range(1, n + 1):
if a[i - 1] == b[j - 1]:
d[i][j] = d[i - 1][j - 1]
else:
d[i][j] = 1 + min(d[i - 1][j], d[i][j - 1])
return d[m][n]
print(indel_distance("ABCBDAB", "BDCABA")) # 5
print(indel_distance_direct("ABCBDAB", "BDCABA")) # 5Common Errors
- Using the LCS of substrings. The formula needs the longest common subsequence. A contiguous match understates L and overstates the distance.
- Forgetting the empty cases. If either sequence is empty, LCS = 0 and the distance is the length of the other sequence. The base row and column of the direct table encode this.
Exercise
For "kitten" and "sitting", find the LCS length, the indel distance, and compare it with the Levenshtein distance of 3.
Show answer
One LCS is "ittn", so LCS = 4. Indel distance = 6 + 7 − 2·4 = 5. Levenshtein distance is smaller (3) because it counts each of the substitutions k→s and e→i as one operation, where the indel model needs two.
Exercise
Explain why the indel distance between two sequences always has the same parity (odd or even) as m + n.
Show answer
The distance equals m + n − 2·LCS, and subtracting an even number does not change parity.