Suffix Tree (from Geeksforgeeks)

Source: Internet
Author: User

Given a text txt[0..n-1] and a pattern pat[0..m-1], write a function search (char pat[], Char txt[]) that prints all Occurr Ences of pat[] in txt[]. Assume that n > M.

Preprocess Pattern or preoprocess Text?
We have discussed the following algorithms in the previous posts:

KMP algorithm
Rabin Karp algorithm
Finite automata based algorithm
Boyer Moore algorithm

All of the above algorithms preprocess the pattern to make the pattern searching faster. The best time complexity this we could get by preprocessing pattern are O (n) where n is the length of the text. In this post, we'll discuss an approach that preprocesses the text. A suffix tree is built of the text. After preprocessing text (building suffix tree of text), we can search all pattern in O (m) time where m is length of the P Attern.
Imagine You has stored complete work Of william Shakespeare and preprocessed it. You can search any string in the just proportional to length of the pattern. This is really a great improvement because length of the pattern is generally much smaller than text.
preprocessing of text may become costly if the text changes frequently. It is good for the fixed text or less frequently changing text though.

a Suffix Tree for a given text was a compressed triefor all suffixes of the given text. We have the discussed standard Trie. Let us understand compressed Trie with the following array of words.

{Bear, Bell, bid, Bull, buy, sell, stock, stop}

Following is standard trie for the above input set of words.

Following is the compressed trie. Compress Trie is obtained from standard Trie by joining chains of single nodes. The nodes of a compressed trie can be stored by storing index ranges at the nodes.

How to build a Suffix Tree for a given text?
As discussed above, Suffix Tree is compressed trie of all suffixes, so following be very abstract steps to build a Suffix Tree from given text.
1) Generate all suffixes of given text.
2) Consider all suffixes as individual words and build a compressed trie.

Let us consider an example text "Banana\0″where ' \0′is string termination character. Following is all suffixes of "Banana\0″

Banana\0anana\0nana\0ana\0na\0a\0\0

If We consider all of the above suffixes as individual words and build a trie, we get following.

If We join chains of single nodes, we get the following compressed trie, which are the Suffix Tree for given text "Banana\0 ″

Please note that above steps is just to manually create a Suffix Tree. We'll be discussing actual algorithm and implementation in a separate post.

How to search a pattern in the built suffix tree?
We have the discussed above how to build a Suffix Tree which are needed as a preprocessing step in pattern searching. Following is abstract steps to search a pattern in the built Suffix Tree.
1) starting from the first character of the pattern and root of Suffix Tree, does following for every character.
..... a) for the current character of pattern, if there are an edge from the current node of suffix tree, follow the EDG E.
..... b) If There is no edge, print "pattern doesn ' t exist in text" and return.
2) If All characters of pattern has been processed, i.e., there is a path from root for characters of the given Pattern, then print "Pattern found".

Let us consider the example pattern as "Nan" to see the searching process. Following diagram shows the path followed for searching "Nan" or "Nana".

How does this work?
Every pattern that's present in text (or we can say every substring of text) must being a prefix of one of all possible suff Ixes. The statement seems complicated, but it's a simple statement, we just need to take an example to check validity of it.

Applications of Suffix Tree
Suffix tree can used for a wide range of problems. Following is some famous problems where Suffix Trees provide optimal time complexity solution.
1) Pattern searching
2) Finding the longest repeated substring
3) Finding the longest common substring
4) Finding the longest palindrome in a string

There is many more applications. See the For more details.

Ukkonen ' s Suffix Tree construction is discussed in following articles:
Ukkonen ' s Suffix Tree Construction–part 1
Ukkonen ' s Suffix Tree Construction–part 2
Ukkonen ' s Suffix Tree Construction–part 3
Ukkonen ' s Suffix Tree Construction–part 4
Ukkonen ' s Suffix Tree Construction–part 5
Ukkonen ' s Suffix Tree Construction–part 6

References:
Http://fbim.fh-regensburg.de/~saj39122/sal/skript/progr/pr45102/Tries.pdf
Http://www.cs.ucf.edu/~shzhang/Combio12/lec3.pdf
http://www.allisons.org/ll/AlgDS/Tree/Suffix/

Suffix Tree (from Geeksforgeeks)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.