Proofs: CAT necklace generation
These are the human proofs of CAT necklace generation, at full length.
GEN 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.
The calls. We show: for each with , the call GEN is made exactly once for each prenecklace of length , at a moment when , and then . Moreover, while a call GEN and the calls below it run, the cells do not change, since the call writes only and its descendants write only higher cells.
Level 1. Line L1 sets to each letter in turn and calls GEN. Every one-letter word is a Lyndon word, hence a prenecklace with , and there are no other prenecklaces of length 1.
From to , for . A prenecklace of length is , where is a prenecklace (a prefix of a prenecklace) and a letter. By the hypothesis, GEN runs exactly once with ; it reads , which stays fixed during the call. By Lemma 5, Lemma 4 applies to with : the extensions that are prenecklaces are exactly those with , with for and for . Line L5 sets and calls GEN, and lines L6 and L7 set and call GEN for each from to . So each prenecklace gets exactly one call, with the right value of , and no other word of length gets a call.
Order. The calls form a tree in which the children of a call are made with increasing letters at index . The words of length are reached in the order of a depth-first walk that takes children in increasing order, which is increasing lexicographic order: two leaves first differ at some index , below the call for their common prefix, and the leaf with the smaller letter at is under the earlier child.
Termination. Each call makes at most calls, and the depth is at most , so the tree is finite. The number of calls is , where is the number of prenecklaces of length .
The tests of line L3. 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, . 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 is a prenecklace, so each Lyndon word of length is visited.
The number of calls. By Lemmas 3 and 5, a prenecklace of length with is for exactly one Lyndon word of length , and each Lyndon word of length gives one. So , where is the number of Lyndon words of length , and the number of calls is
The rotations of a Lyndon word of length are distinct words, so . Put . For , , and these terms sum to at most for . For , each term is at most , and there are at most of them. The number of necklaces of length is at least , since each necklace class has at most words. So the number of calls is at most , and . Each call does work besides the calls it makes, so the total time is for .