Documentation

CslibFrontier.Algorithms.WordRAM.ZFunction

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 #

  1. Z-array construction (zCode, zProgram):

    • Input (State.init s): mem[0] = |s| and s occupies [1, 1 + |s|).
    • Output: zFunction s written as a length-prefixed array at y.regs Reg.out = |s| + 1 (mem[|s| + 1] = |s| and z[j] at |s| + 2 + j), preserving s at [1, 1 + |s|).
    • Word width and time: for every word width w with Fits w (2 * |s| + 1), zCode executes within 39 * |s| + 9 instructions (39 * |s| + 10 for zProgram).
  2. 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| at mem[1], |txt| at mem[2], and pat ++ txt contiguously at [3, 3 + |pat| + |txt|) (pat at [3, 3 + |pat|) and txt at [3 + |pat|, 3 + |pat| + |txt|)).
    • Workspace and output: for |pat| > 0, the Z-array of pat ++ txt is computed in [3 + |pat| + |txt|, 3 + 2 * (|pat| + |txt|)), and the matching shifts zMatches pat txt are written as a length-prefixed array at y.regs Reg.out = 3 + 2 * (|pat| + |txt|) (which also serves as the output base when pat = []). Both input blocks pat and txt are preserved.
    • Word width and time: for every word width w with Fits w (2 * |pat| + 3 * |txt| + 4), matchCode executes within 39 * |pat| + 50 * |txt| + 22 instructions (39 * |pat| + 50 * |txt| + 23 for matchProgram).

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 #

Main results #

References #

Input encoding and registers #

Pattern-matching instance on the word RAM: a pattern pat and a text txt over w-bit words.

Instances For

    Word-list layout [|pat|, |txt|] ++ pat ++ txt placed into State.init: mem[1] = |pat|, mem[2] = |txt|, pat occupies [3, 3 + |pat|), txt occupies [3 + |pat|, 3 + |pat| + |txt|), and pat ++ txt occupies [3, 3 + |pat| + |txt|).

    @[reducible, inline]

    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.

    Word-RAM exact pattern-matching code via the Z-array of pat ++ txt.

    Uniform Basis.standard Word-RAM program for Z-algorithm exact pattern matching.

    Bundled register invariants #

    Execution of extCode #

    Execution of buildZNonempty and zCode #

    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.

    Execution of matchCode #

    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.