Suffix tree

Short description: Tree containing all suffixes of a given text

Suffix tree for the text `BANANA`. Each substring is terminated with special character `$`. The six paths from the root to the leaves (shown as boxes) correspond to the six suffixes `A$`, `NA$`, `ANA$`, `NANA$`, `ANANA$` and `BANANA$`. The numbers in the leaves give the start position of the corresponding suffix. Suffix links, drawn dashed, are used during construction.

In computer science, a suffix tree (also called PAT tree or, in an earlier form, position tree) is a compressed trie containing all the suffixes of the given text as their keys and positions in the text as their values. Suffix trees allow particularly fast implementations of many important string operations.

The construction of 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, such as locating a substring in S, locating a substring if a certain number of mistakes are allowed, and locating matches for a regular expression pattern. Suffix trees also provided one of the first linear-time solutions for the longest common substring problem.[2] These speedups come at a cost: storing a string's suffix tree typically requires significantly more space than storing the string itself.

## Contents

* 1 History
* 2 Definition
* 3 Functionality
* 4 Applications
* 5 Implementation
* 6 Parallel construction
* 7 External construction
* 8 See also
* 9 Notes
* 10 References
* 11 External links

## History

The concept was first introduced by (Weiner 1973). Rather than the suffix S[i..n], Weiner stored in his trie[3] the prefix identifier for each position, that is, the shortest string starting at i and occurring only once in S. His Algorithm D takes an uncompressed[4] trie for S[k+1..n] and extends it into a trie for S[k..n]. This way, starting from the trivial trie for S[n..n], a trie for S[1..n] can be built by n−1 successive calls to Algorithm D; however, the overall run time is O(n2). Weiner's Algorithm B maintains several auxiliary data structures, to achieve an overall run time linear in the size of the constructed trie. The latter can still be O(n2) nodes, e.g. for S=anbnanbn$. Weiner's Algorithm C finally uses compressed tries to achieve linear overall storage size and run time.[5] Donald Knuth subsequently characterized the latter as "Algorithm of the Year 1973" according to his student Vaughan Pratt.[original research?][6] The text book (Aho Hopcroft) reproduced Weiner's results in a simplified and more elegant form, introducing the term position tree.

(McCreight 1976) was the first to build a (compressed) trie of all suffixes of S. Although the suffix starting at i is usually longer than the prefix identifier, their path representations in a compressed trie do not differ in size. On the other hand, McCreight could dispense with most of Weiner's auxiliary data structures; only suffix links remained.

(Ukkonen 1995) further simplified the construction.[6] He provided the first online-construction of suffix trees, now known as Ukkonen's algorithm, with running time that matched the then fastest algorithms. These algorithms are all linear-time for a constant-size alphabet, and have worst-case running time of O(nlog⁡n) in general.

(Farach 1997) gave the first suffix tree construction algorithm that is optimal for all alphabets. In particular, this is the first linear-time algorithm for strings drawn from an alphabet of integers in a polynomial range. Farach's algorithm has become the basis for new algorithms for constructing both suffix trees and suffix arrays, for example, in external memory, compressed, succinct, etc.

## Definition

The suffix tree for the string S of length n is defined as a tree such that:[7]

1. The tree has exactly n leaves numbered from 1 to n.
2. Except for the root, every internal node has at least two children.
3. Each edge is labelled with a non-empty substring of S.
4. No two edges starting out of a node can have string-labels beginning with the same character.
5. The string obtained by concatenating all the string-labels found on the path from the root to leaf i spells out suffix S[i..n], for i from 1 to n.

If a suffix of S is also the prefix of another suffix, such a tree does not exist for the string. For example, in the string abcbc, the suffix bc is also a prefix of the suffix bcbc. In such a case, the path spelling out bc will not end in a leaf, violating the fifth rule. To fix this problem, 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.[8] 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 (n leaves, n−1 internal non-root nodes, 1 root).

Suffix links are a key feature for older linear-time construction algorithms, although most newer algorithms, which are based on Farach's algorithm, dispense with suffix links. 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.

A generalized suffix tree is a suffix tree made for a set of strings instead of a single string. It represents all suffixes from this set of strings. Each string must be terminated by a different termination symbol.

## Functionality

