Proofs: PHI algorithm
These are the human proofs of PHI algorithm, at full length.
The PHI algorithm computes PLCP in text order and then the LCP array, in linear time full
This proof shows: correctness, complexity.
We use the pseudocode and the notation of the page. Write , with the empty string, and for the lexicographic order. All suffixes are distinct, so is a strict order and is the suffix immediately before in it.
Lemma 1 (the key fact). For , if and , then and .
Proof. Let , so and they share their first symbols. Removing the common first symbol keeps the order, so , and they share symbols. So has a predecessor in sorted order, , and its predecessor satisfies . A string that lies between two strings in sorted order shares with the larger one at least their common prefix, so .
Invariant of lines L3 to L5. At the start of iteration , for , for , and (when ).
At , line L1 has set and . In iteration , is read before it is overwritten. If it is , then by definition, which line L3 stores. Otherwise, by the invariant the first symbols of and agree, and the loop of line L4 extends while the next symbols agree and both suffixes continue, so it stops at ; line L5 stores it. For iteration : if , is reset to 0, a valid lower bound. If , stays 0. If , becomes by Lemma 1. The other entries are untouched, so the invariant holds.
After the loop, . Line L6 sets for , and , which is the LCP array.
Time. Each iteration of line L4 increments . Between iterations, decreases by at most 1, or is reset to 0, which happens only once (at ), from a value at most . Since always, the total number of increments is at most plus the total decrease, which is at most from the decrements plus at most from the reset. So line L4 runs times in total, and with the scans of lines L1, L3 and L6 the time is .