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 #
- Centers: the centers and radii are those of
CslibFrontier.Algorithms.Lean.Palindrome.Basic([ApostolicoBreslauerGalil1995], Section 1, translated to0-indexed centersc = 0, …, 2 * n), with letters only: no sentinel symbols are interleaved, and the extension phase stops at the ends ofw. - Off-line formulation:
manacherreads the whole array and returns all radii. Manacher's original algorithm is on-line, reporting the palindromic prefixes of the input read so far; that formulation is not modeled here. The palindromic prefixes are read off the radii bypalindrome_take_iff.
Main definitions #
Cslib.Algorithms.Lean.Palindrome.extend: one outward extension phase from[l, r).Cslib.Algorithms.Lean.Palindrome.manacher: Manacher's algorithm returning the array of all2 * w.size + 1maximal palindromic radii.Cslib.Algorithms.Lean.Palindrome.Span,longestPalindromeSpan: the half-open position interval[start, stop)oflongestPalindrome w.toList.
Main results #
Cslib.Algorithms.Lean.Palindrome.add_extend: extending from any palindromic radiusk ≤ radius w.toList creachesradius w.toList c.Cslib.Algorithms.Lean.Palindrome.manacher_eq:manacher wis the arrayradius w.toList cforc < 2 * w.size + 1.Cslib.Algorithms.Lean.Palindrome.extract_longestPalindromeSpan: extractinglongestPalindromeSpan wfromw.toListyieldslongestPalindrome w.toList.
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.
An extension phase makes at most l steps before reaching the start of w.
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.
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.
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).