A suffix tree for a string S of length n can be built in Θ(n) time, if the letters come from an alphabet of integers in a polynomial range (in particular, this is true for constant-sized alphabets).[9] For larger alphabets, the running time is dominated by first sorting the letters to bring them into a range of size O(n); in general, this takes O(nlog⁡n) time. The costs below are given under the assumption that the alphabet is constant.

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.[10]
* 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.[11]
* Search for a regular expression P in time expected sublinear in n.[12]
* 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.[13] 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.[14]
* Find all maximal pairs, maximal repeats or supermaximal repeats in Θ(n+z) time.[15]
* Find the Lempel–Ziv decomposition in Θ(n) time.[16]
* Find the longest repeated substrings in Θ(n) time.[17]
* Find the most frequently occurring substrings of a minimum length in Θ(n) time.[citation needed]
* Find the shortest strings from Σ that do not occur in D, in O(n+z) time, if there are z such strings.[citation needed]
* Find the shortest substrings occurring only once in Θ(n) time.[citation needed]
* Find, for each i, the shortest substrings of Si not occurring elsewhere in D in Θ(n) time.[citation needed]

The suffix tree can be prepared for constant time lowest common ancestor retrieval between nodes in Θ(n) time.[18] One can then also:

* Find the longest common prefix between the suffixes Si[p..ni] and Sj[q..nj] in Θ(1).[19]
* 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.[20]
* Find all z maximal palindromes in Θ(n),[21] or Θ(gn) time if gaps of length g are allowed, or Θ(kn) if k mismatches are allowed.[22]
* Find all z tandem repeats in O(nlog⁡n+z), and k-mismatch tandem repeats in O(knlog⁡(n/k)+z).[23]
* Find the longest common substrings to at least k strings in D for k=2,…,K in Θ(n) time.[24]
* Find the longest palindromic substring of a given string (using the generalized suffix tree of the string and its reverse) in linear time.[25]

## 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.[26] Primary applications include:[26]

* 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 suffix tree for 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, 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 considered their greatest strength. Suffix trees are also used in data compression; they can be used to find repeated data, and can be used for the sorting stage of the Burrows–Wheeler transform. Variants of the LZW compression schemes use suffix trees (LZSS). A suffix tree is also used in suffix tree clustering, a data clustering algorithm used in some search engines.[27]

## 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 all the strings on all 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 word, 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 a pointer to its first child, and to the next node in the child list it is a part of. Other implementations with efficient running time properties use hash maps, sorted or unsorted arrays (with array doubling), or balanced search trees. 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: {| class="wikitable" ! !Lookup !Insertion !Traversal |- | style="text-align: right;" |Sibling lists / unsorted arrays |O(σ) |Θ(1) |Θ(1) |- | style="text-align: right;" |Bitwise sibling trees |O(log σ) |Θ(1) |Θ(1) |- | style="text-align: right;" |Hash maps |Θ(1) |Θ(1) |O(σ) |- | style="text-align: right;" |Balanced search tree |O(log σ) |O(log σ) |O(1) |- | style="text-align: right;" |Sorted arrays |O(log σ) |O(σ) |O(1) |- | style="text-align: right;" |Hash maps + sibling lists |O(1) |O(1) |O(1) |}

The insertion cost is amortised, and that the costs for hashing are given for perfect hashing.

The large amount of information in each edge and node makes the suffix tree very expensive, consuming about 10 to 20 times the memory size of the source text in good implementations. The suffix array reduces this requirement to a factor of 8 (for array including LCP values built within 32-bit address space and 8-bit characters.) This factor depends on the properties and may reach 2 with usage of 4-byte wide characters (needed to contain any symbol in some UNIX-like systems, see wchar t) on 32-bit systems. Researchers have continued to find smaller indexing structures.

## Parallel construction

Various parallel algorithms to speed up suffix tree construction have been proposed.[28][29][30][31][32] Recently, a practical parallel algorithm for suffix tree construction with O(n) work (sequential time) and O(log2n) span has been developed. The algorithm achieves good parallel scalability on shared-memory multicore machines and can index the human genome – approximately 3GB – in under 3 minutes using a 40-core machine.[33]

## External construction

Though linear, the memory usage of a suffix tree is significantly higher than the actual size of the sequence collection. For a large text, construction may require external memory approaches.

There are theoretical results for constructing suffix trees in external memory. The algorithm by (Farach-Colton Ferragina) is theoretically optimal, with an I/O complexity equal to that of sorting. However the overall intricacy of this algorithm has prevented, so far, its practical implementation.[34]

