Proofs: Suffix array
These are the human proofs of Suffix array, at full length.
Prefix doubling sorts the suffixes, and the two searches find the range of a pattern full
This proof shows: correctness, termination.
Let with , , and for let be the prefix of length of . Strings are compared in lexicographic order, in which a proper prefix is smaller.
Lemma 1 (splitting). For and positions : if and only if , or and , where a suffix that starts at or later is the empty string.
Proof. is the concatenation : if , the first part has length and the second is the rest, cut at length ; if , the first part is all of and the second is empty. If , then either they differ at a position less than and inside both, which decides both comparisons the same way, or one of them is a proper prefix of the other. In the second case the shorter one, say , has length less than , so it is all of , and is a proper prefix of as well, so the order is again the same. If , both have the same length; if that length is less than then both second parts are empty and the strings are equal, which matches " is false"; otherwise the comparison of the -prefixes is the comparison of the second parts.
Invariant (R). After line L2, and after line L6 in the round with value (writing , or after line L2): for all , if and only if , and if and only if . Moreover the ranks are , where is the number of distinct -prefixes, and after line L6 lists the positions in non-decreasing order of rank.
Initialization. Line L2 ranks each position by its symbol among the sorted distinct symbols, which is the order of the 1-prefixes.
Maintenance. In the round with value , (R) holds with before line L4. The key of line L4 is with when . The value is below each rank, as the empty string is below each nonempty one. By (R) for and Lemma 1, in lexicographic order of pairs if and only if , and if and only if . Line L5 sorts by key, and line L6 gives rank 0 to the first position and adds 1 exactly when the key changes, so the new ranks are and satisfy (R) with .
Termination and result. If line L7 sees , the ranks are , so the -prefixes of the suffixes are pairwise distinct. Two suffixes whose -prefixes differ compare like these prefixes (a common prefix of length would make them equal), so is in increasing order of the suffixes and is its inverse. The test holds at the latest when : then for each , and different suffixes are different strings. Since doubles in line L8, there are at most rounds, and the loop ends.
Lemma 2 (monotone heads). For a pattern of length , let . Then , and the set of with is .
Proof. Let be two strings and cut both to length . If and differ at a first position inside both, the cut strings are equal when , and ordered like and otherwise. If is a proper prefix of , the cut of is a prefix of the cut of . In all cases the cuts satisfy . The second statement holds because has length exactly when the suffix has length at least , and then means that is a prefix of the suffix; a shorter is never equal to .
Theorem. SA-BOUND returns the least with , and SA-BOUND the least with ( if there is none). The suffixes that start with are exactly those with ranks in , where and are these two values.
Proof. The predicate , "" or "", is monotone in by Lemma 2: once true, it stays true. Lines L9 to L16 keep the invariant " is false for , and true for with " (where is read as true). It holds at line L9. In a step, ; line L14 sets only when is false, so is false for all by monotonicity; line L15 sets only when is true. The length drops by at least in each step, so the loop ends with , the least with true. By Lemma 2, the ranks with are those with and not , which form the interval , and they are the ranks of the suffixes that start with .