Proofs: Manber-Myers algorithm
These are the human proofs of Manber-Myers algorithm, at full length.
Each round of Manber-Myers sorts the suffixes by twice as many symbols full
This proof shows: correctness, termination.
Let with , , and the prefix of length of ; for , is the empty string , which is smaller than each nonempty string. We use the split lemma proved on the suffix array page: , and
Invariant (I). is a permutation of with , and for each , is the least rank with . So if and only if , and if and only if . Extend by for ; then the last two statements also hold for , with .
Initialization. Line L1 sorts by the first symbol, so is ordered by . Line L2 gives each run of equal first symbols the rank of its first member. So (I) holds before the first test of line L4.
Maintenance. Assume (I) before a round. For each bucket (a value of ), let and . By (I) the bucket occupies the ranks .
Each position is placed exactly once, in its bucket. Line L6 places the positions with . Line L7 places for each with , that is, each with , once, since is a permutation. Together these are all positions, each once. A position goes to slot , which starts at and grows by one at each placement into that bucket. Since bucket receives exactly placements, the slots used are , which are the ranks of the bucket. So line L8 makes a permutation in which each bucket keeps its range of ranks, and the buckets stay in increasing order of .
Inside a bucket, the order is by the second half. Take in the same bucket with . We show that is placed before . If , then is placed in line L6, before all placements of line L7, and before unless is also placed in line L6. But two different positions in the same bucket would have and of different lengths (if both are at least , their suffixes have length at most ), so they are not in the same bucket. If , then too (since is nonempty), both are placed in line L7, at the ranks and of and in the old . By (I), implies . So is placed first, in a smaller slot.
So the new is ordered by -prefixes. Take ranks of the new with and . If and are in different buckets, the bucket of comes first, so and by (1) . If they are in the same bucket, the previous paragraph shows that (otherwise would have been placed first), and by (1) .
Line L9 gives the new buckets. By (I), and hold if and only if and , that is, by the split, if and only if . Since the new is sorted by -prefixes, equal -prefixes are contiguous, and line L9 gives each run the rank of its first member. After line L10, (I) holds with the new value of .
Termination. Each round doubles , so the test of line L4 fails after at most rounds.
Result. The loop ends when has distinct values or when . In the second case for all , and different suffixes are different strings, so has distinct values in both cases. Then all -prefixes are different, and by (I) . Two suffixes with different -prefixes compare like these prefixes: they differ at a position less than inside both, or one prefix is shorter than , so it is the whole suffix, and it is a proper prefix of the other suffix. Hence , and line L11 returns the suffix array.