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, σ):
- 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 root0inO((‖P‖ + 1) · σ + |P|)instructions. - Breadth-first failure, next-move, and output-link construction populates
fail[s],delta[s * σ + a],outHead[s], andoutNext[s]over an explicit FIFO queue inO((‖P‖ + 1) · σ)instructions, visiting each shared trie state once. - Amortized text scanning executes Algorithm 1 along
trieandfailusing the failure-depth potentialdepth[u]([AhoCorasick1975], Theorem 2) and emits each reported keyword occurrence as[endPos, keywordLen]viaoutHead/outNextinO(|t| + occ)instructions (independent ofσduring the scan).
The implementation and proof are organized into focused submodules under
CslibFrontier.Algorithms.WordRAM.AhoCorasick:
AhoCorasick.Input: publicInput, encodings/decoders,wordBound,searchTime, andReg.out.AhoCorasick.TrieModel: pure integer-state trie modelPTrieandTreeInv.AhoCorasick.BFSModel: pure breadth-first statePBFS,followOut, and root-loop pass.AhoCorasick.BFSCorrect: pureBFSInv/AutomatonInvcorrectness proof forPBFS.build.AhoCorasick.Layout: workspace base addresses, register aliases, andLayoutInv.AhoCorasick.Code: structuredCode .standard 26blocks for all four phases.AhoCorasick.Phase1Setup:TrieMemand header-decode / zero-fill proof (setupAndClearCode).AhoCorasick.Phase2Trie: flat array-trie construction proof (buildTrieCode).AhoCorasick.Phase3Mem:BFSMem, table-update lemmas, and depth-1 root loop proof.AhoCorasick.Phase3Loop: inner/outer BFS loop proof (bfsBuildCode).AhoCorasick.Phase4Step: amortized failure-link step proof (stepCode).AhoCorasick.Phase4Scan: output-link emission and full text-scan proof (scanCode).
Main definitions #
Cslib.Algorithms.WordRAM.AhoCorasick.Input: integer-alphabet multi-pattern matching instance.Cslib.Algorithms.WordRAM.AhoCorasick.totalPatternLen: total keyword length‖P‖.Cslib.Algorithms.WordRAM.AhoCorasick.Input.Valid: alphabet validity< σ.Cslib.Algorithms.WordRAM.AhoCorasick.encodePatterns,Input.encode: flat word-array input encoding.Cslib.Algorithms.WordRAM.AhoCorasick.encodeOccurrences,decodeOccurrences: flat[pos, len]occurrence encoding and lossless decoder.Cslib.Algorithms.WordRAM.AhoCorasick.Input.wordBound,searchTime: word-width threshold and instruction bound.Cslib.Algorithms.WordRAM.AhoCorasick.Reg.out: output header register.Cslib.Algorithms.WordRAM.AhoCorasick.searchCode,searchProgram: structured and flat Word-RAM Aho–Corasick multi-pattern matcher.
Main results #
Cslib.Algorithms.WordRAM.AhoCorasick.decodeOccurrences_encodeOccurrences:decodeOccurrences trecoversoccurrences P tfromencodeOccurrences (occurrences P t).Cslib.Algorithms.WordRAM.AhoCorasick.wordBound_le: polynomial upper bound oninp.wordBoundindependent ofocc.Cslib.Algorithms.WordRAM.AhoCorasick.Input.Valid.inputFits:InputFits w inp.encodefrominp.ValidandFits w inp.wordBound.Cslib.Algorithms.WordRAM.AhoCorasick.searchCode_exec,searchProgram_computesListInTime,searchProgram_executesList: linearO((‖P‖ + 1) · σ + |P| + |t| + occ)Word-RAM running time.
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.