Palindromic tree.

*
Disclaimer: I am not an inventor of palindromic tree and don't have any rights on it. The inventor of palindromic tree is
Mikhail Rubinchik, and this article just describes basic principles of
his beautiful data structure.
*

This blog post will describe palindromic tree — a nice data structure allowing to solve some interesting problems involving palindromes. This data structure is quite new, and even Google still doesn't provide any information about it. As far as I know the name "palindromic tree" is also not an official name yet. Would you come up with a better name after reading this article?

Thanks to Mikhail Rubinchik, inventor of this data structure, for presenting it during Petrozavodsk Summer Camp 2014 and thus making creation of this article possible.

Structure of palindromic tree.

As in any tree-like data structure in palindromic tree we have nodes. Here each node represents a palindrome.

Example of 4 nodes of palindromic tree.

In addition to nodes we also have edges between them. Directed edge between nodes *u* and *v* marked with some letter - let's say X - means that
we can obtain the palindrome of node *v* by adding letter X to the both sides of the palindrome of node *u*. Please refer to the picture below for
better understanding.

We get palindrome aba by adding letter a to the both sides of palindrome b.

And finally we also have suffix links. A suffix link from node *u* goes to such node *w*, that the palindrome of node *w* is the
largest palindrome which is also a proper suffix of the palindrome of node *u* (suffix is a substring of the string which contains its last character.
Proper suffix is such suffix, which is not equal to the whole string itself). Hereafter we will call the largest palindrome which is also a proper
suffix of a string just largest suffix-palindrome of a string.

We added suffix link (dashed line) from aba to a because a is the largest suffix-palindrome of string aba.

Name palindromic tree is a bit confusing, since this data structure is not an ordinary tree, because here we have two roots. First root will contain a fictive palindrome string of length -1. This strange root is created for convenience — we will agree that adding some letter to both sides of this "string" will produce a new string of length one and consisting of exactly that letter. Second root will just represent an empty string, which is also a palindrome, and will be of length 0. We also set suffix links of both roots going to the root with length -1.

Note that we don't actually store palindrome strings in the nodes, because it would take too much memory. Implementation of node structure will consist only of its palindrome length, edges on all letters of the given alphabet (possibly with some null edges) and suffix link.

Construction of palindromic tree.

Let's learn how to build the palindromic tree for some given string *s*, that is, a palindromic tree which will consist of all different
palindrome substrings of *s*. Because of the fact that any string of length *n* has not more than *n* different palindrome substrings
(try to prove this yourself if you didn't know it before, it is not hard) our palindromic tree will also have not more than length of the given string plus
two nodes, where two extra nodes are roots.

We will process the string letter by letter. Suppose we already processed some prefix (beginning of the string) *p*, and now we want to add
the next letter of the string, say x.

We will also maintain the largest suffix-palindrome of the processed prefix *p*, let's call it *t*.

Since *t* is in already processed prefix, it corresponds to some node of the palindromic tree. This node has a suffix link to some other
existing node, which also has a suffix link and so on.

Chain of suffix links from *t*.

Now let's find the suffix-palindrome of new prefix consisting of *p* plus x.
This suffix-palindrome will look like xAx, where A is some string, possibly empty (or it is our fictive root string of length -1, when suffix-palindrome is just x).
Since xAx is a palindrome, A is also a palindrome, and moreover it is some suffix of *p*, therefore it can be reached from
*t* by suffix links.

The string xAx is the only palindrome substring of *p* + x, that we possibly never saw in *p*. To understand why this is so,
notice that all new palindrome substrings which we didn't meet in *p* must end on that last letter x, and therefore be the suffix-palindromes
of *p* + x. Because xAx is the largest
suffix-palindrome of *p* + x, all other smaller suffix-palindromes of it can be found in some prefix of xAx (since for any suffix of
the palindrome there is a corresponding similar prefix), and therefore we already
saw them in *p*.

So, to process new letter x we just go along suffix links of *t* until we find an appropriate A (and we always find some, possibly of length -1 if
we trace suffix links back to the roots). Then we check if there is an edge on letter x from the node corresponding to A, and if not, set this edge going
to the new node corresponding to xAx.

What about suffix link of the node xAx? If this node already existed before, the suffix link was also already set and we don't need to do anything. If not, then we need to
find the largest suffix-palindrome of xAx, which will be in a form of xBx, where B is some string, possibly empty. By the same logic that we used before,
B is a suffix-palindrome of *p* and can be reached from *t* by suffix links.

We ended up with the following algorithm of construction of the palindromic tree. Process the string letter by letter, always maintaining the largest suffix-palindrome
*t* of the processed prefix (initially *t* is an empty string). When adding new letter *x*, we need to go along suffix link chain from *t*, until we find some
palindrome A, which can be extended with x on both sides. The string xAx will be our new largest suffix-palindrome and the only candidate for the new node of
the tree. To obtain its suffix link we need to continue going along suffix link chain, until we find some another palindrome B, which also can be extended with
x on both sides, and the suffix link from xAx will go to xBx.

For better understanding of how it is all implemented, take a look on my code of the palindromic tree (ignore variable num in the node structure, it is specific to the problem which this particular code solves). As you can see, the code is not very long and not very hard to write.

Time complexity

To understand how long this algorithm works let's take a look at what happens as we build the palindromic tree. We may notice that as
we process string letter by letter, *left* bound of the largest suffix-palindrome of the processed prefix goes only to the right as we
iterate through suffix link chain. This bound can move to the right only n times, where n is the length of the given string. The same logic applies
to the left bound of the string to which the suffix link of the largest suffix-palindrome points. So, overall time complexity of this algorithm is
*O(length of the string)* or *O(n)*.

Applications

Let's take a look at some problems that can be solved using the palindromic tree.

How many new palindromes are added?

We have a problem that asks how many new palindrome substrings are added to the string if we append a letter to its end. For example appending letter *a*
to the string *aba* (which already has subpalindromes a, b and aba) adds new palindrome aa.

We know that the number of new subpalindromes can be either 0 or 1. The solution of this problem is straightforward — we need to build the palindromic tree letter by letter, and for any new letter we can answer how many new palindromes were added just by inspecting the number of newly created nodes of the tree.

The number of palindrome substrings.

In this problem we have to answer how many palindrome substrings the given string has. For example the string *aba* has four — two times a, one time b
and one time aba.

We will solve this problem along with the construction of the palindromic tree for the given string. When we process a new letter we add to the answer
all palindromes that contain this new letter. But these palindromes are the new longest suffix-palindrome *t* (which contains new letter)
and all palindromes that can be reached from it by suffix links. To quickly find the number of them let's store in each node the length of the suffix
link chain from it (including this node), then we need just to add this value for *t* to the answer as we process new letter. This value itself is
calculated very simple: for roots it is zero, for other nodes it is just this value in the suffix link node (which was created and handled earlier)
plus one.

You may see the code solving this problem here.

This problem can also be solved by Manacher's algorithm
that also runs in *O(n)* (code of Manacher's algorithm solving this problem is available here).
However, in my opinion, the palindromic tree is easier to write and it can be extended for many other types of problems.

The number of occurrences of the palindromes.

Another problem asks for the number of occurrences of each subpalindrome in the string, and it also can be solved using the palindromic tree.

Note that when we add a new letter, it increases the number of occurrences of the new longest suffix-palindrome *t* (which contains new letter)
and all palindromes that can be reached from it by suffix links.

To quickly update these values we will increase some lazy flag in *t* node, which means that we later increase the number of occurrences of it
and all nodes reachable from *t* node by suffix links. After we are done with the construction of the palindromic tree, we
iterate through the list of nodes from the end to the beginning (from the lately created nodes to the early ones) and propagate this
lazy flag from the node to its suffix link node — update the number of occurrences of the current node by the value of the flag and then
add this value to the flag of the current node's suffix link.

In the end, we get the palindromic tree with each node containing the number of its occurrences and the construction runtime remains the same
*O(n)*.

Conclusion

The code of palindromic tree is available here. You can practice palindromic tree in online judges on this and this problems.

All comments, questions and notices about any errors in the text are more than welcome.

P.S. Комментарии и вопросы на русском языке также приветствуются :)

25 September 2014

13 comments

Darkhan28 September 2014, at 23:09Thanks, Adilet. Nice blog. Keep it up!

Temirulan16 November 2014, at 22:53Thanks, ADJA)

