Sub topic 12
When Is an Edit Distance a True Metric?
This chapter assumes you know the Levenshtein recurrence, the weighted-cost version, and the optimal string alignment (OSA) variant from earlier chapters. It also assumes you can read short Python functions.
The Four Metric Axioms
A distance function d is a metric if, for all strings x, y, z, it satisfies these four rules:
| Axiom | Statement | What breaks without it |
|---|---|---|
| Non-negativity | `d(x, y) >= 0` | Negative "distances" make no sense |
| Identity | `d(x, y) == 0` only when `x == y` | Different strings could look identical |
| Symmetry | `d(x, y) == d(y, x)` | Direction of comparison changes the answer |
| Triangle inequality | `d(x, z) <= d(x, y) + d(y, z)` | Detours can be "shorter" than direct paths |
The triangle inequality matters most in practice. Metric index structures such as BK-trees and vantage-point trees use it to skip whole groups of candidates during fuzzy search. If the inequality fails, those structures can silently miss correct matches.
Why Levenshtein and Hamming Qualify
Levenshtein distance is defined as the minimum cost over all edit sequences. That definition gives you the triangle inequality for free. If you have an edit sequence from x to y and another from y to z, putting them end to end gives a valid sequence from x to z. The cheapest direct sequence can cost no more than that combined one.
Symmetry holds because every insertion can be reversed by a deletion of equal cost, and every substitution can be reversed by another substitution. Identity holds because every real edit costs 1. Hamming distance passes all four axioms for the same reasons, restricted to strings of equal length.
Where Weighted Costs Can Break It
Weighted edit distance keeps the triangle inequality as long as costs are non-negative, because it is still a minimum over all sequences. The other axioms depend on the cost choices:
- Symmetry requires insertion cost equal to deletion cost and
sub(p, q) == sub(q, p). With insert = 1 and delete = 2,d("", "a") = 1butd("a", "") = 2. - Identity requires every edit that changes the string to have a positive cost. A zero-cost substitution between
'O'and'0'makesd("CO", "C0") = 0for two different strings.
Why OSA Fails the Triangle Inequality
OSA does not take the minimum over all edit sequences. It forbids editing any substring more than once, so you cannot transpose two characters and then insert a character between them. Joining two valid OSA scripts can produce a script OSA does not allow, so the argument above stops working.
The following code checks this case:
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]
x, y, z = "ca", "ac", "abc"
dxy, dyz, dxz = osa_distance(x, y), osa_distance(y, z), osa_distance(x, z)
print(dxy, dyz, dxz) # 1 1 3
print(dxz <= dxy + dyz) # False: triangle inequality violatedUnrestricted Damerau-Levenshtein allows edits after a transposition and gives d("ca", "abc") = 2. It is a true metric. It needs a more complex algorithm that tracks the last position where each character appeared.
Exercise
A weighted edit distance uses insert = 1, delete = 1, and substitution cost 1 for every pair of distinct characters, except that substituting 'a' for 'b' costs 3. Which metric axioms does it satisfy, and what is d("a", "b")?
Show answer
It satisfies all four axioms. Costs are positive and symmetric, and the distance is still a minimum over all edit sequences. d("a", "b") = 2, because deleting 'a' and inserting 'b' (cost 2) is cheaper than the substitution (cost 3).
Exercise
Why does the triangle inequality hold automatically for weighted Levenshtein with non-negative costs but not for OSA?
Show answer
Weighted Levenshtein is the minimum over all edit sequences, so joining a script from x to y with one from y to z gives a valid script from x to z. OSA restricts which sequences are allowed, so the joined script may not be permitted and the bound fails.