The Z-Algorithm and Exact Pattern Matching on the Word RAM #
This module implements Gusfield's Z-algorithm ([Gusfield1997], Sections 1.4–1.5) as uniform
16-register Basis.standard Word-RAM programs over word arrays, proving exact output agreement
with Cslib.Algorithms.Lean.ZFunction.zFunction and Cslib.Algorithms.Lean.ZFunction.zMatches
(= KMP.kmpMatchesList) together with linear O(|s|) and O(|pat| + |txt|) instruction bounds
that charge every comparison, Z-box copy, arithmetic operation, memory access, and output write.
Memory layouts and cost bounds #
Z-array construction (
zCode,zProgram):- Input (
State.init s):mem[0] = |s|andsoccupies[1, 1 + |s|). - Output:
zFunction swritten as a length-prefixed array aty.regs Reg.out = |s| + 1(mem[|s| + 1] = |s|andz[j]at|s| + 2 + j), preservingsat[1, 1 + |s|). - Word width and time: for every word width
wwithFits w (2 * |s| + 1),zCodeexecutes within39 * |s| + 9instructions (39 * |s| + 10forzProgram).
- Input (
Exact pattern matching via the Z-array of
pat ++ txt(matchCode,matchProgram):- Input (
State.init I.encode):MatchInput.encode I = |pat| :: |txt| :: (pat ++ txt)places|pat|atmem[1],|txt|atmem[2], andpat ++ txtcontiguously at[3, 3 + |pat| + |txt|)(patat[3, 3 + |pat|)andtxtat[3 + |pat|, 3 + |pat| + |txt|)). - Workspace and output: for
|pat| > 0, the Z-array ofpat ++ txtis computed in[3 + |pat| + |txt|, 3 + 2 * (|pat| + |txt|)), and the matching shiftszMatches pat txtare written as a length-prefixed array aty.regs Reg.out = 3 + 2 * (|pat| + |txt|)(which also serves as the output base whenpat = []). Both input blockspatandtxtare preserved. - Word width and time: for every word width
wwithFits w (2 * |pat| + 3 * |txt| + 4),matchCodeexecutes within39 * |pat| + 50 * |txt| + 22instructions (39 * |pat| + 50 * |txt| + 23formatchProgram).
- Input (
Amortized analysis #
During the Z-box scan over s (|s| ≥ 1), the rightmost Z-box right endpoint r never decreases
(r ≤ r' ≤ |s|), and each matched character comparison in extCode (10 instructions per match)
advances the new right endpoint r' = i + k past max r i ≥ r by 1. Thus the potential
$$\Phi(i, r) = 10 \cdot (|s| - r) + 29 \cdot (|s| - i)$$
pays 10 instructions per matched character comparison from 10 * (|s| - r) and 29 instructions
per position i from 29 * (|s| - i) to cover the Z-box test, at most one mismatch check, memory
writes, and loop overhead.
Main definitions #
Cslib.Algorithms.WordRAM.ZFunction.MatchInput: pattern-and-text word-array input record.Cslib.Algorithms.WordRAM.ZFunction.MatchInput.encode: flat word-list encoding[|pat|, |txt|] ++ pat ++ txt.Cslib.Algorithms.WordRAM.ZFunction.Reg.out: output base register (15).Cslib.Algorithms.WordRAM.ZFunction.zCode,zProgram: Word-RAM Z-array code and program.Cslib.Algorithms.WordRAM.ZFunction.matchCode,matchProgram: Word-RAM Z-algorithm pattern matcher code and program.
Main results #
Cslib.Algorithms.WordRAM.ZFunction.zCode_execandzProgram_computesListInTime: linear-time Word-RAM Z-array construction (39 * |s| + 9/39 * |s| + 10instructions).Cslib.Algorithms.WordRAM.ZFunction.matchCode_exec,matchProgram_computesListInTime, andmatchProgram_computesKmpMatchesInTime: linear-time Word-RAM exact pattern matching (39 * |pat| + 50 * |txt| + 22/39 * |pat| + 50 * |txt| + 23instructions).
References #
Input encoding and registers #
Address register out of the length-prefixed output array.
Programs #
Standalone Z-array code: reads s from State.init s and writes zFunction s as a
length-prefixed array at out = |s| + 1.
Uniform Basis.standard Word-RAM program for Z-array construction.
Uniform Basis.standard Word-RAM program for Z-algorithm exact pattern matching.
Bundled register invariants #
Execution of extCode #
Z-array construction on the word RAM. Starting from Data.init s at any word width w
with Fits w (2 * s.length + 1), zCode executes within 39 * s.length + 9 instructions and
writes zFunction s as the length-prefixed array at y.regs Reg.out = s.length + 1, preserving
the input block [1, 1 + s.length).
zProgram computes zFunction s in 39 * |s| + 10 instructions on every fitting word array at
every word width fitting 2 * |s| + 1.
End-to-end Z-algorithm exact pattern matching on the word RAM. Starting from
Data.init I.encode at any word width w with Fits w (2 * I.pat.length + 3 * I.txt.length + 4),
matchCode executes within 39 * I.pat.length + 50 * I.txt.length + 22 instructions and writes
zMatches 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).
matchProgram computes zMatches I.pat I.txt in 39 * |I.pat| + 50 * |I.txt| + 23
instructions on every fitting MatchInput at every word width fitting
2 * |I.pat| + 3 * |I.txt| + 4.
matchProgram computes KMP.kmpMatchesList I.pat I.txt in 39 * |I.pat| + 50 * |I.txt| + 23
instructions on every fitting MatchInput at every word width fitting
2 * |I.pat| + 3 * |I.txt| + 4.