Proofs: Knuth-Morris-Pratt algorithm
These are the human proofs of Knuth-Morris-Pratt algorithm, at full length.
KMP outputs exactly the valid shifts full
This proof shows: correctness, termination.
Claim. For a text and a pattern with , KMP-MATCHER stops and outputs exactly the shifts , , with , in increasing order. (For , line L2 outputs all shifts , which is the definition.)
Setup. For a string , let be the length of the longest prefix of that is a suffix of and has length less than . Let be the prefix function of . We use two facts proved on that page.
- (F1) The borders of , for , have the lengths , and no other lengths.
- (F2) A border of a border of is a border of , and a border of shorter than another border of is a border of .
Lemma 1 (the table). For , line L16 sets to the largest such that is a border of and , or to if there is none. Also , which is the same rule for (no exists).
Proof. By strong induction on . Let , the longest proper border length of . If , then is the largest candidate, and line L16 sets . If , then is not a candidate, and by (F1) and (F2) the remaining candidates are exactly the lengths such that is a border of and . By the induction hypothesis for , the largest of them is , or if there is none.
Lemma 2 (one text character). Let be a string, a character, and . Run lines L6 to L8 with this and . Then they stop, and afterwards $q = $ the length of the longest prefix of that is a suffix of (this length may be ).
Proof. Let be the set of lengths with such that is a suffix of . A prefix with is a suffix of exactly when is a suffix of and . Each such satisfies by the definition of , so . And for , is a suffix of exactly when it is a border of or equal to it, by (F2) applied to the suffixes of . So the answer is , or if that set is empty.
The loop of line L6 keeps the invariant: , or and no with has . It holds at the start. If and , line L7 sets to . By Lemma 1, each with has , so the invariant holds for the new value. Each pass decreases by at least 1 and , so the loop stops after at most passes. At the exit, either and , so is the largest with , or and no has . Line L8 adds 1, which gives the answer in both cases.
Theorem. The claim holds.
Proof. We show by induction on that at the start of the iteration for (and at the end, for ):
(I) , and the output so far is the increasing list of valid shifts with .
Initialization. For , the text read is empty, of the empty string is 0, and nothing is output, so (I) holds after line L4.
Maintenance. Assume (I) for . By Lemma 2, after line L8, is the length of the longest prefix of that is a suffix of . A valid shift ends at exactly when itself is a suffix of , that is when , and then the shift is , larger than all earlier outputs. So lines L9 and L10 output exactly the valid shift that ends at , if any. If , then . If , the longest prefix of shorter than that is a suffix of is the longest proper border of , by (F2), whose length is , the value set by line L11. In both cases (I) holds for .
Termination. The loop of line L5 runs times, and each inner loop stops by Lemma 2. At the end, (I) for says that the output is the increasing list of all valid shifts.
Cost. The proof of Lemma 2 shows that each pass of line L7 decreases by at least 1. The complexity section of the page turns this into the bound of comparisons.