![]() |
VOOZH | about |
In earlier suffix tree articles, we created suffix tree for one string and then we queried that tree for substring check, searching all patterns, longest repeated substring and built suffix array (All linear time operations).
There are lots of other problems where multiple strings are involved.
e.g. pattern searching in a text file or dictionary, spell checker, phone book, Autocomplete, Longest common substring problem, Longest palindromic substring and More.
For such operations, all the involved strings need to be indexed for faster search and retrieval. One way to do this is using suffix trie or suffix tree. We will discuss suffix tree here.
A suffix tree made of a set of strings is known as Generalized Suffix Tree.
We will discuss a simple way to build Generalized Suffix Tree here for two strings only.
Later, we will discuss another approach to build Generalized Suffix Tree for two or more strings.
Here we will use the suffix tree implementation for one string discussed already and modify that a bit to build generalized suffix tree.
Lets consider two strings X and Y for which we want to build generalized suffix tree. For this we will make a new string X#Y$ where # and $ both are terminal symbols (must be unique). Then we will build suffix tree for X#Y$ which will be the generalized suffix tree for X and Y. Same logic will apply for more than two strings (i.e. concatenate all strings using unique terminal symbols and then build suffix tree for concatenated string).
Lets say X = xabxa, and Y = babxba, then
X#Y$ = xabxa#babxba$
If we run the code implemented at Ukkonenβs Suffix Tree Construction β Part 6 for string xabxa#babxba$, we get following output:
Output:
Pictorial View:
We can use this tree to solve some of the problems, but we can refine it a bit by removing unwanted substrings on a path label. A path label should have substring from only one input string, so if there are path labels having substrings from multiple input strings, we can keep only the initial portion corresponding to one string and remove all the later portion. For example, for path labels #babxba$, a#babxba$ and bxa#babxba$, we can remove babxba$ (belongs to 2nd input string) and then new path labels will be #, a# and bxa# repectively. With this change, above diagram will look like below:
Below implementation is built on top of original implementation. Here we are removing unwanted characters on path labels. If a path label has "#" character in it, then we are trimming all characters after the "#" in that path label.
Note: This implementation builds generalized suffix tree for only two strings X and Y which are concatenated as X#Y$
# [5] $ [12] a [-1] # [4] $ [11] bx [-1] a# [1] ba$ [7] b [-1] a [-1] $ [10] bxba$ [6] x [-1] a# [2] ba$ [8] x [-1] a [-1] # [3] bxa# [0] ba$ [9]
If two strings are of size M and N, this implementation will take O(M+N) time and space.
If input strings are not concatenated already, then it will take 2(M+N) space in total, M+N space to store the generalized suffix tree and another M+N space to store concatenated string.
Followup:
Extend above implementation for more than two strings (i.e. concatenate all strings using unique terminal symbols and then build suffix tree for concatenated string)
One problem with this approach is the need of unique terminal symbol for each input string. This will work for few strings but if there is too many input strings, we may not be able to find that many unique terminal symbols.
We will discuss another approach to build generalized suffix tree soon where we will need only one unique terminal symbol and that will resolve the above problem and can be used to build generalized suffix tree for any number of input strings.
We have published following more articles on suffix tree applications: