On this page
Type "recieve" and your phone offers "receive". Part of what sits behind that is one question: how many single-letter changes separate two words? Here is the method, by hand, on KITTEN and SITTING.
What is edit distance (Levenshtein distance)?
The NIST Dictionary of Algorithms and Data Structures defines Levenshtein distance as the smallest number of insertions, deletions and substitutions required to change one string into another. The same entry says it is also known as edit distance, and cites Vladimir Levenshtein's paper on binary codes (Russian, 1965; English translation, 1966).
Each operation costs 1:
Substitute one letter for another (cat → cut).
Insert a letter (cat → cart).
Delete a letter (cart → cat).
KITTEN becomes SITTING in three edits:
kitten → sitten (substitute K with S)
sitten → sittin (substitute E with I)
sittin → sitting (insert G)
Proving that two will not do is what the table is for.
How do you fill the edit distance table?
Robert Wagner and Michael Fischer described this method in the Journal of the ACM in 1974; their abstract says the algorithm runs in time proportional to the product of the two string lengths. Write KITTEN down the side and SITTING across the top, each with an empty start. The cell in row i, column j holds the edit distance between the first i letters of KITTEN and the first j letters of SITTING.
The rules:
Top row: 0, 1, 2 ... 7. Turning nothing into j letters takes j insertions.
Left column: 0, 1, 2 ... 6. Turning i letters into nothing takes i deletions.
Every other cell is the smallest of three numbers: the cell above plus 1 (delete), the cell to the left plus 1 (insert), and the diagonal cell plus 0 if the two letters match, or plus 1 if they differ (substitute).
K against S: diagonal 0 + 1 = 1 beats above and left (2 each), so 1. I against I match, so copy the diagonal: 1. Row by row (columns S I T T I N G after the empty start):
Empty: 0 1 2 3 4 5 6 7
K: 1 1 2 3 4 5 6 7
I: 2 2 1 2 3 4 5 6
T: 3 3 2 1 2 3 4 5
T: 4 4 3 2 1 2 3 4
E: 5 5 4 3 2 2 3 4
N: 6 6 5 4 3 3 2 3
The bottom-right cell is the answer: 3. Because every cell took the cheapest of all possible last moves, no route with two edits exists.
How do you trace back the edits?
Walk back from the bottom-right corner, asking which neighbour each cell came from.
Bottom-right is 3 (N against G). The cell to its left is 2, and 2 + 1 = 3, so the last edit was insert G.
At N against N the letters match, so step diagonally to 2 at no cost.
At E against I the diagonal is 1, and 1 + 1 = 2: substitute E with I.
T against T, T against T and I against I all match: step diagonally through the 1s.
At K against S the diagonal is 0, and 0 + 1 = 1: substitute K with S.
That is the three-edit list from the start. When neighbours tie, either choice gives an equally short set of edits.
Where is edit distance used?
Spell checkers. Fred Damerau's 1964 method in Communications of the ACM assumed a misspelled word has at most one error: a wrong, missing or extra letter, or a single transposition (two neighbouring letters swapped). On a test run of garbled text, his abstract reports correct identifications for over 95 percent of these error types. Note the swap: plain Levenshtein counts "teh" → "the" as 2 edits, while a distance that also allows Damerau's swap counts it as 1.
DNA and proteins. In 1970 Saul Needleman and Christian Wunsch published a method for finding similarities between protein sequences90057-4) in the Journal of Molecular Biology. Sequence alignment of this kind uses a similar table: a substitution lines up two different letters, and an insertion or deletion becomes a gap. For Class 12, our guide to genomics, proteomics and bioinformatics shows where alignment fits, and how a molecular clock dates species splits from DNA shows what biologists do with the differences.
How can you practise edit distance?
Fill the table for SUNDAY → SATURDAY without looking at the KITTEN example. You should get 3.
Trace back and apply the three edits to check.
Write the cell rule from memory in one line.
The same fill-from-smaller-answers idea drives the forward algorithm for hidden Markov models. If dynamic programming still feels shaky, the Fermi Concept Library has every concept in physics, chemistry, biology, mathematics and computing, each with its own lesson and its place in the map. And if your table ends on a different number, the Fermi Problem Library has worked problems with a step-by-step solver that explains the reasoning rather than printing an answer.



