Algorithm Search⌘K

Proofs: Manacher's algorithm

These are the human proofs of Manacher's algorithm, at full length.

Manacher's algorithm computes the maximal palindrome at every center full

This proof shows: correctness.

Let s=s[0..n−1]s = s[0..n-1], m=2n+1m = 2n + 1, and T[0..m−1]T[0..m-1] the implicit string with T[2k]=#T[2k] = \# and T[2k+1]=s[k]T[2k+1] = s[k], where #\# is not a symbol of ss. Indices are 0-based. For 0≤c≤m−10 \le c \le m - 1 let ρ(c)\rho(c) be the largest r≥0r \ge 0 with c−r≥0c - r \ge 0, c+r≤m−1c + r \le m - 1 and T[c−r..c+r]T[c-r..c+r] a palindrome. Write W(c,q)W(c, q) for the window T[c−q..c+q]T[c-q..c+q].

Fact 1 (nesting). If W(c,q)W(c, q) is a palindrome and q≥1q \ge 1, then W(c,q−1)W(c, q-1) is a palindrome. So W(c,q)W(c, q) is a palindrome if and only if q≤ρ(c)q \le \rho(c), and if q<ρ(c)q < \rho(c), then c−q−1≥0c - q - 1 \ge 0, c+q+1≤m−1c + q + 1 \le m - 1 and T[c−q−1]=T[c+q+1]T[c-q-1] = T[c+q+1].

Proof. Removing the first and the last symbol of a palindrome leaves a palindrome. The second claim follows, because W(c,ρ(c))W(c, \rho(c)) is a palindrome and contains W(c,q)W(c, q) for q≤ρ(c)q \le \rho(c), and its symbols at distance q+1q + 1 from cc are equal. □\square

Fact 2 (mirror). Let X=T[a..b]X = T[a..b] be a palindrome, and x↦a+b−xx \mapsto a + b - x its mirror map. If W(x,q)W(x, q) lies inside [a,b][a, b], then W(a+b−x,q)W(a + b - x, q) is the reversal of W(x,q)W(x, q). So one is a palindrome if and only if the other is.

Proof. For ∣d∣≤q|d| \le q, T[x+d]=T[a+b−x−d]T[x + d] = T[a + b - x - d] because XX is a palindrome. So the window at a+b−xa + b - x, read from left to right, is the window at xx read from right to left. □\square

Lemma (mirror lemma). Let c<i<rc < i < r with r=c+ρ(c)r = c + \rho(c), and j=2c−ij = 2c - i. Then ρ(i)≥min⁡(ρ(j),r−i)\rho(i) \ge \min(\rho(j), r - i). If ρ(j)<r−i\rho(j) < r - i, then ρ(i)=ρ(j)\rho(i) = \rho(j). If ρ(j)>r−i\rho(j) > r - i, then ρ(i)=r−i\rho(i) = r - i.

Proof. Let L=c−ρ(c)=2c−rL = c - \rho(c) = 2c - r, so X=T[L..r]X = T[L..r] is a palindrome with mirror map x↦2c−xx \mapsto 2c - x, which sends ii to jj. Since c<i<rc < i < r, we have L<j<cL < j < c. Let q=min⁡(ρ(j),r−i)q = \min(\rho(j), r - i). Then W(i,q)W(i, q) lies inside [i−(r−i),r]⊆[L,r][i - (r - i), r] \subseteq [L, r], because i−(r−i)=2i−r>2c−r=Li - (r - i) = 2i - r > 2c - r = L. The window W(j,q)W(j, q) is a palindrome by Fact 1, so by Fact 2, W(i,q)W(i, q) is a palindrome, and ρ(i)≥q\rho(i) \ge q.

Case ρ(j)<r−i\rho(j) < r - i. Suppose ρ(i)≥ρ(j)+1\rho(i) \ge \rho(j) + 1. The window W(i,ρ(j)+1)W(i, \rho(j) + 1) is a palindrome, and it lies inside [L,r][L, r] because ρ(j)+1≤r−i\rho(j) + 1 \le r - i. By Fact 2, W(j,ρ(j)+1)W(j, \rho(j) + 1) is a palindrome, which contradicts the definition of ρ(j)\rho(j). So ρ(i)=ρ(j)\rho(i) = \rho(j).

Case ρ(j)>r−i\rho(j) > r - i. Let d=r−id = r - i. If r=m−1r = m - 1, then i+d+1=mi + d + 1 = m is outside TT and ρ(i)≤d\rho(i) \le d. Otherwise L≥1L \ge 1, because j−ρ(j)≥0j - \rho(j) \ge 0 and L−1=j−d−1≥j−ρ(j)L - 1 = j - d - 1 \ge j - \rho(j). By the maximality of ρ(c)\rho(c), T[L−1]≠T[r+1]T[L-1] \ne T[r+1]. Now L−1=j−(d+1)L - 1 = j - (d + 1), and since d+1≤ρ(j)d + 1 \le \rho(j), the palindrome W(j,ρ(j))W(j, \rho(j)) gives T[L−1]=T[j+d+1]T[L-1] = T[j + d + 1]. The position j+d+1=2c−2i+r+1j + d + 1 = 2c - 2i + r + 1 lies in [L,r][L, r], because i>ci > c gives 2c−2i+1≤02c - 2i + 1 \le 0. Its mirror in XX is 2c−(j+d+1)=i−d−12c - (j + d + 1) = i - d - 1, so T[j+d+1]=T[i−d−1]T[j + d + 1] = T[i - d - 1]. Together, T[i−d−1]=T[L−1]≠T[r+1]=T[i+d+1]T[i - d - 1] = T[L - 1] \ne T[r + 1] = T[i + d + 1], so W(i,d+1)W(i, d+1) is not a palindrome, and ρ(i)=d\rho(i) = d by Fact 1. □\square

