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 read so far. Indices are 0-based, includes both ends, and is the palindrome of node . For the two roots, is a string of length (a formal object with ) and the empty string.
Invariant.
- The nodes other than the roots are in one-to-one correspondence with the distinct nonempty palindromes that occur in , and .
- For each node , is the node of the longest proper palindromic suffix of , the empty root if there is none; and .
- if and only if and this palindrome occurs in .
- is the node of the longest palindromic suffix of , and the empty root when is empty.
At the start, is empty, there are only the two roots, and the invariant holds.
Lemma 1 (the chain). If the invariant holds for , the nodes are all the palindromic suffixes of , the empty one included, in strictly decreasing length, followed by the imaginary root.
Proof. Let be a palindromic suffix of and a palindromic suffix of shorter than . Then is a suffix of . So the palindromic suffixes of in decreasing length are: the longest one, 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.
Lemma 2 (the new longest suffix). Let . The longest palindromic suffix of is for the first node on the chain of Lemma 1 that fits at for , that is, with and .
Proof. A palindromic suffix of of length at least 2 has the form , where is a palindromic suffix of , possibly empty, preceded by ; and the suffix of length 1 is . Conversely, each node of the chain that fits gives such a suffix. The imaginary root always fits, since . Longer give longer suffixes, and the chain is in decreasing length, so the first node that fits gives the longest one.
Lemma 3 (at most one new palindrome). Each distinct palindrome of that does not occur in ends at , and only the longest palindromic suffix of can be one.
Proof. A new occurrence must end at , so it is a palindromic suffix of . Let be a palindromic suffix shorter than . Then is a suffix of , and since and are palindromes, is also a prefix of . That occurrence ends at position , so occurs in .
Lemma 4 (the suffix link of a new node). If is new and , the longest proper palindromic suffix of is for the first node after on the chain of Lemma 1 that fits at for , and it has a node.
Proof. The proper palindromic suffixes of are the palindromic suffixes of shorter than . By the proof of Lemma 2, they are for the nodes after on the chain that fit, and the first one gives the longest. The node fits, so exists, and the result has length 1 when . By Lemma 3 this palindrome occurs in , so by parts 1 and 3 for , the node exists and is its node. If , then has no nonempty proper palindromic suffix and its link is the empty root, which line L5 sets.
Theorem. If the invariant holds for , it holds for after ADD, and ADD returns true exactly when has a palindrome that does not have.
Proof. Line L2 walks the chain of Lemma 1 and stops at the first node that fits, which exists because the imaginary root fits. By Lemma 2, is the longest palindromic suffix of . By part 3, exists if and only if occurs in . If it does, line L3 sets 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 is the only new palindrome, by Lemma 3. Line L4 makes one node for it with , which keeps part 1. Lines L5 to L8 set as in Lemma 4, since the walk of line L7 starts at , the node after on the chain; this gives part 2 for , and the links of the other nodes do not change. Line L9 adds the one edge by , the only new pair for part 3, since is the only new palindrome and for exactly one node and symbol . Finally gives part 4, and the result true is correct. Each walk follows links that strictly decrease the length, from at most down to , so ADD stops. By induction on , the invariant holds after every call.