Proofs: Karp-Miller-Rosenberg algorithm
These are the human proofs of Karp-Miller-Rosenberg algorithm, at full length.
The KMR naming gives the ranks of all basic factors full
This proof shows: correctness, termination, time.worst.
Notation. with , positions 0-based. For with , the basic factors of level are for . The claim is that KMR returns arrays with
(N) is the number of distinct strings among that are smaller than ,
for every level with . In particular, if and only if , and if and only if .
Lemma 1 (doubling). For and , . Two strings of the same length compare as the pairs of their halves, the first half first.
Proof. The first symbols of are , and the next are ; the index is a valid start of level . For strings and with and , the first difference of and is in the first halves if , and otherwise in the second halves.
Theorem 1. At each test of line L3, (N) holds for the levels .
Proof. By induction on . For , line L1 gives each symbol its rank among the distinct symbols, which is (N) for single symbols. Assume (N) for level and . Line L4 forms the pairs . By (N) for level , the pairs compare as , so by Lemma 1 they compare as . Line L5 sorts the positions by the pairs; the two passes of the radix sort are stable counting sorts, the second key first, so the result is sorted by the pair. Lines L6 to L8 scan the sorted positions and increase the counter exactly when the pair differs from the one before. The first position gets , and the name of a position is the number of distinct pairs before its pair in sorted order, which is the number of distinct factors of level smaller than . That is (N) for level .
Termination. The level increases by 1 in each round, and the loop stops when , after rounds. For the algorithm returns at once.
Corollary (comparison of factors). For and , the factor is smaller than, equal to or larger than as the pair compares with .
Proof. Since , the two windows of length at offsets 0 and cover the factor, and they overlap or touch. If the first windows differ, the first difference of the two factors is inside them, and (N) gives the order. If the first windows are equal, the two factors agree on offsets to , which include the overlap to . So their first difference, if any, is at an offset of at least , inside the second windows, and the second windows compare as the factors do.
Theorem 2 (time). KMR takes time and words of space for all levels, after the symbols are mapped to their ranks.
Proof. Level has positions. Line L4 and lines L6 to L8 take time, and line L5 is two counting sorts with buckets, since names are below , so time. There are rounds, and each level stores at most names.