Documentation

CslibFrontier.Algorithms.Lean.ZFunction.Basic

The Z-algorithm #

The Z-function of a word s records, at each position i, the length of the longest common prefix of s and its suffix s.drop i. The Z-algorithm ([Gusfield1997], Section 1.4) computes all of these values from left to right. It keeps the box [l, r), the rightmost interval found so far that repeats a prefix of s. At a position i inside the box, the value z[i - l] either gives z[i] outright or shows that z[i] ≥ r - i, so explicit comparisons start at r.

Machine-level O(|s|) and O(|p| + |t|) running-time bounds on word arrays are proved in CslibFrontier.Algorithms.WordRAM.ZFunction.

Source Conventions #

Main definitions #

Main results #

References #

Specification #

The Z-function of s: at each position i < |s|, the length of the longest common prefix of s and s.drop i.

The Z-function at 0 is the length of the word.

The algorithm #

The pure Z-algorithm on arrays. The value at 0 is the length of the word; the loop computes the remaining positions from left to right.

The Z-algorithm on lists.

Correctness #

theorem Cslib.Algorithms.Lean.ZFunction.min_lcpLen_zBox {α : Type u_1} [DecidableEq α] (s : List α) {i l r : ℕ} (hli : l ≤ i) (hir : i ≤ r) (hrl : r ≤ l + s.lcpLen (List.drop l s)) :
min (s.lcpLen (List.drop (i - l) s)) (r - i) = min (s.lcpLen (List.drop i s)) (r - i)

Z-box transfer lemma ([Gusfield1997], Section 1.4): if [l, r) repeats a prefix of s (r ≤ l + List.lcpLen s (s.drop l)) and l ≤ i ≤ r, then s.drop (i - l) and s.drop i agree on their longest common prefix with s up to r - i.

Correctness of the Z-algorithm: zArray s computes the Z-function of s on every input.

Reading the Z table with default zero gives the common prefix length at every position. At and beyond the end of the array the remaining suffix is empty.

Correctness of the Z-algorithm on lists.