Anuar Talipof23 February 2015, at 03:55Как считать occurence-ы палиндромов. Я так и не понял.

ADJA23 February 2015, at 04:41Anuar Talipof, подсчет идет вот так: когда добавляется буква, встречается еще один раз новый longest suffix-palindrome (LSP), а также по еще одному разу все, что достижимо из него по суффиксным ссылкам. Однако обновлять все сразу слишком долго, поэтому мы увеличиваем счетчик только в LSP и говорим, что по суффиксным ссылкам увеличим значение потом, когда закончим обработку строки (что-то вроде lazy propagation в дереве отрезков).

Вот код который выводит все подпалиндромы строки и количество раз, которые они встречаются: http://pastebin.com/A9azCttf. Из-за вывода это работает за квадрат, без вывода подсчет количества работает за линию.

Nickolay Bukreyev26 August 2015, at 09:47Thank you for your article, Adilet! And thanks to Mikhail Rubinchik - palindromic tree is really cool and handy. By the way, it is funny that the only information I could find about it is in English.

Could you tell us please if it is possible to use palindromic tree to build Manacher's arrays?

Adilet28 August 2015, at 10:47Thanks, Nickolay. I am glad that you enjoyed an article and a data structure.

What do you mean by Manacher's arrays? Is it the same arrays that are produced during Manarcher's algorithm — it is, largest odd/even palindrome with a middle in this position?

I don't know the answer right away. I will think about your question and write later :)

BananaTree25 September 2015, at 16:21@Adilet, great article!

I translated your article from English to Chinese to make it more readable for programmers from China. Hope you don't mind, please send me a message in case you have problem with my such behavior, I will delete it.

url for translated blog: http://blog.csdn.net/lwfcgz/article/details/48739051

my email in case you need to send a message: wangliangpeking@gmail.com

Best wishes.

Adilet26 September 2015, at 12:29@BananaTree

I am very glad that you found it useful and worth translation. You are welcome to keep it, hope it will help many Chinese programmers!

Billy bob Joe fred14 November 2015, at 19:07Hi hahahahahahaha

Nikolay Zinov20 November 2015, at 06:20I guess "Hereafter we will call palindrome which is also a LARGEST proper suffix of a string just largest suffix-palindrome of a string." should be reformulated as "Hereafter we will call LARGEST palindrome which is also a proper suffix of a string just largest suffix-palindrome of a string." The current edition is confusing.

Adilet24 November 2015, at 05:17Thanks, Nikolay. Your version is indeed clearer (I noticed that I also used this form in the sentence before). Fixed now.

Foo27 April 2016, at 15:29Oh nice

:D28 November 2016, at 11:50You explained it really nice. thanks for the blog :)

Write your comment:

*Requested field

*Your comment cannot be empty