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:
radiiCodeandradiiProgramcompute the length-prefixed list of all2 * |xs| + 1maximal palindromic radii(manacher xs.toArray).toListat address|xs| + 1within87 * |xs| + 16instructions wheneverFits w (3 * |xs| + 2)andInputFits w xshold.longestCodeandlongestProgramadditionally scan the computed radii to locatebestCenter xsandlongestPalindromeSpan xs.toArrayin registers(regl, regr)and copy the factor to a length-prefixed block at address3 * |xs| + 3, returninglongestPalindrome xswithin116 * |xs| + 37instructions wheneverFits w (4 * |xs| + 4)andInputFits w xshold.
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 #
Cslib.Algorithms.WordRAM.Manacher.Reg.regl,Reg.regr,Reg.out: output span and array-header registers (Fin 16).Cslib.Algorithms.WordRAM.Manacher.radiiCode,radiiProgram: structured and flat programs computing all2 * |xs| + 1maximal palindromic radii.Cslib.Algorithms.WordRAM.Manacher.longestCode,longestProgram: structured and flat programs computinglongestPalindromeSpan xs.toArrayandlongestPalindrome xs.
Main results #
Cslib.Algorithms.WordRAM.Manacher.radiiCode_exec,radiiProgram_computesListInTime: all maximal palindromic radii in87 * |xs| + 16word-RAM instructions at widths fitting3 * |xs| + 2.Cslib.Algorithms.WordRAM.Manacher.longestCode_exec,longestProgram_computesListInTime: longest palindromic span and factor in116 * |xs| + 37word-RAM instructions at widths fitting4 * |xs| + 4.
References #
Registers and programs #
Left extension cursor l (1-indexed input address), and output span start.
Right extension cursor r (0-indexed input position), and output span stop.
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).
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.