Proofs: DC3 algorithm
These are the human proofs of DC3 algorithm, at full length.
DC-v sorts the sample by recursion and all suffixes by one comparison through the sample full
This proof shows: correctness.
We use the pseudocode and notation of the page. Let have length , with for and for . Write for the suffix , read with its trailing zeros, and for the lexicographic order; for , , since has its first zero at offset . The proof is by induction on : we assume that DC returns the suffix array of every shorter string, and the base case sorts directly.
Lemma 1 (the first difference of two sample suffixes). Let be in , and let be the least with . If is in class and is the last position of class in , then .
Proof. The position is the largest with , so and . The first zero of is at offset , and has a nonzero letter there unless ; in the same way has a nonzero letter at offset unless . Since , the two first zeros are at different offsets, so , and .
Lemma 2 (the sample ranks). After line L5, for , if and only if , and the ranks are .
Proof. Line L2 gives equal names to equal -tuples and orders the names like the tuples. If all names differ, the order of the names is the order of the first letters, which decides each comparison, so the names are the ranks. Otherwise let be the string of names of line L4, and let be the place of in . For in class , the letters of from up to the end of the block of class are the names of , which are the names of the consecutive -blocks of up to offset . Compare and , with as in Lemma 1, and let . The blocks of and are equal, so they have the same names, and block differs at its letter , so the names of block differ in the same direction as and . By Lemma 1 applied to and to , block of each suffix is still inside the block of its class in . So the suffixes of at and first differ at offset , in the same direction as and . is shorter than when , since , so by induction line L4 returns its suffix array, and line L5 ranks in the order of the .
Set for . This is consistent with the order: such a is never compared with a rank unless the letters before it already include a zero at a place where the other suffix has a zero too, which we treat in Lemma 3.
Lemma 3 (the comparison). Let , , and let be such that and are in classes of . Then if and only if in lexicographic order.
Proof. is followed by , and the same for . If the first letters differ, both sides are decided by them. If they are equal, then neither contains a zero: a zero at offset in both would put both first zeros at the same offset, so . Hence and , both are in , , and the comparison of and is the comparison of and , which by Lemma 2 is that of their ranks.
Line L6. For a position of class , is in a class of , so by Lemma 3 with in the same class and , sorting by the key sorts the class in the order of the suffixes. The radix sort is correct, and the keys of distinct positions differ by Lemma 3, so the order is strict.
Lines L7 and L8. For classes and , is a difference cover, so there are with . With , and , both in , so the least such exists and is defined. By Lemma 3, the comparison of line L8 is the order of the suffixes, for any two positions. Merging lists that are sorted in this order, with this comparison, gives all positions in the order of their suffixes, which is the suffix array.