On the other hand, there have been practical works for constructing disk-based suffix trees which scale to (few) GB/hours. The state of the art methods are TDD,[35] TRELLIS,[36] DiGeST,[37] and B2ST.[38]

TDD and TRELLIS scale up to the entire human genome resulting in a disk-based suffix tree of a size in the tens of gigabytes.[35][36] However, these methods cannot handle efficiently collections of sequences exceeding 3 GB.[37] DiGeST performs significantly better and is able to handle collections of sequences in the order of 6 GB in about 6 hours.[37]

All these methods can efficiently build suffix trees for the case when the tree does not fit in main memory, but the input does. The most recent method, B2ST,[38] scales to handle inputs that do not fit in main memory. ERA is a recent parallel suffix tree construction method that is significantly faster. ERA can index the entire human genome in 19 minutes on an 8-core desktop computer with 16 GB RAM. On a simple Linux cluster with 16 nodes (4 GB RAM per node), ERA can index the entire human genome in less than 9 minutes.[39]

## See also

* Suffix automaton

## Notes

1. ↑ Donald E. Knuth; James H. Morris; Vaughan R. Pratt (Jun 1977). "Fast Pattern Matching in Strings". SIAM Journal on Computing 6 (2): 323-350. doi:10.1137/0206024. https://www.cs.jhu.edu/~misha/ReadingSeminar/Papers/Knuth77.pdf. Here: p.339 bottom.
2. ↑ Knuth conjectured in 1970 that the problem could not be solved in linear time.[1] In 1973, this was refuted by Weiner's suffix-tree algorithm (Weiner 1973).
3. ↑ This term is used here to distinguish Weiner's precursor data structures from proper suffix trees as defined above and unconsidered before (McCreight 1976).
4. ↑ i.e., with each branch labelled by a single character
5. ↑ See :File:WeinerB aaaabbbbaaaabbbb.gif and :File:WeinerC aaaabbbbaaaabbbb.gif for an uncompressed example tree and its compressed correspondant.
6. ↑ 6.0 6.1 Giegerich & Kurtz (1997).
7. ↑ (Gusfield 1999), p.90.
8. ↑ (Gusfield 1999), p.90-91.
9. ↑ Farach (1997).
10. ↑ (Gusfield 1999), p.92.
11. ↑ (Gusfield 1999), p.123.
12. ↑ Baeza-Yates & Gonnet (1996). harv error: no target: CITEREFBaeza-YatesGonnet1996 (help)
13. ↑ (Gusfield 1999), p.132.
14. ↑ (Gusfield 1999), p.125.
15. ↑ (Gusfield 1999), p.144.
16. ↑ (Gusfield 1999), p.166.
17. ↑ Such substrings corresponds to internal nodes of maximal depth within the suffix tree, hence can be found in linear time using depth-first search.
18. ↑ (Gusfield 1999), Chapter 8.
19. ↑ (Gusfield 1999), p.196.
20. ↑ (Gusfield 1999), p.200.
21. ↑ (Gusfield 1999), p.198.
22. ↑ (Gusfield 1999), p.201.
23. ↑ (Gusfield 1999), p.204.
24. ↑ (Gusfield 1999), p.205.
25. ↑ (Gusfield 1999), pp.197–199.
26. ↑ 26.0 26.1 Allison, L.. "Suffix Trees". http://www.allisons.org/ll/AlgDS/Tree/Suffix/.
27. ↑ First introduced by (Zamir Etzioni).
28. ↑ Apostolico et al. (1988).
29. ↑ Hariharan (1994).
30. ↑ Sahinalp & Vishkin (1994).
31. ↑ Farach & Muthukrishnan (1996). harv error: no target: CITEREFFarachMuthukrishnan1996 (help)
32. ↑ Iliopoulos & Rytter (2004).
33. ↑ Shun & Blelloch (2014).
34. ↑ Smyth (2003).
35. ↑ 35.0 35.1 (Tata Hankins).
36. ↑ 36.0 36.1 (Phoophakdee Zaki).
37. ↑ 37.0 37.1 37.2 (Barsky Stege).
38. ↑ 38.0 38.1 (Barsky Stege).
39. ↑ Mansour et al. (2011).

## References