Invariant. At the start of the iteration of line L4 with index ii:

(I) P[k]=ρ(k)P[k] = \rho(k) for all k<ik < i, and P[k]=0P[k] = 0 for k≥ik \ge i. If i=0i = 0, then c=r=0c = r = 0. If i≥1i \ge 1, then c<ic < i, r=c+ρ(c)r = c + \rho(c), and r=max⁡k<i(k+ρ(k))r = \max_{k < i} (k + \rho(k)).

At i=0i = 0, lines L2 and L3 make (I) true. Assume (I) at ii.

Line L5. If i<ri < r, then i≥1i \ge 1, so c<i<r=c+ρ(c)c < i < r = c + \rho(c), and 2c−i<c<i2c - i < c < i, so P[2c−i]=ρ(2c−i)P[2c - i] = \rho(2c - i). Line L5 sets P[i]=min⁡(ρ(2c−i),r−i)P[i] = \min(\rho(2c - i), r - i), which is at most ρ(i)\rho(i) by the mirror lemma. If i≥ri \ge r, P[i]P[i] stays 0. In both cases W(i,P[i])W(i, P[i]) is a palindrome and P[i]≤ρ(i)P[i] \le \rho(i).

Lines L6 and L7. The test of line L6 is "W(i,P[i]+1)W(i, P[i] + 1) lies in TT and its two new ends are equal", and since W(i,P[i])W(i, P[i]) is a palindrome, this is the test "W(i,P[i]+1)W(i, P[i] + 1) is a palindrome". By Fact 1 it succeeds exactly while P[i]<ρ(i)P[i] < \rho(i). So the loop ends with P[i]=ρ(i)P[i] = \rho(i). It ends, because P[i]P[i] grows and is bounded by ρ(i)\rho(i).

Line L8. If i+ρ(i)>ri + \rho(i) > r, line L8 sets c=ic = i and r=i+ρ(i)r = i + \rho(i); otherwise it keeps them. In both cases r=max⁡k≤i(k+ρ(k))r = \max_{k \le i}(k + \rho(k)) and r=c+ρ(c)r = c + \rho(c) with c≤i<i+1c \le i < i + 1. For i=0i = 0, ρ(0)=0\rho(0) = 0 and c=r=0c = r = 0, which is also of this form. So (I) holds at i+1i + 1.

At the end of the loop, (I) with i=mi = m gives P=ρP = \rho, which line L9 returns.

From TT to ss. Fix cc and let ρ=ρ(c)\rho = \rho(c).

Step 1: the window W(c,ρ)W(c, \rho) ends on separators. Suppose c−ρc - \rho is odd. Then c+ρc + \rho is odd too, so 1≤c−ρ1 \le c - \rho and c+ρ≤2n−1c + \rho \le 2n - 1, and both c−ρ−1c - \rho - 1 and c+ρ+1c + \rho + 1 are even positions inside TT. They hold #\#, so W(c,ρ+1)W(c, \rho + 1) is a palindrome, a contradiction. So c−ρ=2ac - \rho = 2a and c+ρ=2bc + \rho = 2b for integers 0≤a≤b≤n0 \le a \le b \le n, with a+b=ca + b = c and b−a=ρb - a = \rho.

Step 2: windows with separator ends. For 0≤a≤b≤n0 \le a \le b \le n, the string T[2a..2b]T[2a..2b] is a palindrome if and only if s[a..b−1]s[a..b-1] is a palindrome. The mirror of 2a..2b2a..2b sends even positions to even positions, where TT is always #\#, and the odd position 2k+12k + 1, which holds s[k]s[k], to 2a+2b−2k−1=2(a+b−1−k)+12a + 2b - 2k - 1 = 2(a + b - 1 - k) + 1, which holds s[a+b−1−k]s[a + b - 1 - k]. So T[2a..2b]T[2a..2b] is a palindrome exactly when s[k]=s[a+b−1−k]s[k] = s[a + b - 1 - k] for a≤k≤b−1a \le k \le b - 1, which is the definition of a palindrome for s[a..b−1]s[a..b-1].

Step 3. Each palindrome s[i..j]s[i..j] with i≤j+1i \le j + 1 corresponds by Step 2 to the palindrome T[2i..2j+2]T[2i..2j+2] with center c=i+j+1c = i + j + 1 and radius j−i+1j - i + 1. So the palindromes of ss with center cc, that is with i+j+1=ci + j + 1 = c, are the windows W(c,q)W(c, q) with even ends, and their lengths are the radii q≡c(mod2)q \equiv c \pmod 2 with q≤ρ(c)q \le \rho(c), by Fact 1. By Step 1, ρ(c)\rho(c) itself is such a radius. So the longest palindrome of ss with center cc has length P[c]=ρ(c)P[c] = \rho(c) and is s[a..b−1]s[a..b-1] with a=(c−P[c])/2a = (c - P[c])/2 and b=(c+P[c])/2b = (c + P[c])/2. And for 0≤i≤j≤n−10 \le i \le j \le n - 1, s[i..j]s[i..j] is a palindrome if and only if j−i+1≤P[i+j+1]j - i + 1 \le P[i + j + 1]. ■\blacksquare

Back to Manacher's algorithm