Proofs: Larsson-Sadakane algorithm
These are the human proofs of Larsson-Sadakane algorithm, at full length.
Larsson-Sadakane suffix sorting is correct and ends after O(log n) passes full
This proof shows: correctness, termination.
Notation. with , 0-based, no sentinel, and . The order of strings is lexicographic, with a proper prefix before the longer string. The algorithm keeps a permutation of the positions (the array sa), and a partition of the ranks into consecutive blocks, the groups. For a position , is the last rank of the group that holds . A group of size 1 is sorted.
Invariant. At the start of each pass with the value :
(A) For ranks in different groups, .
(B) The positions of one group all have the same prefix of length ; in particular each of their suffixes has length at least .
(C) is the last rank of the group of .
From (A) and (C), implies . From (B) and (C), implies that and share a prefix of length .
Lemma 1 (keys). At any moment during a pass with the value , (A) and (C) hold, and each group shares a prefix of length at least .
Proof. A split replaces a group by subgroups in consecutive ranks and gives each subgroup the number of its last rank, so (C) holds after each split. We show below that the subgroups are ordered by their suffixes, so (A) holds after each split. A subgroup is a subset of a group, so its members still share a prefix of length .
Lemma 2 (one split). Let be a group that is split during the pass with the value . For , let if and otherwise, read at the moment the split of starts. Then for : if , then ; and if , then and share a prefix of length .
Proof. By Lemma 1, and share a prefix of length , so and , and if and only if , where is the empty string. If , then is empty and is a proper prefix of , unless ; only one member of can have , because the suffix lengths differ. So with gives . If both keys are at least 0, then by Lemma 1 at the moment of reading, implies , and implies that and share a prefix of length , so and share a prefix of length .
The ternary-split quicksort sorts by and makes each block of equal keys a subgroup. By Lemma 2 the subgroups are in the order of their suffixes, so (A) holds after the split, and each subgroup shares a prefix of length .
Theorem 1 (pass). If (A), (B) and (C) hold at the start of a pass with the value , they hold at the start of the next pass with the value .
Proof. The pass walks over all ranks. The sorted runs are skipped; they consist of groups of size 1, for which (B) holds for every length. Every group that is not sorted is split once, and by Lemma 2 each of its subgroups shares a prefix of length . Lemma 1 gives (A) and (C). So (B) holds for .
Initialization. Line L2 sorts the positions by their first symbol and makes each block of equal symbols a group numbered by its last rank. (A) holds for different first symbols, (B) holds for , and (C) holds by construction.
Theorem 2 (result and termination). The algorithm ends after at most passes, and then is the suffix array and is its inverse.
Proof. By (B), after the passes with , each group shares a prefix of length . Two different suffixes cannot share a prefix of length , since only has length . So once , all groups have size 1, and the next pass finds no unsorted group and returns (line L9). When all groups have size 1, (A) says that lists the suffixes in increasing order, and (C) says that .