Sub topic 10
Indel Distance and the Longest Common Subsequence
This chapter assumes you know the Levenshtein recurrence, how to fill the dynamic programming table, and how to change operation costs, all covered in earlier chapters. Here we remove substitution entirely and see what remains.
What Indel Distance Measures
Indel distance allows only two operations: insertion and deletion ("indel" is short for insertion/deletion). Each one costs 1. To change one character into another, you delete the old character and insert the new one, which costs 2 instead of 1.
You can get indel distance from the weighted recurrence by setting the substitution cost to 2. A substitution then costs exactly as much as a deletion plus an insertion, so it never beats that pair. The diagonal move still matters when characters match, because a match costs 0.
The Connection to LCS
A subsequence is a sequence you get by deleting zero or more characters from a string without reordering the rest. The longest common subsequence (LCS) of a and b is the longest string that is a subsequence of both.
Every indel edit script keeps some characters untouched. Those kept characters appear in the same order in both strings, so they form a common subsequence. Every other character of a must be deleted and every other character of b must be inserted. To minimize the edits, keep as many characters as possible:
indel(a, b) = m + n − 2 · LCS(a, b)
Here m and n are the string lengths.
For kitten and sitting, the LCS is ittn with length 4, so the indel distance is 6 + 7 − 8 = 5. The Levenshtein distance is 3, because Levenshtein can substitute k→s and e→i at cost 1 each.
Computing Both in Code
The two tables below differ only in how they treat a mismatch on the diagonal.
def lcs_length(a, b):
m, n = len(a), len(b)
dp = [[0] * (n + 1) for _ in range(m + 1)]
for i in range(1, m + 1):
for j in range(1, n + 1):
if a[i - 1] == b[j - 1]:
dp[i][j] = dp[i - 1][j - 1] + 1
else:
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1])
return dp[m][n]
def indel_distance(a, b):
m, n = len(a), len(b)
dp = [[0] * (n + 1) for _ in range(m + 1)]
for i in range(m + 1):
dp[i][0] = i
for j in range(n + 1):
dp[0][j] = j
for i in range(1, m + 1):
for j in range(1, n + 1):
best = min(dp[i - 1][j] + 1, dp[i][j - 1] + 1)
if a[i - 1] == b[j - 1]:
best = min(best, dp[i - 1][j - 1])
dp[i][j] = best
return dp[m][n]
for a, b in [("kitten", "sitting"), ("flaw", "lawn"), ("", "abc")]:
d = indel_distance(a, b)
assert d == len(a) + len(b) - 2 * lcs_length(a, b)
print(a, b, d)The LCS table maximizes kept characters, and the indel table minimizes edits. The formula above turns one into the other.
Where It Shows Up
| Measure | Operations | `kitten` → `sitting` |
|---|---|---|
| Levenshtein | insert, delete, substitute | 3 |
| Indel | insert, delete | 5 |
Line-based diff tools such as Unix diff work on the indel model. A changed line appears as one removed line plus one added line, and the unchanged lines form an LCS of the two files. Myers' diff algorithm finds a shortest indel script quickly when the files are similar.
Exercise
Compute the indel distance between "abcde" and "ace" using the LCS formula, and give the Levenshtein distance for comparison.
Show answer
The LCS is "ace" with length 3, so the indel distance is 5 + 3 − 2·3 = 2 (delete b and d). The Levenshtein distance is also 2, because no substitution helps here.
Exercise
Why does setting the substitution cost to 2 give the same answer as forbidding substitution?
Show answer
A substitution at cost 2 costs the same as a deletion plus an insertion, so any script that uses it can be rewritten as an indel-only script of equal cost. Allowing it can never lower the minimum.