Suffix tree
From Wikipedia, the free encyclopedia
In computer science, a suffix tree (also called suffix trie, PAT tree or, in an earlier form, position tree) is a data structure that presents the suffixes of a given string in a way that allows for a particularly fast implementation of many important string operations.
The suffix tree for a string S is a tree whose edges are labeled with strings, and such that each suffix of S corresponds to exactly one path from the tree's root to a leaf. It is thus a radix tree (more specifically, a Patricia trie) for the suffixes of S.
Constructing such a tree for the string S takes time and space linear in the length of S. Once constructed, several operations can be performed quickly, for instance locating a substring in S, locating a substring if a certain number of mistakes are allowed, locating matches for a regular expression pattern etc. Suffix trees also provided one of the first linear-time solutions for the longest common substring problem. These speedups come at a cost: storing a string's suffix tree typically requires significantly more space than storing the string itself.
Contents |
[edit] History
The concept was first introduced as a position tree by Weiner in 1973[1] in a paper which Donald Knuth subsequently characterized as "Algorithm of the Year 1973". The construction was greatly simplified by McCreight in 1976 [2] , and also by Ukkonen in 1995[3][4]. Ukkonen provided the first linear-time online-construction of suffix trees, now known as Ukkonen's algorithm.
[edit] Definition
The suffix tree for the string S of length n is defined as a tree such that ([5] page 90):
- the paths from the root to the leaves have a one-to-one relationship with the suffixes of S,
- edges spell non-empty strings,
- and all internal nodes (except perhaps the root) have at least two children.
Since such a tree does not exist for all strings, S is padded with a terminal symbol not seen in the string (usually denoted $
). This ensures that no suffix is a prefix of another, and that there will be n leaf nodes, one for each of the n suffixes of S. Since all internal non-root nodes are branching, there can be at most n − 1 such nodes, and n + (n − 1) + 1 = 2n nodes in total.
Suffix links are a key feature for linear-time construction of the tree. In a complete suffix tree, all internal non-root nodes have a suffix link to another internal node. If the path from the root to a node spells the string χα, where χ is a single character and α is a string (possibly empty), it has a suffix link to the internal node representing α. See for example the suffix link from the node for ANA
to the node for NA
in the figure above. Suffix links are also used in some algorithms running on the tree.
[edit] Functionality
A suffix tree for a string S of length n can be built in Θ(n) time, if the alphabet is constant or integer [6]. Otherwise, the construction time depends on the implementation. The costs below are given under the assumption that the alphabet is constant. If it is not, the cost depends on the implementation (see below).
Assume that a suffix tree has been built for the string S of length n, or that a generalised suffix tree has been built for the set of strings D = {S1,S2,...,SK} of total length n = | n1 | + | n2 | + ... + | nK | . You can:
- Search for strings:
- Check if a string P of length m is a substring in O(m) time ([5] page 92).
- Find the first occurrence of the patterns P1,...,Pq of total length m as substrings in O(m) time.
- Find all z occurrences of the patterns P1,...,Pq of total length m as substrings in O(m + z) time ([5] page 123).
- Search for a regular expression P in time expected sublinear in n ([7]).
- Find for each suffix of a pattern P, the length of the longest match between a prefix of P[i...m] and a substring in D in Θ(m) time ([5] page 132). This is termed the matching statistics for P.
- Find properties of the strings:
- Find the longest common substrings of the string Si and Sj in Θ(ni + nj) time ([5] page 125).
- Find all maximal pairs, maximal repeats or supermaximal repeats in Θ(n + z) time ([5] page 144).
- Find the Lempel-Ziv decomposition in Θ(n) time ([5] page 166).
- Find the longest repeated substrings in Θ(n) time.
- Find the most frequently occurring substrings of a minimum length in Θ(n) time.
- Find the shortest strings from Σ that do not occur in D, in O(n + z) time, if there are z such strings.
- Find the shortest substrings occurring only once in Θ(n) time.
- Find, for each i, the shortest substrings of Si not occurring elsewhere in D in Θ(n) time.
The suffix tree can be prepared for constant time lowest common ancestor retrieval between nodes in Θ(n) time ([5] chapter 8). You can then also:
- Find the longest common prefix between the suffixes Si[p..ni] and Sj[q..nj] in Θ(1) ([5] page 196).
- Search for a pattern P of length m with at most k mismatches in O(kn + z) time, where z is the number of hits ([5] page 200).
- Find all z maximal palindromes in Θ(n)([5] page 198), or Θ(gn) time if gaps of length g are allowed, or Θ(kn) if k mismatches are allowed ([5] page 201).
- Find all z tandem repeats in O(nlogn + z), and k-mismatch tandem repeats in O(knlog(n / k) + z) ([5] page 204).
- Find the longest substrings common to at least k strings in D for k = 2..K in Θ(n) time ([5] page 205).
[edit] Applications
Suffix Trees can be used to solve a large number of string problems that occur in text-editing, free-text search, computational biology, and other application areas.[8] Primary applications of Suffix trees include,[8]
- String Search, in O(m) complexity, where m is the length of the sub-string. (But with initial O(n) time required to build the string)
- Finding the Longest Repeated Substring.
- Finding the Longest Common Substring.
- Finding the Longest palindrome in a string.
Suffix trees are often used in bioinformatics applications, where they are used for searching for patterns in DNA or protein sequences, which can be viewed as long strings of characters. The ability to search efficiently with mismatches might be the suffix tree's greatest strength. It is also used in data compression, where on the one hand it is used to find repeated data and on the other hand it can be used for the sorting stage of the Burrows-Wheeler transform. Variants of the LZW compression schemes use it (LZSS). A suffix tree is also used in suffix tree clustering, a data clustering algorithm used in some search engines (first introduced in [9]).
[edit] Implementation
If each node and edge can be represented in Θ(1) space, the entire tree can be represented in Θ(n) space. The total length of the edges in the tree is O(n2), but each edge can be stored as the position and length of a substring of S, giving a total space usage of Θ(n) computer words. The worst-case space usage of a suffix tree is seen with a fibonacci string, giving the full 2n nodes.
An important choice when making a suffix tree implementation is the parent-child relationships between nodes. The most common is using linked lists called sibling lists. Each node has pointer to its first child, and to the next node in the child list it is a part of. Hash maps, sorted/unsorted arrays (with array doubling), and balanced search trees may also be used, giving different running time properties. We are interested in:
- The cost of finding the child on a given character.
- The cost of inserting a child.
- The cost of enlisting all children of a node (divided by the number of children in the table below).
Let σ be the size of the alphabet. Then you have the following costs:
Lookup | Insertion | Traversal | |
---|---|---|---|
Sibling lists / unsorted arrays | O(n) | Θ(1) | Θ(1) |
Hash maps | Θ(1) | Θ(1) | O(n) |
Balanced search tree | O(logn) | O(logn) | O(1) |
Sorted arrays | O(logn) | O(n) | O(1) |
Hash maps + sibling lists | O(1) | O(1) | O(1) |
Note that the insertion cost is amortised, and that the costs for hashing are given perfect hashing.
The large amount of information in each edge and node makes the suffix tree very expensive, consuming about ten to twenty times the memory size of the source text in good implementations. The suffix array reduces this requirement to a factor of four, and researchers have continued to find smaller indexing structures.
[edit] See also
[edit] References
- ^ P. Weiner (1973). "Linear pattern matching algorithm". 14th Annual IEEE Symposium on Switching and Automata Theory: 1-11.
- ^ Edward M. McCreight (1976). "A Space-Economical Suffix Tree Construction Algorithm". Journal of the ACM 23 (2): 262--272. doi:. http://doi.acm.org/10.1145/321941.321946.
- ^ E. Ukkonen (1995). "On-line construction of suffix trees". Algorithmica 14 (3): 249--260. doi:. http://www.cs.helsinki.fi/u/ukkonen/SuffixT1withFigs.pdf.
- ^ R. Giegerich and S. Kurtz (1997). "From Ukkonen to McCreight and Weiner: A Unifying View of Linear-Time Suffix Tree Construction". Algorithmica 19 (3): 331--353. doi:. http://www.zbh.uni-hamburg.de/staff/kurtz/papers/GieKur1997.pdf.
- ^ a b c d e f g h i j k l m n Gusfield, Dan (1999) [1997]. Algorithms on Strings, Trees and Sequences: Computer Science and Computational Biology. USA: Cambridge University Press. ISBN 0-521-58519-8.
- ^ Martin Farach (1997). "Optimal suffix tree construction with large alphabets". Foundations of Computer Science, 38th Annual Symposium on: 137--143.
- ^ Ricardo A. Baeza-Yates and Gaston H. Gonnet (1996). "Fast text searching for regular expressions or automaton searching on tries". Journal of the ACM (ACM Press) 43 (6): 915--936. doi: .
- ^ a b Allison, L.. "Suffix Trees". http://www.allisons.org/ll/AlgDS/Tree/Suffix/. Retrieved on 2008-10-14.
- ^ Oren Zamir and Oren Etzioni (1998). "Web document clustering: a feasibility demonstration". SIGIR '98: Proceedings of the 21st annual international ACM SIGIR conference on Research and development in information retrieval: 46--54, ACM.
[edit] External links
- Suffix Trees by Dr. Sartaj Sahni (CISE Department Chair at University of Florida)
- Suffix Trees by Lloyd Allison
- Suffix Trees links collection by Mark Nelson
- NIST's Dictionary of Algorithms and Data Structures: Suffix Tree
- libstree, a generic suffix tree library written in C
- Tree::Suffix, a Perl binding to libstree
- Strmat a faster generic suffix tree library written in C (uses arrays instead of linked lists)
- SuffixTree a Python binding to Strmat
- Universal Data Compression Based on the Burrows-Wheeler Transformation: Theory and Practice, application of suffix trees in the BWT
- Theory and Practice of Succinct Data Structures, C++ implementation of a compressed suffix tree]
- Practical Algorithm Template Library, a C++ library with suffix tree implementation on PATRICIA trie, by Roman S. Klyujkov
- Google video on constructing a suffix tree
- Visualizer constructs a suffix tree by algorithm McCreight