Documentation

CslibFrontier.Algorithms.WordRAM.AhoCorasick

Aho–Corasick Multi-Pattern Matching on the Word-RAM #

Linear-time implementation of the Aho–Corasick multi-pattern string matching machine ([AhoCorasick1975], Algorithms 1, 2, and 3 and Section 6) on the Basis.standard Word-RAM over bounded integer alphabets [0, σ):

  1. Array-trie construction inserts all keywords P (|P| keywords of total length ‖P‖ = totalPatternLen P) into a flat (‖P‖ + 1) × σ integer-state goto table with root 0 in O((‖P‖ + 1) · σ + |P|) instructions.
  2. Breadth-first failure, next-move, and output-link construction populates fail[s], delta[s * σ + a], outHead[s], and outNext[s] over an explicit FIFO queue in O((‖P‖ + 1) · σ) instructions, visiting each shared trie state once.
  3. Amortized text scanning executes Algorithm 1 along trie and fail using the failure-depth potential depth[u] ([AhoCorasick1975], Theorem 2) and emits each reported keyword occurrence as [endPos, keywordLen] via outHead / outNext in O(|t| + occ) instructions (independent of σ during the scan).

The implementation and proof are organized into focused submodules under CslibFrontier.Algorithms.WordRAM.AhoCorasick:

Main definitions #

Main results #

References #

Structured Word-RAM implementation of Aho–Corasick multi-pattern string matching: initializes and clears the scratch tables, builds the array trie, runs breadth-first failure, next-move, and output-link construction, and scans the text emitting [endPos, keywordLen] pairs at Reg.out.

Flat Word-RAM program computing encodeOccurrences (occurrences inp.patterns inp.text) from inp.encode.

Top-level Word-RAM running-time theorems #

searchCode computes encodeOccurrences (occurrences inp.patterns inp.text) at Reg.out within searchTime inp - 1 instructions at any word width w with Fits w inp.wordBound.

searchProgram computes encodeOccurrences (occurrences inp.patterns inp.text) within searchTime inp = O((‖P‖ + 1) · σ + |P| + |t| + occ) instructions at any fitting width with inp.Valid and Fits w inp.wordBound.

Direct ExecutesList corollary for searchProgram discharging InputFits automatically from inp.Valid and Fits w inp.wordBound.