Proofs: Enhanced suffix array
These are the human proofs of Enhanced suffix array, at full length.
The child table scans compute up, down and next, and give the child intervals full
This proof shows: correctness, termination, time.worst.
Setting. has , for and . The fields , and are defined as on the page. Write for , with for an empty range. So is the smallest with and ; is the largest with and ; is the smallest with and .
Lemma 1 (first stack). In the first scan, just before rank is processed, the stack holds, from the bottom, exactly the ranks with , that is, the ranks with no smaller value after them before , in increasing order of rank. Their values do not decrease from the bottom.
Proof. Induction on . For the stack is and the claim holds. Suppose it holds for . Rank stays after step if and only if (the pops remove exactly the top ranks with , and since the values do not decrease from the bottom, these are all the ranks on the stack with a larger value), and then is pushed. So after step the stack holds the ranks with and , and itself, which is the claim for .
Lemma 2 (up). The first scan sets correctly for every .
Proof. By Lemma 1, the candidates with and are exactly the stack ranks with a value above . They are the top part of the stack and are popped at step , the smallest rank last. Line L6 stores the last one popped, so is the smallest candidate. If no rank is popped there is no candidate, and stays nil; the variable is reset after each use, so a pop at an earlier step does not leak.
Lemma 3 (down). The first scan sets correctly for every .
Proof. Call a candidate for if , and .
First, a write in L5 is a write of a candidate. At that point was directly above on the stack, so , and the test gives . Take a rank with and suppose . If was popped before step , it was popped by a rank with and ; repeat the argument with . This ends with a rank between and , with , that is still on the stack at step . Step does not pop it, so it lies between and on the stack, which is false. So every value between and is larger than , and is a candidate.
Second, every candidate is pushed directly above : all values between them are larger than , so stays on the stack, and step pops every rank between them. If are candidates, then , so step pops , with , and L5 does not write. Let be the largest candidate. It is popped at some step , since . Every rank between and has a value of at least , or it would have popped earlier. If , then would be a candidate larger than , so . After the pop, is the top and , so L5 writes . By the first part no other write to happens after this one, since has no candidate after , and if has no candidate, nothing is written.
Lemma 4 (next). The second scan sets correctly for every .
Proof. As in Lemma 1, with one change: a rank with a value equal to is popped by line L11 and replaced by . So before step , the stack holds the ranks with , with strictly increasing values. At step , after the pops of larger values, the top has . If , the top satisfies , and it is the only such rank with this value, so , and no smaller was possible since was not popped before. If no top has the value , no earlier rank has . Each rank is set at most once, when it is popped by an equal value; it is popped by a smaller value otherwise and keeps nil, which is right since every later rank with an equal value has the smaller value in between.
Theorem (children). Let be an -interval with -indices . Then ESA-CHILDREN emits .
Proof. The -indices are the ranks in with , and all values there are at least . For , , since all values between are larger than ; and is nil, since lies between and any later rank with the value . So L16 and L17 emit the right intervals once .
For the root , , and is the first rank after 0 with ; the chain ends because .
Otherwise or , and , . If , then , and the smallest with and is : it qualifies since everything between and is at least ; every rank in has a value above and then , with value , lies between it and ; and rank and those before it have between or on them. So , which lies in . If , then rank has the smaller value , and , if defined, is at most : rank itself qualifies as a candidate when , which holds as all values there are at least . So the test in L14 fails and the algorithm uses . By definition, is the largest with and only larger values between and . The rank qualifies. A rank inside the interval has between with , so it fails, and a rank beyond has , so either blocks it or itself fails when , and later ranks have between. So .
The case also needs : any has rank or a smaller value between, and is not larger than , so fails the first condition, and a with fails the second, since lies between.
Time and termination. Each scan pushes each rank once and pops it at most once, so each takes time and ends. ESA-CHILDREN takes time per child, since every step moves to a larger rank inside . Find takes time per node for the child scan, at most nodes, and symbol comparisons in all, so .