Knuth–Morris–Pratt String Matching in Linear Time on the Word RAM #
This module implements the Knuth–Morris–Pratt ([KnuthMorrisPratt1977], Sections 1–2;
[CLRS2009], Section 32.4) failure-table construction and exact pattern matcher as fixed
Basis.standard word-RAM programs over word sequences, proving linear O(|p|) and
O(|p| + |t|) instruction bounds:
prefixCodeandprefixProgramtake a patternp : List ℕinState.init playout and write the length-prefixed failure table(computePrefix p.toArray).toListat address|p| + 1within31 * |p| + 10instructions (31 * |p| + 9beforehalt) wheneverFits w (2 * |p| + 1)andInputFits w phold.searchCodeandsearchProgramtake aSearchInput(pat, txt)encoded as[|pat|, |txt|] ++ pat ++ txtinState.init, construct the failure table at3 + |pat| + |txt|whenpat ≠ [], and write the length-prefixed list of all exact match shiftskmpMatchesList pat txtat address3 + 2 * |pat| + |txt|within31 * |pat| + 37 * |txt| + 23instructions (+ 22beforehalt) wheneverFits w (2 * |pat| + 2 * |txt| + 4)andInputFits w I.encodehold.
Every index calculation, memory load and store, character comparison, fallback transition, match
recording step, loop test, and final halt is charged by the machine semantics. Both the
failure-table loop and the text-scanning loop use ExecWithin.loop_potential with matched-prefix
potential 10 * k, charging each 10-instruction fallback step against the strict decrease
pi[k - 1] ≤ k - 1.
Main definitions #
Cslib.Algorithms.WordRAM.KMP.Reg.out: output register (Fin 15) holding the returned length-prefixed array address.Cslib.Algorithms.WordRAM.KMP.SearchInput,SearchInput.encode: pattern-matching instance(pat, txt)and its word-sequence encoding[|pat|, |txt|] ++ pat ++ txt.Cslib.Algorithms.WordRAM.KMP.prefixCode,prefixProgram: structured and flat programs computing(computePrefix p.toArray).toList.Cslib.Algorithms.WordRAM.KMP.searchCode,searchProgram: structured and flat programs computingkmpMatchesList I.pat I.txt.
Main results #
Cslib.Algorithms.WordRAM.KMP.prefixCode_exec,prefixProgram_computesListInTime: failure-table construction in31 * |p| + 10word-RAM instructions at widths fitting2 * |p| + 1.Cslib.Algorithms.WordRAM.KMP.searchCode_exec,searchProgram_computesListInTime: end-to-end KMP pattern search in31 * |pat| + 37 * |txt| + 23word-RAM instructions at widths fitting2 * |pat| + 2 * |txt| + 4.
References #
Input encoding and registers #
Address register out of the length-prefixed output array.
Programs #
Structured program computing (computePrefix p.toArray).toList as a length-prefixed array at
out = |p| + 1 from Data.init p.
Flat program computing (computePrefix p.toArray).toList via ExecutesList.
Amortized execution of advanceCode #
Execution of buildPrefixNonempty and prefixCode #
KMP failure-table construction on the word RAM. Starting from Data.init p at any word
width w with Fits w (2 * p.length + 1), prefixCode executes within 31 * p.length + 9
instructions and writes (computePrefix p.toArray).toList as the length-prefixed array at
y.regs Reg.out = p.length + 1, preserving the input block [1, 1 + p.length).
prefixProgram computes (computePrefix p.toArray).toList in 31 * |p| + 10 instructions on
every fitting word array at every word width fitting 2 * |p| + 1.
Execution of searchCode #
End-to-end KMP pattern search on the word RAM. Starting from Data.init I.encode at any
word width w with Fits w (2 * I.pat.length + 2 * I.txt.length + 4), searchCode executes
within 31 * I.pat.length + 37 * I.txt.length + 22 instructions and writes
kmpMatchesList I.pat I.txt as the length-prefixed array at
y.regs Reg.out = 3 + 2 * I.pat.length + I.txt.length, preserving the pattern and text blocks at
[3, 3 + I.pat.length) and [3 + I.pat.length, 3 + I.pat.length + I.txt.length).
searchProgram computes kmpMatchesList I.pat I.txt in
31 * |I.pat| + 37 * |I.txt| + 23 instructions on every fitting SearchInput at every word width
fitting 2 * |I.pat| + 2 * |I.txt| + 4.