Sub topic 9
Custom Costs for Each Operation
This chapter assumes you already know the Levenshtein recurrence and how the dynamic programming table is filled, from the earlier chapters. You should also be comfortable passing functions as arguments in Python.
Plain Levenshtein distance charges 1 for every insertion, deletion, and substitution. Real problems often need different prices. A spell checker might treat s typed for a as a cheap mistake, since the keys sit next to each other. In DNA comparison, some base substitutions happen more often than others. Weighted edit distance keeps the same table and changes only the prices.
The Weighted Recurrence
Let ins(c), del(c), and sub(x, y) be cost functions, with sub(x, x) = 0. The recurrence becomes:
dp[0][0] = 0dp[i][0] = dp[i-1][0] + del(a[i-1])dp[0][j] = dp[0][j-1] + ins(b[j-1])dp[i][j] = min(dp[i-1][j] + del(a[i-1]), dp[i][j-1] + ins(b[j-1]), dp[i-1][j-1] + sub(a[i-1], b[j-1]))
The base cases change too. A first column filled with i only works when every deletion costs 1.
def weighted_distance(a, b,
ins=lambda c: 1,
dele=lambda c: 1,
sub=lambda x, y: 0 if x == y else 1):
m, n = len(a), len(b)
dp = [[0.0] * (n + 1) for _ in range(m + 1)]
for i in range(1, m + 1):
dp[i][0] = dp[i - 1][0] + dele(a[i - 1])
for j in range(1, n + 1):
dp[0][j] = dp[0][j - 1] + ins(b[j - 1])
for i in range(1, m + 1):
for j in range(1, n + 1):
dp[i][j] = min(
dp[i - 1][j] + dele(a[i - 1]),
dp[i][j - 1] + ins(b[j - 1]),
dp[i - 1][j - 1] + sub(a[i - 1], b[j - 1]),
)
return dp[m][n]
# Default costs reproduce Levenshtein distance.
assert weighted_distance("kitten", "sitting") == 3
# Keyboard-aware substitution: neighboring keys cost 0.5.
NEAR = {("a", "s"), ("s", "a"), ("e", "r"), ("r", "e")}
def keyboard_sub(x, y):
if x == y:
return 0
return 0.5 if (x, y) in NEAR else 1
assert weighted_distance("cat", "cst", sub=keyboard_sub) == 0.5
assert weighted_distance("cat", "cxt", sub=keyboard_sub) == 1
# Substitution priced at 2 is never better than delete plus insert.
assert weighted_distance("kitten", "sitting", sub=lambda x, y: 0 if x == y else 2) == 5How Cost Choices Change Behavior
| Cost setting | Effect |
|---|---|
| All costs 1 | Standard Levenshtein distance |
| `sub` cheaper for similar characters | Ranks likely typos above random ones |
| `sub` ≥ `ins + del` | Substitution is never needed, which gives indel distance (next chapter) |
| `ins` ≠ `del` | Distance becomes asymmetric: `d(a, b)` may differ from `d(b, a)` |
Common Errors
- Unchanged base cases. If you keep
dp[i][0] = i, the first row and column use the wrong prices, and every cell built on them is wrong. - Nonzero match cost. If
sub(x, x) > 0, identical strings get a positive distance. - Negative costs. The minimum stops meaning "cheapest edit sequence." Keep all costs non-negative.
- Float comparisons in traceback. With costs like 0.1, tests such as
dp[i][j] == dp[i-1][j] + del(...)can fail because of rounding. Use integer costs (for example, scale 0.5 to 1 and 1 to 2) or compare within a small tolerance.
Exercise
With ins = 1, del = 1, and sub = 3 for any mismatch, what is the weighted distance from "abc" to "abd", and which edits achieve it?
Show answer
2. Delete "c" (cost 1) and insert "d" (cost 1). A single substitution would cost 3, so the algorithm never chooses it.
Exercise
With ins = 1 and del = 2 per character, compute d("ab", "") and d("", "ab"). What does this show?
Show answer
d("ab", "") = 4 (two deletions) and d("", "ab") = 2 (two insertions). When insertion and deletion have unequal costs, the distance is not symmetric.