3.8.6 · D1String Algorithms

Foundations — Aho-Corasick — multiple pattern search, automaton

2,720 words12 min readBack to topic

Before you climb into the main note, you must own the vocabulary it speaks. We build it one brick at a time.


0 · The raw material — strings, characters, and the alphabet

Everything here is about text: a row of symbols read left to right.

Why the symbol ? Because later, cost formulas count "one slot per possible next letter", and that number of slots is . Naming the alphabet lets us talk about that cost.

Figure — Aho-Corasick — multiple pattern search, automaton

Look at the figure: a string is just cells in a line, each cell holding one character. The little index numbers under the cells will matter the moment we talk about positions — "the match ended at position 4".


1 · Prefix and suffix — the two halves that make everything work

These two words are the heart of the whole algorithm. Get them wrong and nothing else lands.

Figure — Aho-Corasick — multiple pattern search, automaton

Why does Aho-Corasick obsess over these? Because the machine's whole job is:

"The longest prefix of some search word that is also a suffix of the text I've read so far."

That single sentence uses both concepts at once. When a letter fails to extend a match, the "shortcut rope" jumps you to the longest proper suffix of where you were that is still a prefix of some word. Both halves, again. Hold onto this — it returns in Step 2 of the parent note.


2 · Set, and — measuring a whole dictionary

We do not search for one word; we search for a dictionary of words at once.

Why do we need ? Because the build cost of the machine is measured in (and ), not in "number of patterns". Two patterns of length 1000 cost the same to build as 2000 patterns of length 1 — what matters is total characters. Naming lets the complexity line say that precisely.


3 · Graphs, nodes, edges — the shape of the machine

The machine is a graph. If that word scares you, it is just dots joined by arrows.

Figure — Aho-Corasick — multiple pattern search, automaton

Why a tree and not a plain list? Because words that start the same (he and hers both start he) can share their front edges, so the machine stores he once. That sharing is exactly what a tree gives you and a list does not.


4 · The trie — a tree of prefixes

Now combine "tree" with "prefix" and you get the first real structure.

We go deeper on the trie itself in Trie — prefix tree; here you only need: node = a prefix, edge = a character, terminal = a word ends.

We name three operations on it, matching the parent note:

  • = the string that node stands for.
  • or = the node you reach from by following the real edge labeled (may not exist).
  • terminal: a node flagged because a full pattern ends there.

This is the one genuinely new idea; the parent note derives it fully. Here we only make sure the symbol and picture are clear.

That "always shorter / always higher" fact is why we build failure links in a specific order — see the next brick.

This idea is borrowed directly from single-pattern matching. If you have seen KMP — single pattern matching, the failure link is exactly KMP's prefix function, but spread over a whole tree of words instead of one word.


6 · BFS — processing shortest strings first

To fill in every failure link correctly, we must visit nodes in order of depth. The tool that does that is BFS.


7 · The automaton and its transition

Finally, the word in the topic title: automaton.

There is a whole family of string machines built on suffixes rather than a fixed dictionary — see Suffix Automaton and Suffix Tree — and other matching engines like the Z-algorithm and string matching. Aho-Corasick is the "many fixed patterns at once" specialist.


8 · The cost symbols — , , , , and big-

Last brick: the letters in the complexity line, so it reads as English.


Prerequisite map

characters and alphabet

strings

prefix and suffix

pattern set and total length M

graph nodes and edges

tree and root and depth

Trie prefix tree

failure link

BFS by depth

automaton and transition delta

cost symbols n M Sigma z

Aho-Corasick machine

Read it upward: characters make strings; strings give prefixes/suffixes and a dictionary; graphs give trees, trees give the trie; prefix + suffix + BFS give failure links; trie + failure links give the automaton; add the cost symbols and you have the full Aho-Corasick machine.


Equipment checklist

Cover the right side and answer aloud; if any stalls, reread that section before the main note.

What is a proper suffix of hers, and list them all
A suffix that is not the whole string: ers, rs, s, "".
What single string is both a prefix and a suffix of every string
The empty string "".
In the trie, what does a node at depth represent
A string (prefix of some pattern) of length exactly .
What does measure
The total number of characters across all patterns added together.
Where does point — shorter or longer string, and which one exactly
To a strictly shorter string: the longest proper suffix of that is still a trie node (or root).
What is , and why
It is defined as the root itself (and never followed), because the empty string has no proper suffix to fall back to.
Why compute failure links with BFS and not recursion
needs failure links of shorter strings; BFS finishes all smaller depths first.
What does give you when has no real edge on
The state you reach by following the failure rope(s) — it never fails, worst case root.
In the cost , what is
The length of the text being scanned.
In the cost , what is
The number of matches actually reported during the scan.
Why is scanning independent of the number of patterns
With precomputed, each text char is one lookup regardless of dictionary size.