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 #
Cslib.Algorithms.Lean.KMP.IsProperBorderLen:k < w.lengthandw.take k <:+ w.Cslib.Algorithms.Lean.KMP.IsPeriod: perioddofw(w[i] = w[i + d]fori + d < |w|).Cslib.Algorithms.Lean.KMP.maxPrefixSuffixUpTo: largestk ≤ Bwithk ≤ |p|andp.take k <:+ t.Cslib.Algorithms.Lean.KMP.maxBorderLen: maximum proper border length ofw(0on[]).Cslib.Algorithms.Lean.KMP.minPeriod: minimum positive periodw.length - maxBorderLen w.Cslib.Algorithms.Lean.KMP.borderLen: KMP prefix valuemaxBorderLen (p.take q).Cslib.Algorithms.Lean.KMP.borderChain: strictly decreasing list of all proper border lengths ofp.take qobtained by iteratingk ↦ borderLen p k.Cslib.Algorithms.Lean.KMP.nextLen: pure KMP automaton transition updating a matched prefix lengthk < p.lengthupon reading characterc.
Main results #
Cslib.Algorithms.Lean.KMP.isProperBorderLen_iff_isPeriod:IsProperBorderLen w kholds iffk < w.lengthandIsPeriod w (w.length - k).Cslib.Algorithms.Lean.KMP.IsProperBorderLen.transandIsProperBorderLen.to_take: proper borders ofwstrictly shorter than a proper borderkare exactly the proper borders ofw.take k.Cslib.Algorithms.Lean.KMP.mem_borderChain_iffandpairwise_gt_borderChain: iteratingk ↦ borderLen p kenumerates all proper border lengths ofp.take qin strictly decreasing order ([CLRS2009], Lemma 32.5).Cslib.Algorithms.Lean.KMP.maxPrefixSuffixUpTo_append_singleton: extendingtby one charactercupdatesmaxPrefixSuffixUpTo p t B(B < p.length) vianextLen p · c, specializing both toborderLen_succ([CLRS2009], Lemma 32.6 and Corollary 32.7) and to the KMP text-scanning step.
References #
One-character suffix extension #
Proper borders and periods #
A proper border of a proper border of w is itself a proper border of w.
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.
Maximum proper border length of w (0 when w = []).
Minimum positive period of a nonempty word w (0 when w = []).
The KMP prefix function $\pi[q]$: maximum proper border length of the prefix p.take q.
Prefix-function iteration ([CLRS2009], Lemma 32.5) #
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).
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.
borderChain p q lists proper border lengths in strictly decreasing order.
Pure KMP automaton transition and master step theorem #
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.
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.
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.