Definition · Edit distanceThe edit distance between strings a and b is the fewest single-character operations — insert, delete, substitute — that turn a into b. Each costs one; keeping a character costs nothing and is not an operation in the count, though it is a step in the script.
Theorem · The edit recurrenceWith D(i, 0) = i and D(0, j) = j, for i, j ≥ 1: D(i, j) = min( D(i−1, j) + 1, D(i, j−1) + 1, D(i−1, j−1) + c ), where c is 0 if the i-th character of a equals the j-th of b and 1 otherwise.
Consider a cheapest sequence of operations turning a[1..i] into b[1..j] and look at what happens to the last character of each. Either a[i] is deleted, in which case the rest is a cheapest way of turning a[1..i−1] into b[1..j], costing D(i−1, j) + 1; or b[j] is inserted, giving D(i, j−1) + 1; or a[i] is aligned with b[j], kept if they are equal and substituted otherwise, giving D(i−1, j−1) + c.
In each case the remaining sub-sequence must itself be cheapest, or substituting a cheaper one would beat the original — optimal substructure again, discharged by the same substitution argument as every other page on this course. No other last step is possible, so the minimum over three cases is the distance.
□
Filling this table in row-major order is correct, and it is worth pausing on why, given “The Fill Order Is Part of the Algorithm”. The three cells a cell reads are (i−1, j), (i, j−1) and (i−1, j−1): one row up, one column left, and both. Row-major writes every cell of row i−1 before any of row i, and within row i it writes column j−1 before column j. All three reads are therefore already written, and the count of reads landing on unwritten cells is zero. The fill order is not always wrong; it is always something to check.
Example · Kitten to sitting, three waysThe table is 7 by 8, fills 56 cells and makes 126 reads, and its corner is 3. A recursion with no table at all reaches 3 after 29 737 calls. The script the pointers spell out is: substitute k for s, keep i, keep t, keep t, substitute e for i, keep n, insert g. Performing that on kitten, character by character, produces sitting, and the panel compares that string with the second word rather than trusting the walk.
Why the script is carried out rather than added up
Suppose the walk back followed one wrong pointer and returned a script that still had three paid operations. Costing it would give 3, matching the table, and the check would pass. Performing it would not: a delete that names the wrong character, or a keep whose character is not where the script says, stops the execution and the panel reports that the script could not be performed. The stronger check is available because the object here is a program, and a program can be run.
This generalises past strings. Whenever a reconstruction is a sequence of steps rather than a set, executing it against the input is strictly more informative than re-costing it, and usually no harder to write. The knapsack's set could only be re-weighed; this script can be run.
Two counts, and the bound that is loose
The measured figures are 56 cells against 29 737 calls on two words of six and seven letters. The proved bounds are Θ(mn) for the table and, for the memo-free recursion, at most 3^(m+n) calls because each call makes three and the arguments shrink by at least one in total. For kitten and sitting that upper bound is 3^13, which is 1 594 323 — true, and roughly fifty times the measured 29 737.
That gap is worth naming rather than smoothing over. The bound is an over-count because many branches hit a base case early and because the three recursive calls do not all reduce the same amount. A measurement cannot repair a loose bound; it can only tell you that on this input the truth is well below it. Change the pair to sunday and saturday and the measured count is 60 121 against a bound of 3^14, so the ratio moved without either number being wrong.
One more count that does not mean what it looks like: the panel reports seven operations in the script and a distance of three. Four of the seven are keeps. A reader who quotes the script's length as the distance has reported the length of a path instead of the cost of one, and on dog to dogma the two numbers are five and two.