Proofs: String-matching automaton
These are the human proofs of String-matching automaton, at full length.
The string-matching automaton reports exactly the occurrences full
This proof shows: correctness, termination.
Claim. If for every state and every character , then AUTOMATON-SEARCH stops after steps and outputs exactly the shifts with , in increasing order. That the construction produces this table is proved on the page of the construction.
Here is the length of the longest prefix of that is a suffix of . It is at most and at most .
Lemma 1. For any string and character , .
Proof. Let . If there is nothing to show. Otherwise is a suffix of , so is a suffix of , and .
Lemma 2. If , then .
Proof. Let , a suffix of , so is a suffix of . Any prefix of that is a suffix of is then a suffix of , so . Conversely, let . By Lemma 1, . The strings and are both suffixes of , and the first is not longer, so it is a suffix of . So .
Theorem. The claim holds.
Proof. Assume ; for , line L2 outputs all shifts , and each is valid because the empty string occurs everywhere. We show by induction on that after line L5 in the iteration for , . Before the loop, of the empty string. If before line L5, then line L5 sets by Lemma 2.
Now exactly when is a suffix of , that is, when . So line L7 outputs the shift exactly when it is valid. Each valid shift is output in the iteration , and the shifts are output in increasing order of , hence of . When , the state never reaches , because . The loop has iterations of constant work, so the algorithm stops.