Documentation

CslibFrontier.Algorithms.Lean.ZFunction.FromPrefix

From a KMP prefix table to a Z-array #

prefixWord assigns a representative index to each position, reading the prefix table from left to right. A positive proper border forces its last letter to equal the last letter of the corresponding prefix. A zero entry leaves the position free to introduce a fresh letter.

fromPrefix_computePrefix proves that the existing Z algorithm on this word recovers the Z-array of every original word realizing the table. Representatives may distinguish equal original letters. They preserve every match against the start of the word.

The abstract of Bland, Kucherov and Smyth describes conversion between their prefix table (the Z-array here) and their border array (the KMP prefix table here). We use zero-based arrays, including z[0] = n for a nonempty word. prefixTableEquiv pairs the two executable conversions on tables realized over any fixed alphabet.

A word of representative indices determined by a numeric prefix table. At position i, a positive entry k ≤ i copies the representative at k - 1. A zero or out-of-range entry introduces i as a fresh letter.

theorem Cslib.Algorithms.Lean.ZFunction.getElem_prefixWord (pi : Array ℕ) (i : ℕ) (hi : i < pi.size) :
(prefixWord pi)[i] = if h : 0 < pi[i] ∧ pi[i] ≤ i then (prefixWord pi)[pi[i] - 1] else i

Each entry of the representative word is either a previously assigned letter or its own position. This recurrence also specifies the result on malformed tables.

Convert a numeric KMP prefix table into a Z-array by executing the Z algorithm on its word of representative indices.

Conversion from the KMP table recovers the Z-array for every input word, including the empty word.

Z-tables and KMP prefix tables realizable over the same alphabet are in bijection. Both directions compute on the numeric table alone. A word witnessing realizability is used only to prove that the outputs belong to the stated ranges and that the conversions are inverse.