Proofs: DC3 (skew) algorithm
These are the human proofs of DC3 (skew) algorithm, at full length.
The DC3 algorithm sorts the suffixes in linear time full
This proof shows: correctness, termination, time.worst.
Notation. with symbols in , and . Positions are 0-based, , and the order is lexicographic with a proper prefix first. For , is the empty string. Because 0 is smaller than all symbols, comparing the padded strings gives the same order as comparing the suffixes, and two different suffixes never compare equal. Let , , : the numbers of positions with . The sample is the set of positions with . When , it contains the dummy position , whose suffix is empty.
Lemma 1 (reduced string). Let be the rank of the triple among the distinct triples of the sample, from 1. Let be the string (all sample positions , of them) followed by (all , of them). For sample positions and , if and only if the suffix of that starts at the index of is smaller than the suffix that starts at the index of .
Proof. The suffix of at the index of is up to the end of the first half, followed by the names of the second half. The run of names spells triple by triple, padded with zeros. The last triple of the first half contains a 0: if , the last position below is or , whose triple reaches index ; if , it is the dummy with triple . A triple that contains a 0 can be equal to another triple only if the zeros are at the same offsets, which happens only for the triples that end at the same place in . So when two suffixes of are compared, they differ at the latest at the first triple that contains the end of , before the comparison reaches the second half of (except for two equal triples at the very same position, which means ). For two positions the same argument applies, since the last triple of the second half also contains a 0. Comparing names in order is comparing triples in order, and comparing triples is comparing the padded suffixes.
Lemma 2 (sample ranks). After lines L4 to L7, for is the rank of among the sample suffixes, from 1, and for through the padding.
Proof. If all names are different (line L7), the triple order is the suffix order, because two suffixes that differ in the first triple compare as the triples. Otherwise line L6 sorts the suffixes of , which by Lemma 1 orders the sample suffixes; the recursion is correct by induction on , since for .
Lemma 3 (non-sample order). For , if and only if .
Proof. and or . If the first symbols decide. Otherwise and decide, and by Lemma 2 their ranks compare as they do. Line L8 takes the positions in the sorted sample order, which is sorted by , and sorts them stably by , so the result is sorted by the pair.
Lemma 4 (merge). For and :
- if : if and only if , because and are both in the sample or beyond the end;
- if : if and only if , because and are both in the sample or beyond the end.
Proof. As in Lemma 3, peel one or two symbols and compare the rest by the sample ranks. A rank of 0 belongs to an empty suffix and is smaller than all others. The pairs or triples are never equal for , since the suffixes are different.
Theorem 1 (correctness). DC3 returns the suffix array of .
Proof. The sample suffixes are sorted by Lemma 2, without the dummy, which is the smallest and is skipped at the start of the merge. The other suffixes are sorted by Lemma 3. Line L9 merges two sorted lists, and by Lemma 4 each comparison of the merge is correct. So the result lists all suffixes in order. For the result is trivial.
Theorem 2 (time). DC3 runs in time when .
Proof. Without the recursion, each line is a scan or a counting sort with buckets; in the recursive calls the alphabet is the names, at most the length of the string. The reduced string has length , so the time satisfies , whose solution is . The recursion depth is , and the recursion stops, since the length drops for and the case returns at once.