Proofs: Levenshtein automaton
These are the human proofs of Levenshtein automaton, at full length.
The capped rows form a deterministic automaton for the words within distance k full
This proof shows: correctness.
Indices are 0-based. Fix a word of length and , and let . For a string let for , in the Levenshtein model with unit costs, and let be the capped row. For a row and a symbol let be the row of lines L3 to L5: and for .
Lemma 1 (the uncapped step). For each string and symbol , and, for ,
Proof. is the entry in row and column of the Wagner-Fischer table of and , and appending to adds one column. The first line is , and the second is the Wagner-Fischer recurrence for the new column, whose correctness is proved on that page.
Lemma 2 (capping commutes with the step). For every string and symbol , .
Proof. For integers and , : if both sides are , and else they are equal. Also . Induction on : by Lemma 1. For , assume . Each of the three terms in line L5 has the form with , where is the matching term of Lemma 1. Taking the minimum with and using the two identities gives .
Theorem 1 (the automaton). Let the states be the capped rows, the start state with , the transition , and the accepting states the rows with . Then the state after reading is , and the automaton accepts exactly when .
Proof. , so the start state is (line L2). By Lemma 2 and induction on , the state after is . It is accepting exactly when , that is, when (line L7). Lines L6 and the memo table only give each distinct row one id, which does not change the function from inputs to rows.
Lemma 3 (the dead state). exactly when some string has .
Proof. Suppose for some . Let . An alignment of with of cost , followed by matches, aligns with , so . Conversely, suppose all entries of equal . Then gives , and by induction on each term of line L5 is at least , so . So the row of all maps to itself on every symbol, and it is not accepting. By Theorem 1, no extension has .
So line L8 returns false exactly for the dead state, and the walk of lines L9 to L12 loses no answer when it drops a trie node whose state is dead: every word below that node extends the string of the node.
Theorem 2 (the lookup). LOOKUP reports each word of the dictionary with exactly once, and no other word.
Proof. The trie has one node for each prefix of a dictionary word, and a node is marked when its string is a word. Each entry pushed on the stack has for the string of node : this holds for the root with , and line L12 pushes the child of by symbol with by Lemma 2. Each node is pushed at most once, because it has one parent. Line L11 reports exactly when is marked and , so every report is correct by Theorem 1 and no word is reported twice. For completeness, let be a word with . For each prefix of , extends , so is not dead by Lemma 3, and line L12 pushes the node of when it pops its parent. By induction on the length of the prefix, the node of is pushed and popped, and line L11 reports .
Search mode. In search mode line L3 sets . With , the column of the Sellers table of against the text , Lemma 1 holds with in place of , by the Sellers recurrence, and the proof of Lemma 2 goes through unchanged for . So after reading the state is the capped column , and it is accepting exactly when , which is the condition for the end position proved on the Sellers page.
Theorem 3 (the number of states). The automaton has at most reachable states.
Proof. Let . Each alignment of with needs at least insertions or deletions, so . If , then every has , so is the dead row. Else . For the entry is , so is fixed by and the entries with in the window , which has at most indices. Deleting turns an alignment of with into one of with at one more unit of cost, and the reverse insertion works the same way, so . Capping keeps this bound, since does not increase distances between integers. So the window is fixed by its first entry, one of the values to , and at most differences in . This gives at most rows for each of the values of , plus the dead row.