Edit distance
This is the Levenshtein distance. The table holds, for every pair of prefixes, the fewest single-character edits that turn one into the other. Three edits are allowed and each corresponds to one neighbour: delete a character from the first string, which is the cell above; insert one from the second, which is the cell to the left; or substitute one for the other, which is the cell diagonally back. Where the two characters already agree, the diagonal costs nothing.
Its recurrence
D(i, j) = a[i] = b[j] ? D(i−1, j−1) : 1 + min( D(i−1, j), D(i, j−1), D(i−1, j−1) )
When it is the right choice
Spelling correction, fuzzy search, "did you mean", record deduplication, and measuring how far apart two versions of a sequence are. It is also the honest way to score an approximate match where a pattern matcher would say only yes or no. Its cost is the product of the two lengths, so comparing long documents wants a banded or a heuristic variant rather than the whole table.
How it works
- Make a table with one row per character of the first string plus a row for the empty prefix, and the same for the second down the columns.
- Fill the first row and the first column with 0, 1, 2 and so on: turning a prefix into nothing costs one deletion per character.
- For each pair of characters, read all three neighbours: above, left, and diagonally back.
- If the characters match, take the diagonal unchanged — no edit is needed.
- Otherwise take the smallest of the three and add one, and remember which edit that was.
- Walk back from the bottom-right, reading each step as the edit it stood for.
Worth noticing
- Three arrows, not two, and that is the entire difference from the longest-common-subsequence page, which draws the same table over the same two strings. Load the same pair of words on both and step through them together.
- A diagonal step where the characters match carries no cost at all, so the number in the cell does not change. That is the only arrow in this tool that adds nothing, and it is why a long shared run shows as a diagonal of equal numbers.
- The traceback is a list of instructions rather than a set of chosen things: insert this, delete that, substitute the other. Applying them in order genuinely turns the first string into the second, which the test suite checks rather than asserting.
Cost
- Best
- O(m · n)
- Average
- O(m · n)
- Worst
- O(m · n)
- Extra space
- O(m · n)
- Table
- two dimensions
- Accepts
- up to 16 characters in each string
The limits are pedagogical rather than technical: this tool keeps the whole table in every recorded step, so the shape of the table sets the size of the trace directly, and a run has to stay short enough to sit and watch. An input that would take more than three thousand steps on this algorithm is refused before a single step is generated, from a formula in the input sizes rather than by running it and finding out.
An unfilled cell is drawn differently from a cell holding a zero — no fill, a dashed outline, and no number at all — because early in a run that difference is most of the picture.