Sub topic 3
Indel Distance and the Longest Common Subsequence
This chapter assumes you know the Levenshtein recurrence, the two-row table, and Big-O notation.
Indel Distance and LCS
If you remove substitution and allow only insertions and deletions, you get the indel distance. A substitution can be replaced by one delete and one insert, so substitutions add nothing here. The characters you keep form a common subsequence, and the best you can do is keep the longest one (the LCS):
indel(a, b) = len(a) + len(b) − 2 · LCS(a, b)
def lcs_length(a, b):
prev = [0] * (len(b) + 1)
for ca in a:
curr = [0]
for j, cb in enumerate(b, 1):
if ca == cb:
curr.append(prev[j - 1] + 1)
else:
curr.append(max(prev[j], curr[j - 1]))
prev = curr
return prev[-1]
def indel_distance(a, b):
return len(a) + len(b) - 2 * lcs_length(a, b)
assert lcs_length("kitten", "sitting") == 4 # "ittn"
assert indel_distance("kitten", "sitting") == 5Transpositions: OSA versus Damerau-Levenshtein
Optimal string alignment (OSA) adds one more case: swapping two adjacent characters costs 1. OSA never edits a substring more than once. True Damerau-Levenshtein drops that restriction. The two measures differ on "ca" → "abc": OSA gives 3 and Damerau-Levenshtein gives 2 (swap to "ac", then insert b).
When Is It a True Metric?
A metric is non-negative, is zero only for identical strings, is symmetric, and satisfies the triangle inequality. Levenshtein, Hamming, indel, and Damerau-Levenshtein are all metrics when costs are symmetric. OSA is not a metric. It fails the triangle inequality: d("ca","ac") = 1 and d("ac","abc") = 1, but d("ca","abc") = 3. This matters for index structures such as BK-trees, which depend on the triangle inequality.
Banded Computation with a Threshold
Suppose you only care whether the distance is at most k. Any cell with |i − j| > k already costs more than k, so you fill only a diagonal band of width 2k + 1. That takes O(k · min(m, n)) time. If |m − n| > k, you can reject the pair immediately.
Bit-Parallel Speedups
Myers' algorithm (1999) stores the differences between neighboring cells in a column as bit vectors. With machine words of w bits, it runs in O(⌈m/w⌉ · n) time, which is very fast for patterns of 64 characters or fewer.
Why Quadratic Time Is Hard to Beat
Backurs and Indyk (2015) proved that an O(n^(2−ε)) algorithm for edit distance would refute the Strong Exponential Time Hypothesis (SETH). Practical speedups therefore come from thresholds, bit parallelism, or approximation.
In Practice
| Task | Good choice |
|---|---|
| Spell checking (typos) | Damerau-Levenshtein or OSA with a threshold |
| Fuzzy search over a dictionary | Banded Levenshtein or Myers, with a BK-tree for indexing |
| Line-based diff | Indel/LCS (Myers' diff) |
| DNA and protein alignment | Weighted costs (Needleman-Wunsch) |
| Fixed-length codes | Hamming |
Exercise
Strings "abc" and "xyz" have LCS length 0. What are their indel and Levenshtein distances, and why do they differ?
Show answer
Indel distance is 3 + 3 − 0 = 6. Levenshtein distance is 3. Levenshtein can do each mismatch as one substitution, but indel distance needs a delete plus an insert for each one.
Exercise
With threshold k = 2, should you run the DP to compare "cat" and "category"?
Show answer
No. The lengths differ by 5, which is more than 2, so the distance is above the threshold and you can reject the pair without filling any cells.