Aho-Corasick

funk, disco, retro, groovy · 4:30

Listen on 93

Lyrics

[Verse 1]
Started with a problem, matching patterns in a string
Brute force was too slow, had to find a better thing
Multiple patterns at once, that's the challenge we face
Aho-Corasick steps up with algorithmic grace
Build a trie first, every pattern gets its place
Each node represents prefixes in this search space
Add the patterns one by one, character by character
Root to leaf, each path makes the matching massacre

[Chorus]
Trie then fail, that's the way we roll
Suffix links connect when matches don't unfold
Linear time scanning, that's our ultimate goal
Aho-Corasick algorithm, taking full control
Build it right, search it tight
Multiple patterns, single flight
Trie then fail, never stale
Preprocessing sets the scale

[Verse 2]
Failure function is the key, when a match goes wrong
Points you to the longest suffix where you still belong
BFS through the trie, computing every link
Proper suffix that's a prefix, stop and really think
If current character fails, don't restart from scratch
Follow failure links until you find your catch
This preprocessing step makes searching super clean
Linear time complexity, best you've ever seen

[Chorus]
Trie then fail, that's the way we roll
Suffix links connect when matches don't unfold
Linear time scanning, that's our ultimate goal
Aho-Corasick algorithm, taking full control
Build it right, search it tight
Multiple patterns, single flight
Trie then fail, never stale
Preprocessing sets the scale

[Bridge]
Dictionary matching, DNA sequences too
Text editors, spam filters, this algorithm's true
From virus signatures to plagiarism detection
Aho-Corasick brings that pattern intersection
O of m for building where m's the pattern size
O of n for searching, efficiency we prize

[Verse 3]
Now we scan the target text, character by character
Current state transitions, make the matching sinister
If we find a match, report it, but don't stop the flow
Keep following those suffix links, let the algorithm go
Multiple overlapping patterns, all detected clean
Most elegant solution for this search routine
Space complexity linear, time complexity too
Aho-Corasick mastery, now belongs to you

[Chorus]
Trie then fail, that's the way we roll
Suffix links connect when matches don't unfold
Linear time scanning, that's our ultimate goal
Aho-Corasick algorithm, taking full control
Build it right, search it tight
Multiple patterns, single flight
Trie then fail, never stale
Preprocessing sets the scale

[Outro]
From preprocessing to the final search phase
Aho-Corasick sets the algorithmic blaze
Multiple string matching, solved with style and grace
Linear time performance, putting speed in place

← Boyer-Moore | Levenshtein distance (edit distance) →