Sub topic 13
Stopping Early: Banded Computation with a Threshold
This chapter assumes you know the dynamic programming table from earlier chapters, the two-row version, and Big-O notation.
Many programs never need the exact distance. A spell checker asks "is this word within 2 edits of the typo?" A fuzzy search asks "which entries are within distance k?" When you only care whether the distance is at most some threshold k, you can skip most of the table.
Why a Band Is Enough
Cell dp[i][j] compares the first i characters of a with the first j characters of b. Two prefixes whose lengths differ by d need at least d insertions or deletions. So any cell with |i - j| > k already holds a value greater than k.
Every path from dp[0][0] to dp[m][n] that costs at most k must stay inside the diagonal band |i - j| <= k. Cells outside the band can be treated as infinity. Each row then needs at most 2k + 1 cells.
This gives two cheap rejection tests:
- If
|m - n| > k, the answer is greater than k before you fill any cell. - Values along an optimal path never decrease. If every cell in some row exceeds k, no path through that row can finish within k, so you can stop.
Implementation
This version builds on the two-row approach. It uses k + 1 as "infinity", because any value above k counts as a failure.
def bounded_levenshtein(a, b, k):
"""Return the edit distance if it is <= k, otherwise None."""
m, n = len(a), len(b)
if abs(m - n) > k:
return None
INF = k + 1
prev = [j if j <= k else INF for j in range(n + 1)]
for i in range(1, m + 1):
lo = max(1, i - k)
hi = min(n, i + k)
curr = [INF] * (n + 1) # cells outside the band stay INF
curr[0] = i if i <= k else INF
row_min = curr[0]
for j in range(lo, hi + 1):
cost = 0 if a[i - 1] == b[j - 1] else 1
value = min(prev[j] + 1, curr[j - 1] + 1, prev[j - 1] + cost)
curr[j] = min(value, INF)
row_min = min(row_min, curr[j])
if row_min > k:
return None # no path can finish within k
prev = curr
return prev[n] if prev[n] <= k else None
assert bounded_levenshtein("kitten", "sitting", 3) == 3
assert bounded_levenshtein("kitten", "sitting", 2) is None
assert bounded_levenshtein("flaw", "lawn", 2) == 2
assert bounded_levenshtein("", "abc", 3) == 3
assert bounded_levenshtein("cat", "catalog", 3) is NoneCost
| Version | Time | Extra memory |
|---|---|---|
| Full table | O(m·n) | O(m·n) |
| Two rows | O(m·n) | O(min(m, n)) |
| Banded, threshold k | O(k·min(m, n)) | O(n) as written, O(k) with band-indexed rows |
With a small k, such as 1 or 2 for spell checking, the work grows linearly with the string length.
If you don't know k in advance, start with k = 1 and double it until the call succeeds. The total cost is O(d·min(m, n)), where d is the true distance. Ukkonen described this idea in 1985.
Exercise
With k = 2, at most how many cells per row does the banded algorithm compute, and why can it reject "cat" versus "catalog" at k = 3 without filling any cell?
Show answer
At most 2k + 1 = 5 cells per row (columns i-2 through i+2). The lengths differ by 4, which is more than 3, so at least 4 insertions are required and the distance must exceed k.
Exercise
Why is it safe to stop as soon as every cell in a row exceeds k?
Show answer
Any edit path from dp[0][0] to dp[m][n] passes through every row, and dp values never decrease along a path. If every cell in row i exceeds k, every complete path costs more than k.