Proofs: Weiner's algorithm
These are the human proofs of Weiner's algorithm, at full length.
Weiner's algorithm builds the suffix tree in linear time full
This proof shows: correctness, termination, complexity.
Let , , with only at the end, and . Let be the compact trie of . Since is unique, no suffix is a prefix of another, and each has a leaf. For a string that is a prefix of some , , call present in . Write for the string of a node.
Definitions at time (after inserting ). For an inner node or the root of : , and is the node of if that string is a node of , undefined otherwise.
Invariant. After the step for (and after line L1 for ), the tree is , and and match the definitions at time .
For : the tree is the root with the leaf of , , and no link is defined, since no nonempty string is an inner node.
Lemma 1 (branching is inherited to the right). If is an inner node of , then is an inner node of (or the root, if is empty).
Proof. and are present for two symbols , as prefixes of and . Then and are prefixes of and , which are in the tree since (a suffix with as a prefix is not the last one). So branches.
Lemma 2 (the head). Fix and , and let the tree be . Let be the lowest inner node or root on the path of with , if one exists. Then the longest prefix of present in is if exists, and the empty string otherwise.
Proof. A nonempty prefix of present in has the form with a prefix of , and it is present exactly when has in front in some , . Presence of is closed under taking prefixes. Let be the longest one and suppose it lies strictly below on the path of ; let be the first node at or below the place of . Every suffix that has as a prefix passes through (no branching between), and , so if is the leaf of this is impossible; otherwise is a prefix of , so , and is lower than , a contradiction. If no node has , then even the root lacks , so alone is not present.
By Lemma 2 and the uniqueness of , the head of (its longest prefix present in ) is shorter than , so adding creates one leaf, below the place of the head, and at most one inner node, at the head.
Step , line L4. The walk starts at the parent of the leaf of ; the leaf itself cannot have in front, because only passes through it and is not yet present. Since is closed upward (if is present, so is times each prefix), the walk stops at the lowest node with , or at the root without . After is inserted, is present for every prefix of , so must be in for every node on the path of ; the nodes above already have it, and line L4 adds it to the nodes passed and to or the root. No other indicator changes for old nodes: the only new present strings are the prefixes of , and a string that is a prefix of has and a prefix of .
Line L5. If no exists, the head is empty by Lemma 2, and the leaf of goes below the root. No new inner node and no new link are created: the only new node is the leaf.
Lines L6 to L9. Let be the lowest node at or above with defined, or if there is none, and , so (with of length and $v'' = $ root). The head is present and lies on the path from that starts with the symbols of after . No node lies strictly between and the place of the head: such a node would be with (as prefixes), and by Lemma 1 would be an inner node, strictly between and and with a link for , contradicting the choice of ; for , the same argument with ranging over the prefixes of including the empty one, whose link would make the root the choice of . If , then and the head is the node . If , the head is not a node of (it would give , so ), so it is inside the edge entered from by the symbol , and the split creates the node of the head; walking down by symbols uses only the edge length.
Updates of and for the new node. In , has two children: the old lower node and the leaf of . A string is present in if and only if it is a prefix of some , , with a prefix of ; for this would need , so and is an old suffix through , hence through . So at time , where for a leaf of the set is when ( is in because ) and empty for . Note that for an inner node is already correct at time after line L4, since is not on the path of unless it was updated there, and in both cases it matches the definition. No string is a node of : by Lemma 1, would have been an inner node of , but it is new. So is empty. The only new inner node is , and its only incoming link is from the node of without its first symbol, which is ; line L9 sets . Old links stay correct, since nodes are never removed and an old node keeps its string.
Line L10. The leaf of goes below the head, which gives . The invariant holds after step , and after step 0 the tree is , the suffix tree of . The loop has steps, and each walk goes up a finite path, so the algorithm terminates.
Time. Let be the number of nodes on the root path of , with .
Links raise the node depth by at most one. If , each node on the root path of other than the root is for a node on the root path of (Lemma 1), and distinct nodes give distinct . So . For , .
Walks. Lines L4 and L6 pass from the parent of the leaf of up to , which is steps, or up to the root in the case of line L5. The new leaf has (the split adds at most one node above it), and in the case of line L5. In both cases the walk of step has at most steps, and the sum over all is at most .
Sets. Each indicator set only grows, and the copy in line L9 inserts exactly as many entries as it reads, so the total cost of all set operations is of the final number of entries plus the number of walk steps. The entries at the end are the pairs with an inner node or the root and present. Reverse all strings: is a string of that is left-maximal (it has two different symbols on its left in , or is the empty string), and is a symbol after it. Each pair is either an edge of the suffix tree of (when is right-maximal too, it is a node and starts one of its child edges) or the unique right extension of a left-maximal string that is not right-maximal, at most one per inner node . The suffix tree of has edges and the suffix tree of has nodes, so there are entries.
Total. steps, each with map operations: time for a constant alphabet or expected with hashing, and with balanced maps.