Edit Distance & Its Limits
Alignment, bit-parallel speedups and the SETH lower bound.
The Wagner–Fischer table computes the edit distance of two strings of length in time, and in half a century nobody has found an algorithm that is polynomially faster. Since 2015 there is a conditional explanation: a strongly subquadratic algorithm would refute the Strong Exponential Time Hypothesis. This lesson covers what can be done (fast algorithms when the distance is small, word-level parallelism, near-linear approximation) and why the barrier stands.
What you’ll learn
- Prove the structural facts behind fast edit-distance algorithms: the band lemma and the 0/1 steps along diagonals.
- Compute the edit distance in time when it is at most , in by diagonal transitions, and in with constant-time longest common extensions.
- Explain the word-RAM speedups of Masek–Paterson () and Myers ().
- State the SETH-based lower bound, sketch its reduction from orthogonal vectors, and summarize the state of approximation as of 2025.
The recurrence and two structural facts
The edit distance is the fewest single-character insertions, deletions and substitutions that turn into , and . It is a metric (symmetric, and it obeys the triangle inequality), and the Wagner–Fischer table computes it: , with
and . The table is the edit graph: an alignment is a path from to , and horizontal and vertical steps each cost 1. Faster algorithms rest on two facts about this table.
Band. A path from to takes at least horizontal or vertical steps, each costing 1. A path to through a cell with already costs more than there, so it cannot be optimal when :
Neighbors. by the delete option. Conversely, remove from an optimal alignment of with : if it was deleted, the cost drops by 1; if it was aligned with a letter of , that letter becomes an insertion and the cost rises by at most 1. Columns are symmetric:
Diagonals. Each option for is at least : the first adds 0 or 1 to it, and the other two, and , are at least by the neighbor bound. The substitution option gives the upper bound:
Small distances: bands and diagonals
Banded DP. To decide whether , fill only the diagonals and treat every other cell as . By the band lemma the answer is right whenever it is at most , in time. Not knowing in advance, try : the last run dominates, so the total is . Ukkonen (1985) and Myers (1986) obtained bounds of this type.
Diagonal transitions. The diagonal lemma says each diagonal is a staircase of values, so it is described by where its value increases. Let be the furthest row on diagonal with . One more edit moves to a neighboring diagonal or one step along the same one, and then matches are free:
where follows equal letters along diagonal . The distance is the first with .
def edit_distance_diag(a, b):
"""Levenshtein distance by diagonal transitions: O((n + m) * D) time."""
n, m = len(a), len(b)
def slide(i, d): # follow matches along diagonal d = j - i
while i < n and i + d < m and a[i] == b[i + d]:
i += 1
return i
far = {0: slide(0, 0)} # cost 0: furthest row on diagonal 0
e = 0
while far.get(m - n, -1) < n: # (n, m) not reached yet
e += 1
nxt = {}
for d in range(-e, e + 1):
i = max(far.get(d, -2) + 1, # substitute: same diagonal
far.get(d + 1, -2) + 1, # delete a[i]: from diagonal d + 1
far.get(d - 1, -2)) # insert b[j]: from diagonal d - 1
i = min(i, n, m - d) # clamp to the table
if i >= max(0, -d): # a real cell of diagonal d
nxt[d] = slide(i, d)
far = nxt
return e
print(edit_distance_diag('saturday', 'sunday')) # 3For cost only diagonals matter, and each slide only moves forward, so the time is ; this is Myers's algorithm (1986) adapted to substitutions. Clamping at the border is safe because neighbors differ by at most 1. Checked against the full table on thousands of random pairs, including 300-letter DNA strings with up to 20 edits.
Two DNA reads of length are known to differ by at most 50 edits. Which method computes their edit distance fastest?
- The full Wagner–Fischer table: cells
- The band of 101 diagonals: about cells
- Diagonal transitions with constant-time extensions: about steps
- Hirschberg's algorithm, which is faster than the table
Show the answer
C. Diagonal transitions with constant-time extensions: about steps
With LCE queries in after linear preprocessing, the diagonal transitions cost about 2,500 steps on top of the preprocessing. The band is a big improvement on the full table but still about cells; Hirschberg's method saves space, not time.
Word-level parallelism
Four Russians. Cut the table into blocks. By the neighbor lemma, a block's top row and left column are described by differences in , and its bottom row and right column, again as differences, depend only on those and on the letters it compares. Over a constant alphabet there are kinds of block; with that is kinds, all precomputed in time. Filling blocks at each then takes time (Masek & Paterson, 1980, for a finite alphabet and suitably restricted costs).
Bit vectors. Myers (1999) stores a whole column of vertical differences in two bit vectors (where the difference is , and where it is ) and advances to the next column with a constant number of word operations (and, or, xor, addition, shifts) per rows. For a pattern of length , 64 on today's processors, each text character costs word operations: in all. It was designed for approximate pattern matching, and with a different boundary row it computes the edit distance of two whole strings; fast alignment libraries are built on it.
| Exact algorithm | Time | Best when |
|---|---|---|
| Wagner–Fischer (1974) | Simplicity; any costs | |
| Banded DP, doubling | The distance is small | |
| Diagonal transitions (Ukkonen, 1985; Myers, 1986) | Small , no index needed | |
| Landau–Vishkin (1988) | to decide | Very small ; needs constant-time extensions |
| Masek–Paterson (1980) | Theory: constant alphabet, restricted costs | |
| Myers's bit vectors (1999) | Practice: a factor of about over the table |
The SETH barrier, and approximation
Split the variables of a CNF formula with clauses into two halves. For each assignment of the first half, let have a 1 in coordinate exactly when satisfies no literal of clause ; define in the same way for the second half.
The combined assignment satisfies clause iff at least one half does, that is iff . So is satisfiable iff some pair is orthogonal:
Each side has vectors. An OV algorithm running in time would solve -SAT in time for every , and the sparsification lemma lets be linear in : SETH would be false.
What the theorem does and does not say. It rules out algorithms under a hypothesis, not unconditionally, and it does not rule out the -type speedups that exist. Even large polylogarithmic speedups, though, would imply new circuit lower bounds (Abboud, Hansen, Vassilevska Williams & Williams, STOC 2016). It concerns exact distances in the worst case: small distances, special inputs and approximation are fair game.
Approximation. Chakraborty, Das, Goldenberg, Koucký and Saks (FOCS 2018) gave the first constant-factor approximation in truly subquadratic time, about . Andoni and Nosatzki (FOCS 2020) reached near-linear time: for every , a constant-factor approximation (the constant depends on ) in time.
Key takeaways
- Every cell satisfies , and diagonals rise in steps of 0 or 1. So a distance of at most lives in a band of diagonals ( time), and each diagonal is described by where its value increases.
- Diagonal transitions compute the distance in , and with constant-time longest common extensions in (Landau & Vishkin): linear time for small distances.
- Word-level parallelism speeds up the table: the Four Russians reach (Masek & Paterson), and Myers's bit vectors use word operations.
- Under SETH, through orthogonal vectors, no algorithm exists (Backurs & Indyk, 2015), and big polylogarithmic speedups would imply circuit lower bounds; approximation escapes the barrier, with constant factors in near-linear time (Andoni & Nosatzki, 2020).
Check yourself
In the interactive lesson these are scored with your confidence, and what you miss comes back for review.
If , which cells can an optimal alignment path visit?
- Only cells on the main diagonal
- Only cells with
- Only cells with
- Any cell: the band only helps on random strings
Show the answer
B. Only cells with
Reaching costs at least horizontal or vertical steps, each worth 1, so a cell off the band already costs more than . This is why a band of diagonals suffices.
Why does the diagonal-transition algorithm only need the furthest row reached on each diagonal for each cost?
- Because values along a diagonal never decrease and rise by at most 1 per step, so each diagonal is a staircase described by where it steps up
- Because optimal paths never use insertions
- Because the alphabet is finite
- Because the furthest row always holds the smallest value
Show the answer
A. Because values along a diagonal never decrease and rise by at most 1 per step, so each diagonal is a staircase described by where it steps up
By the diagonal lemma, the cells of diagonal with value at most form a prefix of the diagonal. Recording where that prefix ends, for each , describes the whole diagonal, and one edit plus free matches moves from cost to cost .
What does the Backurs–Indyk theorem say?
- Every algorithm for edit distance needs time
- If edit distance has an algorithm for some , then the Strong Exponential Time Hypothesis is false
- Edit distance is NP-hard
- Approximating edit distance within any constant factor requires quadratic time
Show the answer
B. If edit distance has an algorithm for some , then the Strong Exponential Time Hypothesis is false
It is a conditional, fine-grained statement: a strongly subquadratic algorithm would, through orthogonal vectors, solve -SAT in time for every . It does not rule out , which Masek and Paterson achieve, and approximation escapes it altogether.
Myers's bit-vector algorithm compares a pattern of length with a text of length on a 64-bit machine. About how many word operations does it use?
- : a constant number per text character
- , like the table
Show the answer
A. : a constant number per text character
A column of the table, one text character against all pattern positions, is held as bit vectors of vertical differences, and advancing to the next column takes a constant number of word operations. In general the cost is , about times fewer steps than the table.
Further reading
- R. A. Wagner & M. J. Fischer, "The string-to-string correction problem," Journal of the ACM 21(1) (1974), 168–173.
- E. Ukkonen, "Algorithms for approximate string matching," Information and Control 64 (1985), 100–118.
- G. Myers, "A fast bit-vector algorithm for approximate string matching based on dynamic programming," Journal of the ACM 46(3) (1999), 395–415.
- A. Backurs & P. Indyk, "Edit distance cannot be computed in strongly subquadratic time (unless SETH is false)," STOC 2015.
- A. Andoni & N. S. Nosatzki, "Edit distance in near-linear time: it's a constant factor," FOCS 2020.