Explain Aho-Corasick failure link construction. Build a trie from patterns. Failure links point to longest proper suffix that's also a pattern prefix. This allows linear time text scanning.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Failure links in Aho-Corasick are built by first constructing a trie of all patterns, then performing a breadth-first traversal starting from the root. The root's failure link points to itself, and each child of the root has its failure link set to the root. For other nodes, their failure link points to the node corresponding to the longest proper suffix of the current node’s path that is also a prefix in the trie. This is found by following the failure link of the parent node and checking for a matching child edge. This construction enables linear-time scanning of text by allowing the algorithm to fallback efficiently on mismatches while preserving pattern matching state. Outputs from failure-linked nodes are merged to handle multiple pattern matches.
The Aho–Corasick machine starts from the same idea as Knuth–Morris–Pratt: when you can't follow a trie edge for the next input symbol you shouldn't throw away all of the structure you've already matched, you should fall back to the longest suffix of what you've seen that could still lead to a match.
The construction has two phases:
Insert all of your pattern strings into a trie. Each node represents a prefix of one or more patterns; mark the nodes where a pattern ends.
Compute a fail pointer for every node that points to “the longest proper suffix of this node's string that is also a prefix in the trie”. For convenience, the root's fail pointer points to itself.
You can compute the fail pointers with a breadth–first traversal so that when you process a node, all of its ancestors' fail pointers are already known. One common formulation looks like this:
python
from collections import deque
def build_fail_links(root):
root.fail = root
# first level: children of root fail back to root
q = deque()
for child in root.children.values():
child.fail = root
q.append(child)
while q:
state = q.popleft()
for symbol, next_state in state.children.items():
# follow fail links until we find a state with this symbol
f = state.fail
while f is not root and symbol not in f.children:
f = f.fail
# if we can transition on this symbol from f, set the fail link
next_state.fail = f.children[symbol] if symbol in f.children else root
# inherit any output patterns from the fail state
next_state.outputs |= next_state.fail.outputs
q.append(next_state)
Once these links are set, scanning text is linear: keep a current state, consume each character, follow a trie edge if it exists, otherwise follow fail links until you can take an edge or reach the root. Every time you enter a node, its outputs set tells you which patterns end at that position. Because the automaton never backs up in the input and the total number of link traversals is bounded by the input length, the overall matching time is (O(\lvert\text{text}\rvert + \lvert\text{patterns}\rvert)).