Documentation

CslibFrontier.Algorithms.Lean.Palindrome.Manacher

Manacher's Algorithm: Maximal Palindromic Radii at All Centers #

This module implements Manacher's algorithm ([Manacher1975]; all-centers formulation as in [ApostolicoBreslauerGalil1995], Section 1) as a pure function over arrays w : Array α with [DecidableEq α]: manacher w computes radius w.toList c for each of the 2 * w.size + 1 centers c, and longestPalindromeSpan w extracts the half-open index interval [start, stop) of longestPalindrome w.toList.

The algorithm scans the centers left to right, maintaining the center C whose maximal palindrome reaches furthest right, ending at R. At a new center c, the mirror lemma min_radius_mirror says that the radius about c agrees with the already computed radius m about 2 * C - c up to b = R - (c + 1) / 2. If m < b, the radius about c is m immediately; otherwise extend grows the palindrome of radius b about c outward letter by letter.

Linear O(|w|) machine-level running time for both all-centers radii and the longest palindromic factor on word arrays is proved in CslibFrontier.Algorithms.WordRAM.Manacher. For distinct palindromic words, List.card_palindromicFactors_le counts the empty factor as well. The palindromic tree uses this result in Eertree.vertexCount_le to bound its number of vertices, including both roots.

Source Conventions #

Main definitions #

Main results #

References #

The extension phase #

One extension phase: starting from the factor [l, r), compare w[l - 1] with w[r] and grow the factor while they agree, stopping at the ends of w. Returns the number of successful extensions.

theorem Cslib.Algorithms.Lean.Palindrome.extend_le {α : Type u_1} [DecidableEq α] (w : Array α) (l r : ℕ) :
extend w l r ≤ l

An extension phase makes at most l steps before reaching the start of w.

theorem Cslib.Algorithms.Lean.Palindrome.add_extend_le {α : Type u_1} [DecidableEq α] (w : Array α) (l : ℕ) {r : ℕ} (hr : r ≤ w.size) :
r + extend w l r ≤ w.size

An extension phase never moves the right end past the end of w.

theorem Cslib.Algorithms.Lean.Palindrome.add_extend {α : Type u_1} [DecidableEq α] {w : Array α} {c k : ℕ} (hk : k ≤ radius w.toList c) :
k + extend w (c / 2 - k) ((c + 1) / 2 + k) = radius w.toList c

From any palindromic radius k ≤ radius w.toList c, the extension phase reaches the maximal radius about c.

The main loop #

Manacher's algorithm: the maximal palindromic radius about each of the 2 * w.size + 1 centers of w.

theorem Cslib.Algorithms.Lean.Palindrome.manacher_eq {α : Type u_1} [DecidableEq α] (w : Array α) :
manacher w = Array.ofFn fun (x : Fin (2 * w.size + 1)) => radius w.toList ↑x

Correctness of Manacher's algorithm: it returns the maximal palindromic radii about all 2 * w.size + 1 centers of w.

Longest palindromic factor #

Half-open position interval [start, stop) of a factor in a word.

  • start : ℕ

    Inclusive start position.

  • stop : ℕ

    Exclusive end position.

Instances For

    The interval [start, stop) of longestPalindrome w.toList, read off manacher w.

    longestPalindromeSpan w gives the endpoints of longestPalindrome w.toList, a longest palindromic factor of w (length_le_longestPalindrome).