Yuto Nakashima Shunsuke Inenaga Hideo Bannai Masayuki Takeda Kyushu University Computing Maximal Palindromes and Distinct Palindromes in a Trie Palindromes A palindrome is a string that reads the same forward and backward ID: 814568
Download The PPT/PDF document "PSC2019 Mitsuru Funakoshi" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
Slide1
PSC2019
Mitsuru Funakoshi, Yuto Nakashima, Shunsuke Inenaga, Hideo Bannai, Masayuki TakedaKyushu University
Computing
Maximal Palindromes and
Distinct Palindromes in a Trie
Slide2Palindromes
A palindrome is a string that reads the same forward and backward.Examples○ radar
○
neveroddoreven (Never odd or even.)○リモコンてんこもり (りもこんてんこもり) = There are a lot of remote controllers.○
Palindromes
A palindrome is a string that reads the same forward and backward.Examples○ radar
○
neveroddoreven(Never odd or even.)○ In this talk, we consider maximal / distinct palindromes in a trie (rooted edge-labeled tree).I will begin with maximal / distinct palindromes in a string.
Let
be a string of length .For each integer / half-integer position
in
,
the maximal substring of that is a palindrome with center is the maximal palindrome centered at . E.g.)
cbabcaaaccacbabcab
The number of maximal palindromes in any string
of length
is
exactly
(including empty strings)and all of them can be computed in time [Manacher, 1975].
7
9.5
Maximal palindromes in a string
Slide5For a string , let
be the set of all distinct palindromes in .E.g.)
cbabcaaaccacbabcab
{ε,a,b,c,aa,cc,aaa,bab,cac,acca, cbabc,caaac,acbabca}
Distinct palindromes in a string
Slide6For a string , let
be the set of all distinct palindromes in .E.g.)
cbabcaaaccacbabcab
{ε,a,b,c,aa,cc,aaa,bab,cac,acca, cbabc,caaac,acbabca}
Distinct palindromes in a string
Slide7For a string , let
be the set of all distinct palindromes in .E.g.)
cbabcaaaccacbabcab
{ε,a,b,c,aa,cc,aaa,bab,cac,acca, cbabc,caaac,acbabca}
The maximum number of distinct palindromes in any string
of length
is at most
[Droubay et al., 2001].
All of them can be computed in time [Groult et al., 2010].
Distinct palindromes in a string
Slide8a
a
b
c
c
a
d
a
b
c
c
c
b
a
b
b
c
a
b
b
a
a
c
root
A trie is a rooted tree where each edge is labeled by a single char and
out-going edges of each node are labeled by mutually distinct chars.
A trie is a natural extension to a string,
and is a compact representation of a set of strings.
Trie
Slide9a
a
b
c
c
a
d
a
b
c
c
c
b
a
b
b
c
a
b
b
a
a
c
root
Input: a
trie
(rooted edge-labeled tree) with
edges
Output:
・
all
maximal palindromes
in
・
all
distinct palindromes
in
Problem
Slide10Pals in a trie (rooted edge-labeled tree)
only consider paths in the root-to-leaf direction
a
a
b
c
c
a
d
a
bcc
c
b
a
b
b
c
a
b
b
a
a
c
root
Slide11Pals in a trie (rooted edge-labeled tree)
only consider paths in the root-to-leaf direction
a
a
b
c
c
a
d
a
bc
c
c
b
a
b
b
c
a
b
b
a
a
c
root
Slide12a
a
b
c
c
a
d
a
b
c
c
c
b
a
b
b
c
a
b
b
a
a
c
root
Pals in a trie (rooted edge-labeled tree)
only consider paths in the root-to-leaf direction
Slide13The maximal palindrome centered at is
bccaaccb.
Maximal palindromes in a trie
a
a
b
c
c
a
d
a
b
c
c
c
b
a
b
b
c
a
b
b
a
a
c
root
Slide14The maximal palindrome centered at is aaa
.Maximal palindromes in a trie
a
a
b
c
c
a
d
a
b
c
c
c
b
a
b
b
c
a
b
b
a
a
c
root
Slide15Distinct palindromes in a trie
a
a
b
c
c
a
d
a
b
cc
c
b
a
b
b
c
a
b
b
a
a
c
root
For a trie
, let
be the set of all distinct palindromes in
.
{
ε,a,b,c,d,aa,bb,cc,aaa,cdc,acca
,
bccb,caac,cbbc,abccba,ccaacc,bccaaccb
}
Distinct palindromes in a trie
a
a
b
c
c
a
d
a
b
cc
c
b
a
b
b
c
a
b
b
a
a
c
root
For a trie
, let
be the set of all distinct palindromes in
.
{
ε,a,b,c,d,aa,bb,
cc
,aaa,cdc,acca
,
bccb,caac,cbbc,abccba,ccaacc,bccaaccb
}
Distinct palindromes in a trie
a
a
b
c
c
a
d
a
b
cc
c
b
a
b
b
c
a
b
b
a
a
c
root
For a trie
, let
be the set of all distinct palindromes in
.
{
ε,a,b,c,d,aa,bb,
cc
,aaa,cdc,acca
,
bccb,caac,cbbc,abccba,ccaacc,bccaaccb
}
Naïve methods for these problems
All maximal (distinct) pals can be computed by naïvely applying Manacher’s (Groult et al.’s) algorithm to each root-to-leaf path.This takes
time for each root-to-leaf path,
where
is the height of the trie.
a
a
b
c
c
a
d
a
b
c
c
c
b
a
b
b
c
a
b
b
a
a
c
root
Slide19Naïve methods for these problems
Therefore this naïve method computes all maximal / distinct palindromes in a trie in time,
where
is the number of leaves.
This requires time in the worst case.
a
a
b
c
c
a
d
a
b
c
c
c
b
a
b
b
c
a
b
b
a
a
c
root
Slide20Main results for maximal palindromes
# outputs
Time
Working space
In a trie
Algorithm 1
Algorithm 2
In a string
Manacher
# outputs
Time
Working space
In a trie
Algorithm 1
Algorithm 2
In a string
Manacher
: the length of
the
string
: the number of edges of the trie
: the number of leaves of the trie
: the height of the trie
: the number of the branching nodes of the trie
Main results for maximal palindromes
# outputs
Time
Working space
In a trie
Algorithm 1
Algorithm 2
In a string
Manacher
# outputs
Time
Working space
In a trie
Algorithm 1
Algorithm 2
In a string
Manacher
: the length of
the
string
: the number of edges
of the trie
: the number of leaves of the trie
: the height of the trie
: the number of the branching nodes of the trie
These time complexity are
in the worst case.
# outputs
Time
Working space
In a trie
In a string
Groult et al.
# outputs
Time
Working space
In a trie
In a string
Groult et al.
Main results for distinct palindromes
: the length of a string
: the number of edges
of the trie
: the height of the trie
In a trie
# outputs
Time
Working space
Maximal pals
Algorithm 1
Algorithm 2
Distinct
pals
In a trie
# outputs
Time
Working space
Maximal
pals
Algorithm 1
Algorithm 2
Distinct
pals
Main results
: the number of edges
of the trie
: the number of leaves of the trie
: the height of the trie
: the number of the branching nodes of the trie
In a trie
# outputs
Time
Working space
Maximal pals
Algorithm 1
Algorithm 2
Distinct
pals
In a trie
# outputs
Time
Working space
Maximal
pals
Algorithm 1
Algorithm 2
Distinct
pals
Main results
: the number of edges
of the trie
: the number of leaves of the trie
: the height of the trie
: the number of the branching nodes of the trie
The number of maximal palindromes
a
a
b
c
c
a
d
a
b
cc
c
b
a
b
b
c
a
b
b
a
a
c
root
The number of maximal palindromes
a
a
b
c
c
a
d
a
b
c
c
c
b
a
b
b
c
a
b
b
a
a
c
root
Slide27The number of maximal palindromes
a
a
b
c
c
a
d
a
b
c
c
c
b
a
b
b
c
a
b
b
a
a
c
root
Slide28The number of maximal palindromes
a
a
b
c
c
a
d
a
b
c
c
c
b
a
b
b
c
a
b
b
a
a
c
root
Slide29The number of maximal palindromes
There is exactly one maximal palindrome for each center pos
,
since the reversed path from to the root is unique and the out-going edges of each node are labeled by mutually distinct characters.Because there are
center positions in a trie,
the number of maximal palindromes in a trie is also
.
a
a
b
c
c
a
d
a
b
c
c
c
b
a
b
b
c
a
b
b
a
a
c
root
Slide30In a trie
# outputs
Time
Working space
Maximal pals
Algorithm 1
Algorithm 2
Distinct
pals
In a trie
# outputs
Time
Working space
Maximal
pals
Algorithm 1
Algorithm 2
Distinct
pals
Computing maximal palindromes
: the number of edges
of the trie
: the number of leaves of the trie
: the height of the trie
: the number of the branching nodes of the trie
For each edge
in a depth-first traversal on the trie: Check whether all suffix palindromes of the root-to-
path
can be extended with edge
or not.If all out-edges from have been processed,output all palindromes ending at
that did not extend
with any path, as maximal palindromes.
If
is a leaf,
output all palindromes ending at
, as maximal palindromes. Overview
Slide32For each edge
in a depth-first traversal on the trie: Check whether all suffix palindromes of the root-to-
path
can be extended with edge
or not. Step 1
a
a
b
c
c
a
d
a
b
c
c
c
b
a
b
b
c
a
b
b
a
a
c
root
For each edge
in a depth-first traversal on the trie: Check whether all suffix palindromes of the root-to-
path
can be extended with edge
or not. Step 1
a
a
b
c
c
a
d
a
b
c
c
c
b
a
b
b
c
a
b
b
a
a
c
root
For each edge
in a depth-first traversal on the trie: Check whether all suffix palindromes of the root-to-
path
can be extended with edge
or not. Step 1
a
a
b
c
c
a
d
a
b
c
c
c
b
a
b
b
c
a
b
b
a
a
c
root
For each edge
in a depth-first traversal on the trie: Check whether all suffix palindromes of the root-to-
path
can be extended with edge
or not. Step 1
a
a
b
c
c
a
d
a
b
c
c
c
b
a
b
b
c
a
b
b
a
a
c
root
If all out-edges from
have been processed,output all palindromes ending at that did not extend
with any path, as maximal palindromes.
Step 2
Maximal palindrome
a
a
b
c
c
a
d
a
b
c
c
c
b
a
b
b
c
a
b
b
a
a
c
root
There are
suffix palindromes ending at each node. But we can process them in a total of
time and
working space for all nodes,
by using periodic structures of suffix palindromes. Suffix palindromes ending at each node
a
a
b
c
c
a
d
a
b
c
c
c
b
a
b
b
c
a
b
b
a
a
c
root
Slide38Position
Periodic property of suffix pals in a str.
Lemma 1 [Apostolico et al., 1995]
Suffix palindromes in a string which end at
pos
can be classified into
groups
such that each pal in the same group has the
same smallest period.
All groups can be represented in
space.
The maximal palindromes in a group except for the longest one are immediately preceded by the same character,
since these palindromes have same smallest period.
root
Immediately preceding chars for each group
Slide40The maximal palindromes in a group except for the longest one are immediately preceded by the same character.
Therefore, the number of distinct immediately preceding characters for each group is at most 2.
b
b
root
b
b
Immediately preceding chars for each group
Slide41b
bb
b
Let
be the immediately preceding character of the longest one.Let be the immediately preceding character of the others.Let be the label of edge (
.
Extension of each group
root
c
b
bb
b
There are 4 cases to consider for each group:
(1) (2) ,
(3)
,
(4)
,
Extension of each group
root
c
(1)
All these palindromes are extended.
Extension of each group
root
c
b
b
b
b
Slide44b
bb
b
(2)
, The longest one is extended. (Other palindromes might be extended with another out-edge of
.)
Extension of each group
root
c
b
b
b
b
(3)
,
All these palindromes but the longest one are extended.
(The longest one might be extended with another out-edge of
.)
Extension of each group
root
c
b
bb
b
(4)
, All these palindromes are not extended with this edge
. (These palindromes might be extended with another out-edge of
.)
Extension of each group
root
c
We represent each group by an arithmetic progression,
so each group can be processed in time in the above all cases. There are
arithmetic progression at each node,
therefore we can check whether suffix palindromes
can be extended or not in time per each node.
Since there are
nodes,
we can compute all maximal palindromes in
time.
Running time
Slide48Since suffix palindromes that are not extended with edge
might be extended with another path, we have to store them during traversal from
to all children of
.
These palindromes are maximal in the root-to- path, so the working space is during traversal. Working space
a
a
b
c
c
a
d
a
b
c
c
c
b
a
b
b
c
a
b
b
a
a
c
root
Slide49Summary
In a trie
# outputs
Time
Working space
Maximal pals
Algorithm 1
Algorithm 2
Distinct
pals
In a trie
# outputs
Time
Working space
Maximal
pals
Algorithm 1
Algorithm 2
Distinct
pals
Slide50Open problems
Can we compute maximal / distinct pals in a trie in time?
Can we efficiently compute maximal / distinct pals in an unrooted tree?
The maximum number of distinct pals in an unrooted tree
is [Brlek et al., 2015]+[Gawrychowski et al., 2015].Can we efficiently compute runs / squares in a trie / an unrooted tree?The maximum number of distinct squares in an unrooted tree
is
[Crochemore et al., 2012] and all of them can be
counted in
time [
Kociumaka
et al., 2014]. The maximum number of runs in a trie is less than and
all of them can be computed in
time
[
Sugahara
et al., 2019].
Can we generalize
Manachr’s algorithm? a
b
a
aaccb
c
b
b
c
a
root
Slide52Can we generalize
Manachr’s algorithm? ab
c
a
accb
c
b
b
c
a
root
Slide53Previous work of palindromes in a tree
Theorem [Brlek et al., 2015] The maximum number of distinct palindromes in an unrooted tree is
.
Theorem [Gawrychowski et al., 2015] The maximum number of distinct palindromes in an unrooted tree is .
To our knowledge, palindromes in a
trie
(rooted edge-labeled tree)
have not been studied previously
.There is previous work on the number of distinct palindromes in an unrooted tree with edges.
Slide54Periodic property of maximal palindromes
Each group can be represented by
an arithmetic progression.
Each arithmetic progression is
a tuple
representing the sequence
with common difference
.
Position
a
bb
a
a
cabba
a
b
a
b
a
c
a
b
a
a
a
b
c
Strategy of Algorithm 2
Slide56a
bb
a
a
cabba
a
b
a
b
a
c
a
b
a
a
a
b
c
Strategy of Algorithm 2
Slide57a
bb
a
a
cabb
a
a
b
a
b
a
c
a
b
a
a
a
b
c
Strategy of Algorithm 2
Slide58a
b
b
a
a
c
a
b
b
a
a
b
a
b
a
c
a
b
a
a
a
b
c
Strategy of Algorithm 2
Slide59a
b
b
a
acab
b
a
a
b
a
b
a
c
a
b
a
a
a
b
c
Strategy of Algorithm 2
Slide60a
b
b
a
acabb
a
a
b
a
b
a
c
a
b
a
a
a
b
c
Strategy of Algorithm 2
Slide61a
b
b
a
acabb
a
a
b
a
b
a
c
a
b
a
a
a
b
c
Strategy of Algorithm 2
Slide62a
bb
a
a
cabba
a
b
a
b
a
c
a
b
a
a
a
b
c
Strategy of Algorithm 2
Slide63All maximal palindromes can be computed by the above operation.
a
b
b
aacab
b
a
a
b
a
b
a
c
a
b
a
a
a
b
c
Strategy of Algorithm 2