Documentation

CslibFrontier.Algorithms.Lean.KMP.Basic

Word Borders, Periods, and the KMP Prefix Function #

This module develops the combinatorics of word borders, periods, and the Knuth–Morris–Pratt prefix (failure) function over lists with decidable equality ([KnuthMorrisPratt1977]; [CLRS2009], Section 32.4).

A length k < w.length is a proper border length of a word w (IsProperBorderLen w k) when the prefix w.take k is also a suffix of w. Equivalently, d = w.length - k is a positive period of w (IsPeriod w d), meaning w[i] = w[i + d] whenever i + d < w.length.

To treat failure-table construction and text scanning uniformly, we define maxPrefixSuffixUpTo p t B as the largest length k ≤ B with k ≤ p.length such that p.take k is a suffix of t. Specializing t = w and B = w.length - 1 yields the maximum proper border length maxBorderLen w and the KMP prefix function borderLen p q = maxBorderLen (p.take q) (corresponding to $\pi[q]$ in [CLRS2009], Section 32.4 for $1 \le q \le |p|$).

Main definitions #

Main results #

References #

One-character suffix extension #

Proper borders and periods #

k is a proper border length of w if k < w.length and the length-k prefix w.take k is also a suffix of w.

def Cslib.Algorithms.Lean.KMP.IsPeriod {α : Type u_1} (w : List α) (d : ℕ) :

d is a period of w if characters at distance d agree throughout w.

Border-period duality: k < w.length is a proper border length of w iff w.length - k is a period of w.

A proper border of a proper border of w is itself a proper border of w.

theorem Cslib.Algorithms.Lean.KMP.IsProperBorderLen.to_take {α : Type u_1} {w : List α} {j k : ℕ} (hjw : IsProperBorderLen w j) (hkw : IsProperBorderLen w k) (hlt : j < k) :

Any proper border of w strictly shorter than a proper border k of w is a proper border of w.take k.

Maximum prefix-suffix lengths, maximum borders, and minimum periods #

Largest length k ≤ B with k ≤ p.length such that p.take k <:+ t.

theorem Cslib.Algorithms.Lean.KMP.le_maxPrefixSuffixUpTo {α : Type u_1} [DecidableEq α] {p t : List α} {B k : ℕ} (hkB : k ≤ B) (hkp : k ≤ p.length) (hsuf : List.take k p <:+ t) :
theorem Cslib.Algorithms.Lean.KMP.eq_maxPrefixSuffixUpTo_iff {α : Type u_1} [DecidableEq α] {p t : List α} {B m : ℕ} :
m = maxPrefixSuffixUpTo p t B ↔ m ≤ B ∧ m ≤ p.length ∧ List.take m p <:+ t ∧ ∀ (k : ℕ), m < k → k ≤ B → k ≤ p.length → ¬List.take k p <:+ t

Maximum proper border length of w (0 when w = []).

Minimum positive period of a nonempty word w (0 when w = []).

def Cslib.Algorithms.Lean.KMP.borderLen {α : Type u_1} [DecidableEq α] (p : List α) (q : ℕ) :

The KMP prefix function $\pi[q]$: maximum proper border length of the prefix p.take q.

theorem Cslib.Algorithms.Lean.KMP.minPeriod_pos {α : Type u_1} [DecidableEq α] {w : List α} (hw : 0 < w.length) :
theorem Cslib.Algorithms.Lean.KMP.minPeriod_le_of_isPeriod {α : Type u_1} [DecidableEq α] {w : List α} {d : ℕ} (hd0 : 0 < d) (hper : IsPeriod w d) :
@[simp]
@[simp]
theorem Cslib.Algorithms.Lean.KMP.borderLen_lt {α : Type u_1} [DecidableEq α] {p : List α} {q : ℕ} (hq : 0 < q) (hp : p ≠ []) :
borderLen p q < q

Prefix-function iteration ([CLRS2009], Lemma 32.5) #

@[irreducible]

Strictly decreasing list of all proper border lengths of p.take q (empty when q = 0 or p = []), computed by iterating k ↦ borderLen p k ([CLRS2009], Lemma 32.5).

theorem Cslib.Algorithms.Lean.KMP.mem_borderChain_iff {α : Type u_1} [DecidableEq α] {p : List α} {q j : ℕ} (hq1 : 1 ≤ q) (hqp : q ≤ p.length) :

Prefix-function iteration theorem ([CLRS2009], Lemma 32.5): for 1 ≤ q ≤ p.length, borderChain p q contains exactly the proper border lengths of p.take q.

theorem Cslib.Algorithms.Lean.KMP.pairwise_gt_borderChain {α : Type u_1} [DecidableEq α] {p : List α} {q : ℕ} (hqp : q ≤ p.length) :
List.Pairwise (fun (x1 x2 : ℕ) => x1 > x2) (borderChain p q)

borderChain p q lists proper border lengths in strictly decreasing order.

Pure KMP automaton transition and master step theorem #

@[irreducible]
def Cslib.Algorithms.Lean.KMP.nextLen {α : Type u_1} [DecidableEq α] (p : List α) (k : ℕ) (c : α) :

Pure KMP transition: given a matched prefix length k < p.length (or 0 when p = []) and input character c, falls back along borderLen p until matching p[k] = c or exhausting borders.

theorem Cslib.Algorithms.Lean.KMP.nextLen_le_succ {α : Type u_1} [DecidableEq α] (p : List α) (k : ℕ) (c : α) :
nextLen p k c ≤ k + 1
theorem Cslib.Algorithms.Lean.KMP.nextLen_le_length {α : Type u_1} [DecidableEq α] (p : List α) (k : ℕ) (c : α) :
theorem Cslib.Algorithms.Lean.KMP.maxPrefixSuffixUpTo_append_singleton {α : Type u_1} [DecidableEq α] (p t : List α) (c : α) {B : ℕ} (hB : B < p.length) :

Master KMP step theorem: for any bound B < p.length and word t, extending t by one character c updates maxPrefixSuffixUpTo p t B via nextLen p · c.

theorem Cslib.Algorithms.Lean.KMP.borderLen_succ {α : Type u_1} [DecidableEq α] {p : List α} {q : ℕ} (hq1 : 1 ≤ q) (hqp : q < p.length) :
borderLen p (q + 1) = nextLen p (borderLen p q) p[q]

Recurrence for the KMP prefix function ([CLRS2009], Lemma 32.6 and Corollary 32.7): for 1 ≤ q < p.length, borderLen p (q + 1) = nextLen p (borderLen p q) p[q].

Resetting a full pattern match m = p.length (0 < p.length) to borderLen p p.length recovers the longest proper prefix of p that is a suffix of t.

When maxPrefixSuffixUpTo p t p.length < p.length, restricting the bound to p.length - 1 leaves the value unchanged.