Sub topic 11
Handling Transpositions: OSA versus Damerau-Levenshtein
This chapter assumes you know the Levenshtein recurrence and the dynamic programming table from earlier chapters, and can read Python with 2D lists.
Why Swaps Need Their Own Operation
Typing errors often swap two neighboring characters, as in "teh" for "the". Levenshtein distance counts that swap as two substitutions, so it gives "teh" a distance of 2 from "the". If you are building a spell checker, you probably want a swap to cost 1. A transposition is an edit that exchanges two adjacent characters. You can add it to the edit operations in two ways, and the two ways give different answers.
Optimal String Alignment (OSA)
The simple approach adds one more case to the recurrence. If the last two characters of a are the last two characters of b in reverse order, then dp[i][j] may also come from dp[i-2][j-2] + 1:
def osa_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):
cost = 0 if a[i - 1] == b[j - 1] else 1
dp[i][j] = min(dp[i - 1][j] + 1, dp[i][j - 1] + 1, dp[i - 1][j - 1] + cost)
if i > 1 and j > 1 and a[i - 1] == b[j - 2] and a[i - 2] == b[j - 1]:
dp[i][j] = min(dp[i][j], dp[i - 2][j - 2] + 1)
return dp[m][n]
assert osa_distance("ab", "ba") == 1
assert osa_distance("ca", "abc") == 3 # unrestricted Damerau-Levenshtein gives 2The new case jumps two cells diagonally, so the two swapped characters are consumed as a single block. That block is then finished, and no later edit can touch it. OSA therefore enforces a hidden rule: no substring is edited more than once.
Where OSA Falls Short
Turning "ca" into "abc" shows the restriction at work:
| Approach | Edits | Cost |
|---|---|---|
| OSA | substitute c→a, substitute a→b, insert c | 3 |
| Damerau-Levenshtein | swap "ca"→"ac", insert b between them | 2 |
The cheaper route inserts a character between the two characters it just swapped. OSA forbids this, because it would edit the swapped block a second time.
This restriction also breaks the triangle inequality. OSA gives d("ca", "ac") = 1 and d("ac", "abc") = 1, but d("ca", "abc") = 3, which is larger than 1 + 1. OSA is therefore not a true metric. True Damerau-Levenshtein is a metric.
True Damerau-Levenshtein
The unrestricted algorithm, due to Lowrance and Wagner, lets characters be inserted between swapped characters. For each character it remembers the last row where that character appeared in a (da), and for the current row it remembers the last column with a match (db). It runs in O(mn) time, plus a dictionary indexed by the characters of a.
def damerau_levenshtein(a, b):
m, n = len(a), len(b)
INF = m + n
da = {}
d = [[0] * (n + 2) for _ in range(m + 2)]
d[0][0] = INF
for i in range(m + 1):
d[i + 1][0] = INF
d[i + 1][1] = i
for j in range(n + 1):
d[0][j + 1] = INF
d[1][j + 1] = j
for i in range(1, m + 1):
db = 0
for j in range(1, n + 1):
k = da.get(b[j - 1], 0)
l = db
if a[i - 1] == b[j - 1]:
cost = 0
db = j
else:
cost = 1
d[i + 1][j + 1] = min(
d[i][j] + cost,
d[i + 1][j] + 1,
d[i][j + 1] + 1,
d[k][l] + (i - k - 1) + 1 + (j - l - 1),
)
da[a[i - 1]] = i
return d[m + 1][n + 1]
assert damerau_levenshtein("ca", "abc") == 2
assert damerau_levenshtein("ab", "ba") == 1Choosing Between Them
| Need | Choice |
|---|---|
| Fast spell checking of typos | OSA (simpler, and nearly always the same answer) |
| A true metric, for example for metric-tree indexes | Damerau-Levenshtein |
| No transpositions expected | Plain Levenshtein |
Exercise
Compute the OSA distance and the plain Levenshtein distance between "acb" and "abc". Do they differ?
Show answer
OSA gives 1, because one swap turns "cb" into "bc". Levenshtein gives 2, because it needs two substitutions. They differ by one.
Exercise
Why can't OSA be used with a BK-tree, which depends on the triangle inequality?
Show answer
OSA can break the triangle inequality. For example, d("ca","abc") = 3 is larger than d("ca","ac") + d("ac","abc") = 2. A BK-tree that relies on that inequality to prune branches could then skip valid matches.