Documentation

CslibFrontier.Algorithms.Lean.Palindrome.Basic

Maximal palindromes at all centers #

A word w of length n has 2 * n + 1 centers c = 0, …, 2 * n: an odd center c is the letter at position c / 2, and an even center c is the gap before position c / 2 (with c = 2 * n the gap after the last letter). A factor w.extract a b has center a + b, so w.extract (c / 2 - k) ((c + 1) / 2 + k) is the factor with radius k about c: its length is 2 * k + c % 2. Palindromes are Mathlib's List.Palindrome.

radius w c is the maximal radius of a palindromic factor about c. This is the all-centers formulation of Apostolico, Breslauer and Galil, Section 1: their even palindrome of radius R centered at position k (1-indexed, letters S[k - R .. k + R - 1]) has center c = 2 * (k - 1) here, and their odd palindrome of radius R centered on position k (letters S[k - R .. k + R]) has center c = 2 * k - 1; the maximal radii agree. The extra center c = 2 * n always has radius 0.

Every palindromic factor is determined by the radius at its center (palindrome_extract_iff_le_radius), so the longest palindromic factor (longestPalindrome) and the palindromic prefixes (palindrome_take_iff) are read off the radii. Computing all radii in linear time is Manacher's algorithm (CslibFrontier.Algorithms.Lean.Palindrome.Manacher); its key step is the mirror lemma min_radius_mirror.

Main definitions #

Main results #

References #

Palindromic radii #

k is a palindromic radius about center c of w: the factor w.extract (c / 2 - k) ((c + 1) / 2 + k) lies inside w and is a palindrome.

Instances For

    The maximal palindromic radius about center c of w (0 when c > 2 * w.length).

    theorem Cslib.Algorithms.Lean.Palindrome.IsPalRadius.mono {α : Type u_1} {w : List α} {c k k' : ℕ} (h : IsPalRadius w c k) (hk : k' ≤ k) :
    theorem Cslib.Algorithms.Lean.Palindrome.isPalRadius_zero {α : Type u_1} {w : List α} {c : ℕ} (hc : c ≤ 2 * w.length) :
    theorem Cslib.Algorithms.Lean.Palindrome.isPalRadius_succ_iff {α : Type u_1} {w : List α} {c k : ℕ} (hk : IsPalRadius w c k) (hl : k < c / 2) (hr : (c + 1) / 2 + k < w.length) :
    IsPalRadius w c (k + 1) ↔ w[c / 2 - k - 1]? = w[(c + 1) / 2 + k]?

    The extension step: a palindromic radius k about c with room on both sides extends to k + 1 iff the two letters just outside its factor agree.

    theorem Cslib.Algorithms.Lean.Palindrome.isPalRadius_iff_le_radius {α : Type u_1} [DecidableEq α] {w : List α} {c k : ℕ} (hc : c ≤ 2 * w.length) :

    The palindromic radii about a center c ≤ 2 * w.length are exactly 0, …, radius w c.

    theorem Cslib.Algorithms.Lean.Palindrome.add_radius_le_length {α : Type u_1} [DecidableEq α] (w : List α) {c : ℕ} (hc : c ≤ 2 * w.length) :
    (c + 1) / 2 + radius w c ≤ w.length
    theorem Cslib.Algorithms.Lean.Palindrome.isPalRadius_succ_of_lt_radius {α : Type u_1} [DecidableEq α] {w : List α} {c k : ℕ} (hk : k < radius w c) :
    IsPalRadius w c (k + 1)
    theorem Cslib.Algorithms.Lean.Palindrome.palindrome_extract_iff_le_radius {α : Type u_1} [DecidableEq α] {w : List α} {a b : ℕ} (hab : a ≤ b) (hb : b ≤ w.length) :
    (w.extract a b).Palindrome ↔ b - a ≤ 2 * radius w (a + b) + (a + b) % 2

    Palindromic factors are characterized by the radius at their center: for a ≤ b ≤ w.length, w.extract a b is a palindrome iff b - a ≤ 2 * radius w (a + b) + (a + b) % 2.

    theorem Cslib.Algorithms.Lean.Palindrome.min_radius_mirror {α : Type u_1} [DecidableEq α] {w : List α} {C c R : ℕ} (hCc : C ≤ c) (hR : R = (C + 1) / 2 + radius w C) (hRn : R ≤ w.length) :
    min (radius w c) (R - (c + 1) / 2) = min (radius w (2 * C - c)) (R - (c + 1) / 2)

    Mirror lemma (the step of Manacher's algorithm). If the maximal palindrome about center C ends at R = (C + 1) / 2 + radius w C, then for every center c ≥ C the radii about c and about its mirror image 2 * C - c, truncated at the distance R - (c + 1) / 2 from the right end of that palindrome, agree.

    Longest palindromic factors and palindromic prefixes #

    The maximal palindromic factor about center c of w (empty when c > 2 * w.length).

    theorem Cslib.Algorithms.Lean.Palindrome.length_maxPalindrome {α : Type u_1} [DecidableEq α] (w : List α) {c : ℕ} (hc : c ≤ 2 * w.length) :
    (maxPalindrome w c).length = 2 * radius w c + c % 2
    theorem Cslib.Algorithms.Lean.Palindrome.exists_length_le_radius_of_isInfix {α : Type u_1} [DecidableEq α] {w u : List α} (hu : u <:+: w) (hpal : u.Palindrome) :
    ∃ c ≤ 2 * w.length, u.length ≤ 2 * radius w c + c % 2

    Every palindromic factor of w is at most as long as the maximal palindrome about its center.

    A center of w whose maximal palindrome is longest.

    bestCenter w is always one of the 2 * w.length + 1 centers 0, …, 2 * w.length.

    A longest palindromic factor of w: the maximal palindrome about bestCenter w.

    longestPalindrome w is at least as long as every palindromic factor of w.

    theorem Cslib.Algorithms.Lean.Palindrome.palindrome_take_iff {α : Type u_1} [DecidableEq α] {w : List α} {k : ℕ} (hk : k ≤ w.length) :

    Palindromic prefixes are read off the radii: for k ≤ w.length, the prefix w.take k is a palindrome iff the maximal palindrome about center k reaches the start of w.