Proofs: Duval's algorithm
These are the human proofs of Duval's algorithm, at full length.
DUVAL returns the Lyndon factorization full
This proof shows: correctness, termination.
Notation. is a word over a totally ordered alphabet, with 0-based indices, and words are compared in lexicographic order, where a proper prefix is smaller. A Lyndon word is a nonempty word that is smaller than each of its proper nonempty suffixes. For a nonempty word , is the infinite power. We use two facts proved on the Chen-Fox-Lyndon theorem page: the Lyndon factorization exists and is unique, and a proper nonempty suffix of a Lyndon word is larger than and is not a prefix of .
Lemma 1. Let be a Lyndon word of length . For each , , comparing the infinite words letter by letter.
Proof. Let . Then with , a rotation of . If the two words are equal. Otherwise , because a Lyndon word is smaller than its proper rotations, and since they differ inside the first letters, with the smaller letter in . So .
Lemma 2 (extension). Let be a Lyndon word of length , , a proper prefix of , , and a letter. Then is a Lyndon word.
Proof. Let and , so is with its last letter replaced by . Take a proper suffix with . By Lemma 1, , these being prefixes of the same length of and . So either the two words are equal, or they first differ at some offset with .
- If they first differ at , then and , so agrees with at both indices, and and first differ at with the larger letter in .
- Otherwise they agree on the offsets , and at the last offset we have . Then and agree before the last offset, and at it .
In both cases , since the two first differ inside . So is smaller than each of its proper nonempty suffixes.
Lemma 3 (output step). Let , where is a Lyndon word, , is a proper prefix of , and either is empty or starts with a letter . Then the Lyndon factorization of is copies of followed by the Lyndon factorization of .
Proof. If is empty, the claim holds, since is nonincreasing. Otherwise let be the first factor of the Lyndon factorization of . If , then is a prefix of , hence a prefix of , so . If , then is nonempty and starts with , while starts with and , so . In both cases the sequence is a nonincreasing sequence of Lyndon words with product , and by uniqueness it is the Lyndon factorization of .
Outer invariant. At each test of line L2: , the list holds the start indices of factors with , and the Lyndon factorization of is followed by the Lyndon factorization of . At the first test and is empty, so this holds.
Inner invariant. At each test of line L4 in a round that starts at : , and with , the word is a Lyndon word and with and a proper prefix of . Since is a prefix of , the letter that continues the period is , and .
- First test. Line L3 sets and , so , , and is empty.
- Equal letters. If , then , and is a prefix of . If it is a proper prefix, it is the new ; if it equals , then grows by one and the new is empty. Line L5 sets and line L6 sets , so is unchanged and the invariant holds.
- Larger letter. If , then is a Lyndon word by Lemma 2. Line L5 sets and line L6 sets , so the new is , the new is , and is empty.
Each step increases , and , so the inner loop stops.
The output step. When the inner loop stops, as above, and either or . By Lemma 3 with , the Lyndon factorization of is copies of followed by the factorization of . Lines L7 to L9 append while , that is, while . Since with , they append exactly indices, the starts of the copies of , and leave at the old plus . So the outer invariant holds again. Since , each round increases , so the outer loop stops.
Conclusion. At the end , so is empty, and by the outer invariant holds the start indices of the Lyndon factorization of .