Proofs: Two-way algorithm
These are the human proofs of Two-way algorithm, at full length.
The two-way algorithm outputs exactly the valid shifts full
This proof shows: correctness, termination.
Claim. For a text and a pattern over an ordered alphabet, TWO-WAY stops and outputs exactly the shifts , , with , in increasing order.
For , line L2 outputs every shift, so let . A shift is valid when and . Write for the smallest period of .
Definitions. For a cut with (between and ), an integer is a repetition length at when for every with , and . This is the definition of a repetition of length (with and the left part suffixes of one another, and the right part prefixes of one another) written position by position. The local period at is the smallest repetition length; is always a repetition length, and the cut is critical when the local period equals .
Facts about the factorization (Crochemore and Perrin). Let be the result of CRITICAL-FACTORIZATION. Then (F1) the cut is critical; (F2) ; (F3) if then . These are the critical factorization theorem for the cut before the later of the two maximal suffixes, and two lemmas of the same paper; the test test_critical_factorization checks all three on every word of length at most 7 over three letters. We also need:
(F4) if , then . Proof. is the period of the suffix , so for , and the test gives it for . So is a period of , . Conversely is a period of the suffix, so .
So in the branch of line L4, ; in the branch of line L14, by (F3).
Lemma 1 (no occurrence close to a partial match). Let , and suppose for . Let be valid with . (a) If , or if , then . (b) If , then divides .
Proof. Let be the set of with . For , because is valid, and by the hypothesis; so .
(a) The repetition condition for at asks for with and . Such a has , and : if this is the condition , and if it follows from . So and the condition holds. By (F1), .
(b) Let for while . We show by induction on that for every . For this is shown above. For the step, let and : then and , so the period gives . Now if , the argument of (a) with in place of applies: for with , we have and , so and . So is a repetition length at the critical cut and , which means . The sequence decreases by and stays nonnegative, and it can only stop at a value that is not , so it reaches 0: divides .
Lemma 2 (right-part mismatch). If the scan of line L8 or L17 stops at with , and for , then no shift with is valid.
Proof. Let be valid with . Lemma 1(b) with gives . Then , so by the period , a contradiction.
Lemma 3 (full right part). If for , then no shift with is valid.
Proof. Lemma 1(a) with .
The memory. In the branch of line L4, the invariant is: at the start of each window , for . It holds with at the start and after line L13. After line L12, the previous window matched at least the positions where is where the left scan stopped (or and the window matched fully). The new window is and ; for , the position satisfies , so , using the period .
With this invariant, the scans of lines L8 and L10 skip only positions known to match, so in both branches: the right scan establishes for and either or a mismatch at ; and after a full right scan, the left scan ends with all of matched exactly when the window is valid (line L11 or L18 outputs exactly then).
Proof of the claim. At each window, is output exactly when it is valid. The shift is after a right mismatch, safe by Lemma 2; after a full right scan it is or , safe by Lemma 3, and at least 1. So increases at every window, no valid shift is skipped, and the output is in increasing order. Each scan moves monotonically within , so each window takes finite time, and there are at most windows. The procedure MAXIMAL-SUFFIX stops: line L25 strictly increases , which stays below , so it runs fewer than times; between two runs of line L25, each step of line L23 or L24 increases by exactly 1, and in the loop.