Proofs: Apostolico-Giancarlo algorithm
These are the human proofs of Apostolico-Giancarlo algorithm, at full length.
Apostolico-Giancarlo outputs exactly the valid shifts full
This proof shows: correctness, termination.
Claim. For a text and a pattern , APOSTOLICO-GIANCARLO stops and outputs exactly the shifts , , with , in increasing order.
For , line L2 outputs every shift, which is correct, so let . We take as given the tables of the page: is the largest with , is the last position of in or , and is the strong good-suffix table. That GOOD-SUFFIX computes , that is the smallest period of , and that the shift of lines L13 and L14 skips no valid shift, is proved on the page of Boyer-Moore (its Lemma 2), for a mismatch at a position with and .
For a text position , let be the largest with : the length of the longest suffix of that ends at .
Invariant (I). At the start of each window , every entry with is either 0 or equal to , and in the second case . (With the array indexed by , this is the statement about the entry read for .)
Initially all entries are 0. Entries are written only at line L13 or L14 for ; entries for positions that leave the window are cleared at line L15 before a new position with the same residue enters, because the positions entering, , have the same residues as the cleared (all residues if ). So (I) holds if each written value equals and is positive or 0; we prove this in Lemma 2.
Lemma 1 (the scan). Assume (I) at window . The loop of lines L7 to L12 keeps the invariant and ends either with , when is valid, or with and .
Proof. The invariant holds at . Consider one step at position , with and .
If , line L12 compares with and either extends the invariant by one position or ends with a mismatch at .
If , then by (I), so , and if and , then by maximality. Also , and if then by maximality.
- Case , . Then , because and the suffixes of length agree, and because . With the invariant, the window is an occurrence, and line L10 sets .
- Case , . The text and both end with the suffix of of length , so . At position : since , , while . So the invariant holds down to , and is a mismatch.
- Case . Both end with the suffix of length , so the text matches . At position (since ): since , ; and with , so . So is a mismatch.
- Case . The text matches , and line L11 sets and goes on, which keeps the invariant.
Each step decreases or ends the loop, so the loop stops.
Lemma 2 (the note). The value written at line L13 or L14 is .
Proof. At line L13 the window is an occurrence, so . At line L14, by Lemma 1, and , so the longest suffix of ending at has length exactly .
Proof of the claim. By Lemma 2, (I) holds at every window. By Lemma 1, line L13 outputs exactly when is valid, and otherwise line L14 runs with a true mismatch at and the text matching , which is the situation of Boyer-Moore; its shift, and the shift after an occurrence, are at least 1 and skip no valid shift. So increases at each window, every valid shift is tested, and only valid shifts are output, in increasing order. The loop stops once , after at most windows.