Proofs: Sparse suffix tree
These are the human proofs of Sparse suffix tree, at full length.
The sparse suffix tree is the compacted trie of the chosen suffixes, and its searches report the right positions full
This proof shows: correctness, complexity.
We use the pseudocode, the notation and the invariant (P1) to (P3) of the page. Let , let be the chosen suffixes of in lexicographic order (written by their positions), and let for , . Because occurs only at the end, no chosen suffix is a prefix of another.
Lemma 1 (stack). After the suffix is processed, the stack holds the path from the root to the leaf of in the compacted trie of , and the tree built so far is .
Proof. By induction on . For the leaf of is attached to the root, which is . Let it hold for . In , the leaf of hangs from the node of depth on the path to : the suffixes are smaller than , and the one with the longest common prefix with among them is , since a string between two others in sorted order shares at least their common prefix with the larger one. So leaves the path to at depth , and it shares no longer prefix with any earlier suffix. Line L2 pops the nodes of depth greater than from that path. If the top has depth exactly , the leaf hangs from it. Otherwise the last popped node lies below depth on an edge from the top, and line L3 splits this edge at depth , using the representative of the popped node to spell the label, which is correct since that node's string extends the top's string along the path. Line L4 attaches the leaf with the label ; its first symbol differs from at depth , and the later siblings, from suffixes after , are not there yet, so the children keep distinct first symbols. The nodes of the new path are on the stack.
At the end the tree is : (P1) holds by the leaves attached, (P3) holds by the labels, and (P2) holds because a node created by line L3 receives the popped node and the new leaf, and the root and other nodes only gain children. A tree with leaves where each internal node except the root has at least two children has at most internal nodes besides the root, so at most nodes besides the root.
SEARCH. By (P2) and (P3), the walk of line L5 follows the unique path that spells a prefix of , and it fails exactly when no node string, and thus no chosen suffix, extends a string that starts with, so then no chosen suffix has prefix . If it succeeds, the point where ends lies on the edge into a node (or at ), and the chosen suffixes with prefix are exactly the leaves below , by (P1) and (P3). Line L6 returns them. The walk compares at most symbols, and the subtree of has fewer than nodes for leaves, by the bound above, so the time is .
SEARCH-ALL. Let the chosen positions be below , and . Every reported position is an occurrence: if is a chosen position where occurs and , then occurs at . Every occurrence is reported: let occur at , and let be the smallest multiple of with , so . Then , so is a chosen position, is nonempty and occurs at , and SEARCH reports for this ; the check of line L8 succeeds, and is reported. No position is reported twice: a position is reported only for with the chosen position in , which is unique.