Proofs: Booth's algorithm
These are the human proofs of Booth's algorithm, at full length.
LEAST-ROTATION returns the least index of a least rotation full
This proof shows: correctness, termination.
Notation. with is a word over a totally ordered alphabet, with 0-based indices. Let , and let be the infinite word , so and for . For let . Then , and are the rotations of . Words are compared in lexicographic order. For a nonempty word and , is the length of the longest border of , minus one. A border of is always proper here: a word that is both a proper prefix and a proper suffix of , possibly empty.
Lemma 1 (first difference). Let with and . Then ; and if , while if .
Proof. If , then , that is, for . Both sides depend only on , so the equality holds for every , in particular for , a contradiction. So , and the words and of length agree before offset and differ at it.
Lemma 2 (borders of a border). Let be a border of . A word shorter than is a border of if and only if it is a border of .
Proof. A word shorter than is a prefix of if and only if it is a prefix of , because is a prefix of ; the same holds for suffixes, because is a suffix of .
Call an index decided for the candidate if .
Outer invariant. At the start of the iteration for , where and means after the loop, let . Then:
- (I1) , and for .
- (I2) Each with and is decided, or has with a nonempty border of .
Initialization. At , , and . The only index below 1 is itself.
Inner invariant. Fix an iteration and let . At each test of line L4, with the current values of and and with :
- (J1) , for , and is the length of a border of .
- (J2) Each with is decided, or has with a nonempty border of of length at most .
- (J3) Each nonempty border of longer than has .
First test. Line L3 sets , so is the length of the longest border of . Then (J1) and (J2) follow from (I1) and (I2), and (J3) is empty.
One pass of lines L5 and L6. The test passed, so and , where exists because . Let , a border of , and . Then , and and first differ at offset , where meets .
- If , Lemma 1 gives , so becomes decided. Line L6 sets to , so becomes the length of the longest border of . The undecided indices of (J2) other than have borders of shorter than , which by Lemma 2 are borders of , so their length is at most the new . By Lemma 2, the borders of longer than the new are those longer than the old , and ; all of them have . So (J1) to (J3) hold.
- If , Lemma 1 gives , and line L5 sets to . The new word is , a prefix of the old , so for . Each index decided for the old candidate is decided for , since , and so is itself. Each other undecided index has a border of the old shorter than , so , and by Lemma 2 its border is a border of , the new . Line L6 sets to , the length of the longest border of , minus one. So (J1) and (J2) hold, and (J3) is empty, because no border of is longer than its longest border.
Each pass decreases , and , so the inner loop stops.
Maintenance of the outer invariant. At the exit from the inner loop, (J1) to (J3) hold, and either with , or .
Case A: and . Line L7 fails and line L10 sets . Let , the word of the iteration .
- The table. A nonempty border of has the form , where is a border of with . By (J3) the longest such is , so . The entries below are unchanged and stay correct, since .
- The key claim. Let be a border of with and . Then . Indeed, is a border of by Lemma 2, so ends , and is a prefix of . So occurs in at the index , where and . By (J2), is decided, or is a border of . In the second case is a prefix of that covers the index , so , which is excluded. In the first case ; but , and at offset the letters are and , so would give by Lemma 1. Hence .
- The indices. A decided index stays decided. An undecided has a border of of length at most . If , then is a border of . If , then (for the letter is ), so by the key claim, and Lemma 1 at offset gives . The new index is the case : if , then is a border of , and otherwise the key claim with gives , so .
So (I1) and (I2) hold at .
Case B: . By (J2) every other than is decided, and by (J3) every nonempty border of has . So the only possible nonempty border of is itself, which is a border exactly when . The new index meets at offset 0.
- If , line L10 sets , and is undecided with the border of .
- If , line L9 sets , and by Lemma 1.
- If , then by Lemma 1, and line L8 sets . Each has , so it is decided for . The new is , and line L9 sets , which is correct.
In all cases (I1) and (I2) hold at . The outer loop runs times and each inner loop stops, so the algorithm terminates.
The output is a least rotation. At the end, and . Let with . By (I2), , or and is a border of . In the second case this border has length and is a prefix of , so its first letters, , are the first letters of , which are . So for every , and is a least rotation.
The output is the least such index, and . Let be the least index in with minimal; since , . Each change of , at line L5 or L8, is to an index with a strictly smaller rotation, by the cases above. So once , it never changes. Suppose the final value differs from . Then is minimal, as shown, so by the choice of , and was set to at some step of some iteration , from a candidate with . Then and . At the test of line L4 just before this step, (J2) holds, and is not decided for , since . So is a nonempty border of of length at most . If the step is line L5, then and , a contradiction. If the step is line L8, then , and no nonempty border has length at most 0, a contradiction. So : the algorithm returns the least index of a least rotation, and it lies in .