Documentation

CslibFrontier.Algorithms.Lean.ZFunction.Search

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 #

Main definitions #

Main results #

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.

Correctness of Z-algorithm matching: i ∈ zMatches p t iff p occurs in t at shift i.

theorem Cslib.Algorithms.Lean.ZFunction.pairwise_lt_zMatches {α : Type u_1} [DecidableEq α] (p t : List α) :
List.Pairwise (fun (x1 x2 : ℕ) => x1 < x2) (zMatches p t)

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.