Proofs: Wagner-Fischer algorithm
These are the human proofs of Wagner-Fischer algorithm, at full length.
The Wagner-Fischer table holds the Levenshtein distances of all prefix pairs full
This proof shows: correctness.
Indices are 0-based. Let have length and length . The distance is the least number of unit-cost insertions, deletions and substitutions that turn into . Let be a gap symbol that is not in , and let for . This is the discrete metric, so for all three symbols.
An alignment of and is a sequence of columns over , with no column , such that with the gaps removed is and with the gaps removed is . Its cost is . Let be the least cost of an alignment of and .
Lemma 1. After lines L1 to L6, for all and .
Proof. By induction on , in the order of the loops, which compute , and before . The only alignment of with the empty string has columns , so , which line L1 stores. Line L2 is the same for . Let , and consider an alignment of and . Its last column is one of three kinds: , or . If we remove the last column, we get an alignment of and , or of and , or of and , and each alignment of these pairs extends by the matching column. So of the pair is the least of over the three kinds. The three costs of the last columns are , 1 and 1. By the induction hypothesis, this is the minimum of line L6, with from line L5.
Lemma 2. .
Proof. Take an alignment of cost . Process its columns from the last one to the first one. A column with needs no operation. A column with and is a substitution of the character of in this column by . A column is a deletion of that character, and a column is an insertion of at this place. The columns to the left are not changed by an operation on a later column, so their characters keep their positions. At the end the string reads . The script has one operation for each column with , so its length is the cost of the alignment.
Lemma 3. for all strings , , .
Proof. Let be an optimal alignment of and and an optimal alignment of and . Each character of is in one column of and in one column of . We build a sequence of triples . For the -th character of , take the triple , where is its column in and is its column in . A column of gives the triple , and a column of gives the triple . Order the triples by the order of : before the triple of the -th character of , and after the triple of the -th one, put the triples of the columns of that lie between these two characters, in their order in , then those of , in their order in . Then the first entries, read in order, spell with gaps, and the last entries spell with gaps. Remove the triples whose first and last entries are both . The pairs (first entry, last entry) of the other triples form an alignment of and . For each triple, . The term is the cost of the column of in the triple, or 0 for a triple , and the same holds for . Each column of and of is in exactly one triple. So the cost of is at most the cost of plus the cost of .
Lemma 4. .
Proof. Let be a script of length , where each comes from by one operation. For one operation, : align the unchanged characters with themselves, and add one column , or for the changed character. By Lemma 3, applied times, . For , and the alignment of each character with itself costs 0.
Theorem. WAGNER-FISCHER returns , and TRACEBACK returns an alignment of cost .
Proof. By Lemmas 2 and 4, . By Lemma 1, line L7 returns .
For TRACEBACK, we use this invariant at line L9: the columns in form an alignment of and of cost . It holds at line L8, where is empty and . In a step, if line L10 applies, the column costs . If line L11 applies, the column costs . If neither applies, then and : when , this is ; when , line L11 applies, because ; when , line L6 makes equal to one of its three terms, and the first two are excluded. So line L12 adds a column of cost . In each case the invariant holds again, and drops by 1 or 2. So the loop stops after at most steps, with , and is an alignment of and of cost .