* The Design and Analysis of Computer Algorithms, Reading/MA: Addison-Wesley, 1974, ISBN 0-201-00029-6, Bibcode: 1974daca.book.....A .
* Apostolico, A.; Iliopoulos, C.; Landau, G. M.; Schieber, B.; Vishkin, U. (1988), "Parallel construction of a suffix tree with applications", Algorithmica 3 (1–4): 347–365, doi:10.1007/bf01762122, http://docs.lib.purdue.edu/cgi/viewcontent.cgi?article=1617&context=cstech .
* "Fast text searching for regular expressions or automaton searching on tries", Journal of the ACM 43 (6): 915–936, 1996, doi:10.1145/235809.235810 .
* Barsky, Marina; Stege, Ulrike; Thomo, Alex; Upton, Chris (2008), "A new method for indexing genomes using on-disk suffix trees", CIKM '08: Proceedings of the 17th ACM Conference on Information and Knowledge Management, New York, NY, USA: ACM, pp. 649–658, http://webhome.cs.uvic.ca/~thomo/papers/cikm08suffixtrees.pdf .
* Barsky, Marina; Stege, Ulrike; Thomo, Alex; Upton, Chris (2009), "Suffix trees for very large genomic sequences", CIKM '09: Proceedings of the 18th ACM Conference on Information and Knowledge Management, New York, NY, USA: ACM, http://csci.viu.ca/~barskym/publications/CIKMconference2008.pdf .
* Farach, Martin (1997), "Optimal Suffix Tree Construction with Large Alphabets", 38th IEEE Symposium on Foundations of Computer Science (FOCS '97), pp. 137–143, https://users.cs.utah.edu/~pandey/courses/cs6968/spring23/papers/optsuffixtree.pdf .
* "Optimal Logarithmic Time Randomized Suffix Tree Construction", International Colloquium on Automata Languages and Programming, 1996, https://people.cs.rutgers.edu/~farach/pubs/PRAMSuffixICALP.pdf .
* "On the sorting-complexity of suffix tree construction.", Journal of the ACM 47 (6): 987–1011, 2000, doi:10.1145/355541.355547 .
* Giegerich, R.; Kurtz, S. (1997), "From Ukkonen to McCreight and Weiner: A Unifying View of Linear-Time Suffix Tree Construction", Algorithmica 19 (3): 331–353, doi:10.1007/PL00009177, http://europa.zbh.uni-hamburg.de/pubs/pdf/GieKur1997.pdf, retrieved 2012-07-13 .
* Gusfield, Dan (1997), Algorithms on Strings, Trees and Sequences: Computer Science and Computational Biology, Cambridge University Press, ISBN 0-521-58519-8 .
* Hariharan, Ramesh (1994), "Optimal Parallel Suffix Tree Construction", ACM Symposium on Theory of Computing, https://www.sciencedirect.com/science/article/pii/S0022000097914963/pdf?md5=cf7ddbddc821ab41ef268fb03f6bdbaf&pid=1-s2.0-S0022000097914963-main.pdf .
* Iliopoulos, Costas; Rytter, Wojciech (2004), "On Parallel Transformations of Suffix Arrays into Suffix Trees", 15th Australasian Workshop on Combinatorial Algorithms .
* Mansour, Essam; Allam, Amin; Skiadopoulos, Spiros; Kalnis, Panos (2011), "ERA: Efficient Serial and Parallel Suffix Tree Construction for Very Long Strings", Proceedings of the VLDB Endowment 5 (1): 49–60, doi:10.14778/2047485.2047490, Bibcode: 2011arXiv1109.6884M, http://www.vldb.org/pvldb/vol5/p049_essammansour_vldb2012.pdf .
* "A Space-Economical Suffix Tree Construction Algorithm", Journal of the ACM 23 (2): 262–272, 1976, doi:10.1145/321941.321946 .
* Phoophakdee, Benjarath; Zaki, Mohammed J. (2007), "Genome-scale disk-based suffix tree indexing", SIGMOD '07: Proceedings of the ACM SIGMOD International Conference on Management of Data, New York, NY, USA: ACM, pp. 833–844 .
* Sahinalp, Cenk; Vishkin, Uzi (1994), "Symmetry breaking for suffix tree construction", ACM Symposium on Theory of Computing, pp. 300–309, doi:10.1145/195058.195164, ISBN 0-89791-663-8
* Smyth, William (2003), Computing Patterns in Strings, Addison-Wesley .
* Shun, Julian; Blelloch, Guy E. (2014), "A Simple Parallel Cartesian Tree Algorithm and its Application to Parallel Suffix Tree Construction", ACM Transactions on Parallel Computing 1: 1–20, doi:10.1145/2661653 .
* Tata, Sandeep; Hankins, Richard A.; Patel, Jignesh M. (2003), "Practical Suffix Tree Construction", VLDB '03: Proceedings of the 30th International Conference on Very Large Data Bases, Morgan Kaufmann, pp. 36–47, http://www.vldb.org/conf/2004/RS1P3.PDF .
* Ukkonen, E. (1995), "On-line construction of suffix trees", Algorithmica 14 (3): 249–260, doi:10.1007/BF01206331, http://www.cs.helsinki.fi/u/ukkonen/SuffixT1withFigs.pdf .
* Weiner, P. (1973), "Linear pattern matching algorithms", 14th Annual IEEE Symposium on Switching and Automata Theory, pp. 1–11, doi:10.1109/SWAT.1973.13, http://airelles.i3s.unice.fr/files/Weiner.pdf, retrieved 2015-04-16 .
* Zamir, Oren; Etzioni, Oren (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, New York, NY, USA: ACM, pp. 46–54 .

## External links

* Suffix Trees by Sartaj Sahni
* NIST's Dictionary of Algorithms and Data Structures: Suffix Tree
* 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
* Ukkonen's Suffix Tree Implementation in C Part 1 Part 2 Part 3 Part 4 Part 5 Part 6
* Online Demo: Ukkonen's Suffix Tree Visualization

* v
* t
* e

Tree data structures

Search trees
(dynamic sets/associative arrays)|

* 2–3
* 2–3–4
* AA
* (a,b)
* AVL
* B
* B+
* B*
* Bx
* (Optimal) Binary search
* Dancing
* HTree
* Interval
* Order statistic
* (Left-leaning) Red-black
* Scapegoat
* Splay
* T
* Treap
* UB
* Weight-balanced

Heaps|

* Binary
* Binomial
* Brodal
* Fibonacci
* Leftist
* Pairing
* Skew
* van Emde Boas
* Weak

Tries|

* Ctrie
* C-trie (compressed ADT)
* Hash
* Radix
* Suffix
* Ternary search
* X-fast
* Y-fast

Spatial data partitioning trees|

* Ball
* BK
* BSP
* Cartesian
* Hilbert R
* k-d (implicit k-d)
* M
* Metric
* MVP
* Octree
* Priority R
* Quad
* R
* R+
* R*
* Segment
* VP
* X

Other trees|

* Cover
* Exponential
* Fenwick
* Finger
* Fractal tree index
* Fusion
* Hash calendar
* iDistance
* K-ary
* Left-child right-sibling
* Link/cut
* Log-structured merge
* Merkle
* PQ
* Range
* SPQR
* Top

* v
* t
* e

Strings

String metric|

* Approximate string matching
* Bitap algorithm
* Damerau–Levenshtein distance
* Edit distance
* Gestalt Pattern Matching
* Hamming distance
* Jaro–Winkler distance
* Lee distance
* Levenshtein automaton
* Levenshtein distance
* Wagner–Fischer algorithm

String-searching algorithm|

* Apostolico–Giancarlo algorithm
* Boyer–Moore string-search algorithm
* Boyer–Moore–Horspool algorithm
* Knuth–Morris–Pratt algorithm
* Rabin–Karp algorithm

Multiple string searching|

* Aho–Corasick
* Commentz-Walter algorithm

Regular expression|

* Comparison of regular expression engines
* Regular tree grammar
* Thompson's construction
* Nondeterministic finite automaton

Sequence alignment|

* Hirschberg's algorithm
* Needleman–Wunsch algorithm
* Smith–Waterman algorithm

Data structures|

* DAFSA
* Suffix array
* Suffix automaton
* Suffix tree
* Generalized suffix tree
* Rope
* Ternary search tree
* Trie

Other|

* Parsing
* Pattern matching
* Compressed pattern matching
* Longest common subsequence
* Longest common substring
* Sequential pattern mining
* Sorting

0.00

(0 votes)

Original source: https://en.wikipedia.org/wiki/Suffix tree. Read more

|

Retrieved from "https://handwiki.org/wiki/index.php?title=Suffix_tree&oldid=4231316"
*[v]: View this template
*[t]: Discuss this template
*[e]: Edit this template