Proofs: FKM algorithm
These are the human proofs of FKM algorithm, at full length.
FKM visits every prenecklace of length n once, in increasing order, with p = lyn(a) full
This proof shows: correctness, termination.
Notation. The alphabet is with , ordered as integers, and words are compared lexicographically. Indices are 0-based. For a nonempty word , is the infinite word , and is its prefix of length . A Lyndon word is a nonempty word strictly smaller than each of its proper nonempty suffixes; equivalently, strictly smaller than its other rotations. A word is a prenecklace if it is a prefix of for some Lyndon word . Prefixes of prenecklaces are prenecklaces. For a nonempty word , is the length of the longest prefix of that is a Lyndon word; it is at least 1, since a single letter is a Lyndon word.
Lemma 1 (suffixes of a prenecklace). If is a prenecklace of length and , then .
Proof. Let be a prefix of with Lyndon, and let and the rotation . The suffix of from index is . Since is a Lyndon word, , and both have length ; so either , or they differ inside their first letters with smaller. In both cases each prefix of is at most the prefix of of the same length. The words and are these prefixes of length .
Lemma 2 (Duval). Let be a Lyndon word of length , let with , let and let be a letter with . Then is a Lyndon word.
Proof. Let and write with . We show .
- Case , so . The suffix is . It agrees with on its first letters, and the next letters are in the suffix and in , since . As , the suffix is larger.
- Case and . The suffix starts with , a proper suffix of , so . A shorter word that is larger than is not a prefix of , so there is with and . Then the suffix and first differ at offset , and the suffix is larger.
- Case and . Then and the suffix is , of length , while starts with . Take as in the previous case. If , the suffix is larger at offset . Otherwise and (equality when , strict when ), so , and the suffix is larger at offset .
Lemma 3 (the longest Lyndon prefix of a periodic word). Let be a Lyndon word of length and . Then .
Proof. The prefix is Lyndon. Let with , and write with and a proper prefix of . If is nonempty, it is a proper nonempty suffix of and also a prefix of , so , and is not Lyndon. If is empty, then and the proper suffix is a prefix of , so again is not Lyndon.
Lemma 4 (extension). Let be a nonempty prenecklace of length with , and suppose with . Let be a letter and . Then:
- if , is a prenecklace, and ;
- if , is a Lyndon word, so it is a prenecklace with and ;
- if , is not a prenecklace.
Proof. Write with and ; then and , because has period . If , then , a prenecklace, and by Lemma 3. If , is Lyndon by Lemma 2; a Lyndon word is a prefix of , and . If , the suffix of at index is , and the prefix of of the same length is , which is larger. By Lemma 1, is not a prenecklace.
Lemma 5 (structure). Every nonempty prenecklace of length satisfies with .
Proof. Induction on . For , and . For , is a prenecklace, the induction hypothesis applies to it, and since is a prenecklace, Lemma 4 leaves the first two cases, which both give the claim.
So Lemma 4 applies to every nonempty prenecklace. In particular, for the map is a bijection from the Lyndon words of length onto the prenecklaces of length with : it lands there by Lemma 3, it is onto by Lemma 5, and is the prefix of length of its image.
Lemma 6 (successor). Let be a prenecklace of length other than , let be the largest index with , and let . Then is a Lyndon word, is a prenecklace with , and is the least prenecklace of length greater than .
Proof. If , is one letter, a Lyndon word. If , let , a nonempty prenecklace, and . Since is a prenecklace, Lemma 4 gives , so , and by Lemma 4 again is a Lyndon word. Then is a prefix of , a prenecklace, and by Lemma 3, since .
The word is greater than , since they first differ at index . Let be a word of length with . The letters of after index are all , so first exceeds at some index . If , then and ; so , and , since a larger letter would again give . Hence begins with , and since , there is an index with and . The prenecklace has by Lemma 3, and . By Lemma 4, is not a prenecklace, so neither is .
The loop invariant. Each time line L3 is reached, is a prenecklace of length and .
Initialization. Line L1 sets , a prenecklace because is a Lyndon word, and by Lemma 3.
Maintenance. Lines L4 and L5 stop at the largest with , or at if there is none. If , line L7 makes as in Lemma 6 and sets , and line L8, running upward, sets for , so that . By Lemma 6, is a prenecklace with .
Each prenecklace is visited once, in increasing order. The first word visited is , the least word of length . By Lemma 6, each later word visited is the least prenecklace greater than the one before it. So the visited words are increasing, and no prenecklace is skipped. The last one is , a prefix of and hence a prenecklace, after which line L6 returns.
Termination. Each pass through lines L4 to L8 strictly increases , and there are finitely many words of length . Each inner loop runs at most times.
The tests of VISIT. Let be a prenecklace of length with and , so by Lemma 5.
- If divides , is a necklace. Then . A rotation of by is , where is the rotation of by , and because is a Lyndon word. So is at most each of its rotations.
- If is a necklace, divides . Write with primitive. Let and let be the rotation of by . It differs from , because is primitive. The rotation of by is , and ; since and have the same length, this forces , so . So is a Lyndon word, , and by Lemma 3, which divides .
- Lyndon words. is a Lyndon word if and only if its longest Lyndon prefix is itself, that is, . Every Lyndon word of length is a prenecklace, so it is visited.