Exact pattern matching with the Z-algorithm #
The pattern p occurs in the text t at shift i iff the Z-function of p ++ t is at least
|p| at position |p| + i ([Gusfield1997], Section 1.5). zMatchesArray p t and zMatches p t
compute the Z-function of p ++ t with the Z-algorithm and read off these shifts; the output agrees
with the KMP specification KMP.kmpMatchesList p t (zMatches_eq_kmpMatchesList).
Linear O(|p| + |t|) machine-level running-time bounds on word arrays are proved in
CslibFrontier.Algorithms.WordRAM.ZFunction.
Source Conventions #
- Shifts:
0-indexed, as inCslib.Algorithms.Lean.KMP. The shift|t|is reported exactly whenpis empty. - No separator: Gusfield uses
p $ twith a separator$that occurs in neither word. Here there is no separator: values of the Z-function above|p|are allowed, and a shiftiis reported when the value at|p| + iis at least|p|.
Main definitions #
Cslib.Algorithms.Lean.ZFunction.zMatchesArray,zMatches: the shifts ofpintread off the Z-function ofp ++ t.
Main results #
Cslib.Algorithms.Lean.ZFunction.mem_zMatches_iffandzMatches_eq_filter_range:i ∈ zMatches p tiffi ≤ |t|andpis a prefix oft.drop i, giving a kernel-decidable closed form(List.range (t.length + 1)).filter (fun i => decide (p <+: t.drop i)).Cslib.Algorithms.Lean.ZFunction.pairwise_lt_zMatches:zMatches p tis strictly increasing.Cslib.Algorithms.Lean.ZFunction.zMatches_eq_kmpMatchesList: the Z-algorithm and KMP report the same shifts.
References #
The shifts i ≤ |t| at which p occurs in t, read off the Z-array of p ++ t: those
whose value at |p| + i is at least |p|. The value past the end is taken to be 0.
Exact pattern matching with the Z-algorithm on lists.
zMatches p t lists the shifts in strictly increasing order.
Explicit filter characterization of zMatches, useful for kernel decide evaluation in
downstream module clients without unfolding the Z-algorithm loop.
Z-algorithm matching agrees with KMP: both report the shifts of p in t in increasing
order.