Definition · TrieA trie over a set of words is a rooted tree whose edges carry characters, such that the path from the root to a node spells a distinct prefix of at least one word, and every distinct prefix appears as exactly one node. A node is marked as a word end if the prefix it spells is one of the words. The number of nodes is one plus the number of distinct non-empty prefixes.
Example · Ten nodes for twelve charactersThe dictionary is he, she, his, hers. Its distinct non-empty prefixes are h, he, her, hers, hi, his, s, sh and she — nine of them — so the trie has 10 nodes. The four words total 12 characters, and a list of separate strings would store all 12; the trie stores 9 edges, saving the 3 positions where he and hers overlap and where h leads to both he and his.
Held with one array of sigma slots per node that is 10 × 5 = 50 cells, since the dictionary uses the five characters e, h, i, r, s. Held with a map per node it is 9 entries. The panel prints 50 and 10 side by side so the trade is a pair of numbers rather than a preference.
A prefix query walks down from the root and then collects every word-end below. Asking for h returns he, hers and his, and the panel checks that against filtering the word list with a string comparison — the definition of the query, computed a second way.
Definition · Failure and output linksIn a trie over a dictionary, the failure link of a node v points to the node spelling the longest proper suffix of v's string that is itself a node of the trie, or to the root if there is none. The outputs of v are v's own word, if it ends one, together with the outputs of v's failure link. The links are computed by a breadth-first traversal, so a node's link is set after every shorter node's.
Theorem · One pass reports every occurrence of every wordFeed the text one character at a time, following the child edge when it exists and otherwise following failure links until a child exists or the root is reached. After reading t[0..i] the current node spells the longest suffix of t[0..i] that is a node of the trie, and its outputs are exactly the dictionary words ending at position i.
By induction on i, exactly as for the single-pattern automaton. Before anything is read the current node is the root, spelling the empty string, which is the longest suffix of the empty text that is a node.
Suppose the current node spells u, the longest suffix of t[0..i−1] that is a node. A node spelling a suffix of t[0..i] of length at least 1 consists of a suffix of t[0..i−1] followed by t[i], and that suffix must itself be a node because every prefix of a node is a node. So the candidates are the nodes w followed by t[i] for w a suffix of u that is a node — which is precisely the failure-link chain from u — and the walk takes the longest.
A word ends at i exactly when it is a suffix of t[0..i], hence a suffix of the current node's string, hence on the failure chain from it. The outputs are defined as that chain's word ends, so they are exactly the words ending at i.
□
The final sentence of that proof is the operative one and it is easy to read past: the words reported together at a position are the ones related by SUFFIX. Not prefix. That distinction is what the lab's last table is for.
One pass against four passes, in comparisons
On the text ushers hers his she he, 22 characters, one pass reports 9 occurrences for 31 comparisons. The four separate KMP passes cost 23, 25, 26 and 24, which is 98 in total — a factor of more than three. The occurrences are he at 2, 7, 17 and 20; she at 1 and 16; his at 12; and hers at 2 and 7.
The saving is not that the automaton is cleverer per character; it is that there is one scan rather than four. Each separate pass reads all 22 characters, so four of them read 88 characters where one reads 22, and that ratio is where the factor of three comes from. With ten words it would be a factor of nearer ten, which is the reason the structure exists.
Which words are reported at the same position
At the end of ushe — text position 3 — the automaton reports both she and he, because he is a suffix of she and so sits on its failure chain. Position 18 does the same. No other position on this text reports two words, and the panel's per-word table is checked against scanning the text separately for each one.
Now take the dictionary a, ab, abc, abcd, where every word is a prefix of the next, and scan abcdabcab. The words end at positions 0, 1, 2 and 3 respectively for the first occurrence, and at no position does the automaton report more than one — because none of bcd, cd, d is a word, so no failure chain from abcd reaches another word end. Prefix nesting is invisible to the output links; only suffix nesting is not.
That is the one property of this structure most likely to be misremembered, and it is checkable in two clicks: the classic dictionary reports two words at one position and the nested one never reports two at all, although the nested one is the one that looks as though it should.