Proofs: Compressed suffix array
These are the human proofs of Compressed suffix array, at full length.
Lookup, extraction and search on the compressed suffix array are correct full
This proof shows: correctness, termination, time.worst.
Notation. has symbols coded ; with unique and smallest. Positions and rows are 0-based, , is the suffix array of and its inverse. , and . A position is sampled if it is a multiple of or equals . Since is unique and smallest, different suffixes of differ at some position, and none is a prefix of another.
Lemma 1 ( steps). If , then . So for , .
Proof. The first claim is the definition, since . The second follows by induction on .
Lemma 2 (monotone blocks). If and , then . Hence is strictly increasing over the rows .
Proof. and , and the first is smaller, so : their rows satisfy . The symbol is at row 0 only. Since is nondecreasing in (the suffixes are sorted by their first symbol first) and , the value increases strictly: within a block by the first part, and across blocks because grows by at least 1, which adds at least .
So all gaps are positive and have gamma codes, and decoding the sample of the block of plus gaps gives , from which and .
Theorem 1 (lookup). CSA-LOOKUP returns after at most steps of .
Proof. Let be the input row and the number of steps taken. Invariant: . It holds with . If is not sampled, then , because is sampled, so by Lemma 1 the step gives , and the invariant holds with . The positions reach a multiple of or within steps, so the loop stops after at most steps. At that point the stored sample is , and the result equals .
Theorem 2 (inverse and extract). inverse(p) returns , and extract(l, r) returns .
Proof. Let . The stored row is , and by Lemma 1 each of the steps moves from the row of position to that of , as . For extract, start at the row of ; by Lemma 1, the -th output is , for , and , so it is a symbol of .
Theorem 3 (search). COMPARE returns the sign of the comparison of with (0 when is a prefix of the suffix), and CSA-FIND returns the rows of the suffixes that start with .
Proof. By Lemma 1, the loop reads while they equal . The suffix cannot run out before a difference is found, because its last symbol is , which differs from every symbol of ; so every step before a difference has and Lemma 1 applies. The first difference decides the order, and if there is none, is a prefix. Since the suffixes are sorted, the sign of COMPARE is nondecreasing in : all rows with sign , then all with 0, then all with . The first binary search finds the first row with sign at least 0, the second the first row with sign , so the range between them is the set of rows with sign 0.
Time. A or value costs at most gamma decodes. Lookup and inverse take at most such steps; extract takes ; each COMPARE takes at most steps, and the two binary searches comparisons. All loops are bounded, so every operation terminates. With a structure that gives in time, the costs are , and .