Algorithm Search⌘K

Proofs: Palindromic tree (eertree)

These are the human proofs of Palindromic tree (eertree), at full length.

Correctness of the palindromic tree full

This proof shows: correctness.

We prove that after each call of ADD the structure satisfies the invariant below, for the text t=t[0..i]t = t[0..i] read so far. Indices are 0-based, t[a..b]t[a..b] includes both ends, and pal(v)\mathrm{pal}(v) is the palindrome of node vv. For the two roots, pal\mathrm{pal} is a string of length −1-1 (a formal object with c pal(0) c=cc\,\mathrm{pal}(0)\,c = c) and the empty string.

Invariant.

  1. The nodes other than the roots are in one-to-one correspondence with the distinct nonempty palindromes that occur in tt, and len[v]=∣pal(v)∣\mathrm{len}[v] = |\mathrm{pal}(v)|.
  2. For each node v≥2v \ge 2, link[v]\mathrm{link}[v] is the node of the longest proper palindromic suffix of pal(v)\mathrm{pal}(v), the empty root if there is none; link[1]=0\mathrm{link}[1] = 0 and link[0]=0\mathrm{link}[0] = 0.
  3. to[v][c]=w\mathrm{to}[v][c] = w if and only if pal(w)=c pal(v) c\mathrm{pal}(w) = c\,\mathrm{pal}(v)\,c and this palindrome occurs in tt.
  4. last\mathrm{last} is the node of the longest palindromic suffix of tt, and the empty root when tt is empty.

At the start, tt is empty, there are only the two roots, and the invariant holds.

Lemma 1 (the chain). If the invariant holds for t[0..i−1]t[0..i-1], the nodes last,link[last],link2[last],…\mathrm{last}, \mathrm{link}[\mathrm{last}], \mathrm{link}^2[\mathrm{last}], \ldots are all the palindromic suffixes of t[0..i−1]t[0..i-1], the empty one included, in strictly decreasing length, followed by the imaginary root.

Proof. Let pp be a palindromic suffix of t[0..i−1]t[0..i-1] and qq a palindromic suffix of t[0..i−1]t[0..i-1] shorter than pp. Then qq is a suffix of pp. So the palindromic suffixes of t[0..i−1]t[0..i-1] in decreasing length are: the longest one, pal(last)\mathrm{pal}(\mathrm{last}) by part 4, then the longest proper palindromic suffix of it, and so on, which by part 2 are the nodes of the chain. The chain reaches the empty root, whose link is the imaginary root. □\square

Lemma 2 (the new longest suffix). Let c=t[i]c = t[i]. The longest palindromic suffix of t[0..i]t[0..i] is c pal(v) cc\,\mathrm{pal}(v)\,c for the first node vv on the chain of Lemma 1 that fits at ii for cc, that is, with j=i−len[v]−1≥0j = i - \mathrm{len}[v] - 1 \ge 0 and t[j]=ct[j] = c.

Proof. A palindromic suffix of t[0..i]t[0..i] of length at least 2 has the form c q cc\,q\,c, where qq is a palindromic suffix of t[0..i−1]t[0..i-1], possibly empty, preceded by cc; and the suffix cc of length 1 is c pal(0) cc\,\mathrm{pal}(0)\,c. Conversely, each node of the chain that fits gives such a suffix. The imaginary root always fits, since j=ij = i. Longer qq give longer suffixes, and the chain is in decreasing length, so the first node that fits gives the longest one. □\square

Lemma 3 (at most one new palindrome). Each distinct palindrome of t[0..i]t[0..i] that does not occur in t[0..i−1]t[0..i-1] ends at ii, and only the longest palindromic suffix PP of t[0..i]t[0..i] can be one.

Proof. A new occurrence must end at ii, so it is a palindromic suffix of t[0..i]t[0..i]. Let qq be a palindromic suffix shorter than PP. Then qq is a suffix of PP, and since PP and qq are palindromes, q=qRq = q^R is also a prefix of PP. That occurrence ends at position i−∣P∣+∣q∣<ii - |P| + |q| < i, so qq occurs in t[0..i−1]t[0..i-1]. □\square

Lemma 4 (the suffix link of a new node). If P=c pal(v) cP = c\,\mathrm{pal}(v)\,c is new and ∣P∣≥2|P| \ge 2, the longest proper palindromic suffix of PP is c pal(u) cc\,\mathrm{pal}(u)\,c for the first node uu after vv on the chain of Lemma 1 that fits at ii for cc, and it has a node.

Proof. The proper palindromic suffixes of PP are the palindromic suffixes of t[0..i]t[0..i] shorter than PP. By the proof of Lemma 2, they are c pal(u) cc\,\mathrm{pal}(u)\,c for the nodes uu after vv on the chain that fit, and the first one gives the longest. The node u=0u = 0 fits, so uu exists, and the result has length 1 when u=0u = 0. By Lemma 3 this palindrome occurs in t[0..i−1]t[0..i-1], so by parts 1 and 3 for t[0..i−1]t[0..i-1], the node to[u][c]\mathrm{to}[u][c] exists and is its node. If ∣P∣=1|P| = 1, then PP has no nonempty proper palindromic suffix and its link is the empty root, which line L5 sets. □\square

Theorem. If the invariant holds for t[0..i−1]t[0..i-1], it holds for t[0..i]t[0..i] after ADD(t[i])(t[i]), and ADD returns true exactly when t[0..i]t[0..i] has a palindrome that t[0..i−1]t[0..i-1] does not have.

Proof. Line L2 walks the chain of Lemma 1 and stops at the first node vv that fits, which exists because the imaginary root fits. By Lemma 2, P=c pal(v) cP = c\,\mathrm{pal}(v)\,c is the longest palindromic suffix of t[0..i]t[0..i]. By part 3, to[v][c]\mathrm{to}[v][c] exists if and only if PP occurs in t[0..i−1]t[0..i-1]. If it does, line L3 sets last\mathrm{last} to its node, which gives part 4; by Lemma 3 no palindrome is new, so parts 1 to 3 still hold, and the result false is correct. Otherwise PP is the only new palindrome, by Lemma 3. Line L4 makes one node ww for it with len[w]=len[v]+2=∣P∣\mathrm{len}[w] = \mathrm{len}[v] + 2 = |P|, which keeps part 1. Lines L5 to L8 set link[w]\mathrm{link}[w] as in Lemma 4, since the walk of line L7 starts at link[v]\mathrm{link}[v], the node after vv on the chain; this gives part 2 for ww, and the links of the other nodes do not change. Line L9 adds the one edge v→wv \to w by cc, the only new pair for part 3, since PP is the only new palindrome and P=c pal(v) cP = c\,\mathrm{pal}(v)\,c for exactly one node vv and symbol cc. Finally last=w\mathrm{last} = w gives part 4, and the result true is correct. Each walk follows links that strictly decrease the length, from at most ii down to −1-1, so ADD stops. By induction on ii, the invariant holds after every call. ■\blacksquare

Back to Palindromic tree (eertree)