Documentation

CslibFrontier.Algorithms.WordRAM.Manacher

Manacher's Algorithm in Linear Time on the Word RAM #

This module implements Manacher's algorithm ([Manacher1975]; all-centers formulation as in [ApostolicoBreslauerGalil1995], Section 1) as fixed Basis.standard word-RAM programs over word arrays (State.init xs), proving linear O(|xs|) instruction bounds:

Every mirror index computation, array read and write, character load and equality test, argmax comparison, output copy, loop test, and final halt is charged by the machine semantics. The linear bound for the main loop uses ExecWithin.loop_potential with potential 11 * (|xs| - R) + 38 * (2 * |xs| + 1 - c), paying for each successful outward character match by the strictly advancing right endpoint R of the furthest palindrome seen so far.

Main definitions #

Main results #

References #

Registers and programs #

@[reducible, inline]

Left extension cursor l (1-indexed input address), and output span start.

@[reducible, inline]

Right extension cursor r (0-indexed input position), and output span stop.

@[reducible, inline]

Address register out of the length-prefixed output array.

Structured program computing all 2 * |xs| + 1 maximal palindromic radii as a length-prefixed array at address out = |xs| + 1.

Flat program returning all 2 * |xs| + 1 maximal palindromic radii via ExecutesList.

Structured program computing both longestPalindromeSpan xs.toArray in (regl, regr) and longestPalindrome xs as a length-prefixed list at out.

Flat program returning longestPalindrome xs via ExecutesList.

Execution of the extension loop #

Execution of the main Manacher loop #

All-centers Manacher on the word RAM. Starting from Data.init xs at any word width w with Fits w (3 * xs.length + 2), radiiCode executes within 87 * xs.length + 15 instructions and writes (manacher xs.toArray).toList as the length-prefixed array at y.regs out = xs.length + 1, preserving the input block [1, 1 + xs.length).

theorem Cslib.Algorithms.WordRAM.Manacher.radiiProgram_computesListInTime :
radiiProgram.ComputesListInTime (fun (x : ℕ) (xs : List ℕ) => xs) (fun (xs : List ℕ) (x : ℕ) => 87 * xs.length + 16) (fun (xs : List ℕ) (w : ℕ) => WordRAM.Fits w (3 * xs.length + 2)) fun (xs : List ℕ) => (Lean.Palindrome.manacher xs.toArray).toList

radiiProgram computes (manacher xs.toArray).toList in 87 * |xs| + 16 instructions on every fitting word array at every word width fitting 3 * |xs| + 2.

Longest palindromic span and factor #

Longest palindromic span and factor on the word RAM. Starting from Data.init xs at any word width w with Fits w (4 * xs.length + 4), longestCode executes within 116 * xs.length + 36 instructions, leaving (longestPalindromeSpan xs.toArray).start in regl, (longestPalindromeSpan xs.toArray).stop in regr, and longestPalindrome xs as the length-prefixed array at y.regs out = 3 * xs.length + 3, while preserving xs at 1 .. |xs|.

longestProgram computes longestPalindrome xs in 116 * |xs| + 37 instructions on every fitting word array at every word width fitting 4 * |xs| + 4.