Suffix tree

From HandWiki - Reading time: 13 min

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 [math]\displaystyle{ S }[/math] takes time and space linear in the length of [math]\displaystyle{ S }[/math]. Once constructed, several operations can be performed quickly, such as locating a substring in [math]\displaystyle{ S }[/math], 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.

History

The concept was first introduced by (Weiner 1973). Rather than the suffix [math]\displaystyle{ S[i..n] }[/math], Weiner stored in his trie[3] the prefix identifier for each position, that is, the shortest string starting at [math]\displaystyle{ i }[/math] and occurring only once in [math]\displaystyle{ S }[/math]. His Algorithm D takes an uncompressed[4] trie for [math]\displaystyle{ S[k+1..n] }[/math] and extends it into a trie for [math]\displaystyle{ S[k..n] }[/math]. This way, starting from the trivial trie for [math]\displaystyle{ S[n..n] }[/math], a trie for [math]\displaystyle{ S[1..n] }[/math] can be built by [math]\displaystyle{ n - 1 }[/math] successive calls to Algorithm D; however, the overall run time is [math]\displaystyle{ O(n^2) }[/math]. Weiner's Algorithm B maintains several auxiliary data structures, to achieve an over all run time linear in the size of the constructed trie. The latter can still be [math]\displaystyle{ O(n^2) }[/math] nodes, e.g. for [math]\displaystyle{ S = a^n b^n a^n b^n \$ . }[/math] 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 [math]\displaystyle{ S }[/math]. Although the suffix starting at [math]\displaystyle{ i }[/math] 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 [math]\displaystyle{ O(n\log n) }[/math] 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 [math]\displaystyle{ S }[/math] of length [math]\displaystyle{ n }[/math] is defined as a tree such that:[7]

  1. The tree has exactly n leaves numbered from [math]\displaystyle{ 1 }[/math] to [math]\displaystyle{ n }[/math].
  2. Except for the root, every internal node has at least two children.
  3. Each edge is labelled with a non-empty substring of [math]\displaystyle{ S }[/math].
  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 [math]\displaystyle{ i }[/math] spells out suffix [math]\displaystyle{ S[i..n] }[/math], for [math]\displaystyle{ i }[/math] from [math]\displaystyle{ 1 }[/math] to [math]\displaystyle{ n }[/math].

If the a suffix of [math]\displaystyle{ S }[/math] 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, [math]\displaystyle{ S }[/math] 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 [math]\displaystyle{ n }[/math] leaf nodes, one for each of the [math]\displaystyle{ n }[/math] suffixes of [math]\displaystyle{ S }[/math].[8] Since all internal non-root nodes are branching, there can be at most [math]\displaystyle{ n-1 }[/math] such nodes, and [math]\displaystyle{ n+(n-1)+1=2n }[/math] nodes in total ([math]\displaystyle{ n }[/math] leaves, [math]\displaystyle{ n-1 }[/math] 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 [math]\displaystyle{ \chi\alpha }[/math], where [math]\displaystyle{ \chi }[/math] is a single character and [math]\displaystyle{ \alpha }[/math] is a string (possibly empty), it has a suffix link to the internal node representing [math]\displaystyle{ \alpha }[/math]. 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 [math]\displaystyle{ S }[/math] of length [math]\displaystyle{ n }[/math] can be built in [math]\displaystyle{ \Theta(n) }[/math] 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 [math]\displaystyle{ O(n) }[/math]; in general, this takes [math]\displaystyle{ O(n\log n) }[/math] 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 [math]\displaystyle{ S }[/math] of length [math]\displaystyle{ n }[/math], or that a generalised suffix tree has been built for the set of strings [math]\displaystyle{ D=\{S_1,S_2,\dots,S_K\} }[/math] of total length [math]\displaystyle{ n=n_1+n_2+\cdots+n_K }[/math]. You can:

  • Search for strings:
    • Check if a string [math]\displaystyle{ P }[/math] of length [math]\displaystyle{ m }[/math] is a substring in [math]\displaystyle{ O(m) }[/math] time.[10]
    • Find the first occurrence of the patterns [math]\displaystyle{ P_1,\dots,P_q }[/math] of total length [math]\displaystyle{ m }[/math] as substrings in [math]\displaystyle{ O(m) }[/math] time.
    • Find all [math]\displaystyle{ z }[/math] occurrences of the patterns [math]\displaystyle{ P_1,\dots,P_q }[/math] of total length [math]\displaystyle{ m }[/math] as substrings in [math]\displaystyle{ O(m + z) }[/math] time.[11]
    • Search for a regular expression P in time expected sublinear in [math]\displaystyle{ n }[/math].[12]
    • Find for each suffix of a pattern [math]\displaystyle{ P }[/math], the length of the longest match between a prefix of [math]\displaystyle{ P[i\dots m] }[/math] and a substring in [math]\displaystyle{ D }[/math] in [math]\displaystyle{ \Theta(m) }[/math] time.[13] This is termed the matching statistics for [math]\displaystyle{ P }[/math].
  • Find properties of the strings:
    • Find the longest common substrings of the string [math]\displaystyle{ S_i }[/math] and [math]\displaystyle{ S_j }[/math] in [math]\displaystyle{ \Theta(n_i + n_j) }[/math] time.[14]
    • Find all maximal pairs, maximal repeats or supermaximal repeats in [math]\displaystyle{ \Theta(n + z) }[/math] time.[15]
    • Find the Lempel–Ziv decomposition in [math]\displaystyle{ \Theta(n) }[/math] time.[16]
    • Find the longest repeated substrings in [math]\displaystyle{ \Theta(n) }[/math] time.
    • Find the most frequently occurring substrings of a minimum length in [math]\displaystyle{ \Theta(n) }[/math] time.
    • Find the shortest strings from [math]\displaystyle{ \Sigma }[/math] that do not occur in [math]\displaystyle{ D }[/math], in [math]\displaystyle{ O(n + z) }[/math] time, if there are [math]\displaystyle{ z }[/math] such strings.
    • Find the shortest substrings occurring only once in [math]\displaystyle{ \Theta(n) }[/math] time.
    • Find, for each [math]\displaystyle{ i }[/math], the shortest substrings of [math]\displaystyle{ S_i }[/math] not occurring elsewhere in [math]\displaystyle{ D }[/math] in [math]\displaystyle{ \Theta(n) }[/math] time.

The suffix tree can be prepared for constant time lowest common ancestor retrieval between nodes in [math]\displaystyle{ \Theta(n) }[/math] time.[17] One can then also:

  • Find the longest common prefix between the suffixes [math]\displaystyle{ S_i[p..n_i] }[/math] and [math]\displaystyle{ S_j[q..n_j] }[/math] in [math]\displaystyle{ \Theta(1) }[/math].[18]
  • Search for a pattern P of length m with at most k mismatches in [math]\displaystyle{ O(k n + z) }[/math] time, where z is the number of hits.[19]
  • Find all [math]\displaystyle{ z }[/math] maximal palindromes in [math]\displaystyle{ \Theta(n) }[/math],[20] or [math]\displaystyle{ \Theta(g n) }[/math] time if gaps of length [math]\displaystyle{ g }[/math] are allowed, or [math]\displaystyle{ \Theta(k n) }[/math] if [math]\displaystyle{ k }[/math] mismatches are allowed.[21]
  • Find all [math]\displaystyle{ z }[/math] tandem repeats in [math]\displaystyle{ O(n \log n + z) }[/math], and k-mismatch tandem repeats in [math]\displaystyle{ O(k n \log (n/k) + z) }[/math].[22]
  • Find the longest common substrings to at least [math]\displaystyle{ k }[/math] strings in [math]\displaystyle{ D }[/math] for [math]\displaystyle{ k=2,\dots,K }[/math] in [math]\displaystyle{ \Theta(n) }[/math] time.[23]
  • Find the longest palindromic substring of a given string (using the generalized suffix tree of the string and its reverse) in linear time.[24]

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

  • 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.[26]

Implementation

If each node and edge can be represented in [math]\displaystyle{ \Theta(1) }[/math] space, the entire tree can be represented in [math]\displaystyle{ \Theta(n) }[/math] space. The total length of all the strings on all of the edges in the tree is [math]\displaystyle{ O(n^2) }[/math], but each edge can be stored as the position and length of a substring of S, giving a total space usage of [math]\displaystyle{ \Theta(n) }[/math] computer words. The worst-case space usage of a suffix tree is seen with a fibonacci word, giving the full [math]\displaystyle{ 2n }[/math] 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:

Lookup Insertion Traversal
Sibling lists / unsorted arrays O(σ) Θ(1) Θ(1)
Bitwise sibling trees O(log σ) Θ(1) Θ(1)
Hash maps Θ(1) Θ(1) O(σ)
Balanced search tree O(log σ) O(log σ) O(1)
Sorted arrays O(log σ) O(σ) O(1)
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.[27][28][29][30][31] Recently, a practical parallel algorithm for suffix tree construction with [math]\displaystyle{ O(n) }[/math] work (sequential time) and [math]\displaystyle{ O(\log^2 n) }[/math] 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.[32]

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.[33]

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,[34] TRELLIS,[35] DiGeST,[36] and B2ST.[37]

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.[34][35] However, these methods cannot handle efficiently collections of sequences exceeding 3GB.[36] DiGeST performs significantly better and is able to handle collections of sequences in the order of 6GB in about 6 hours.[36]

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,[37] 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 16GB RAM. On a simple Linux cluster with 16 nodes (4GB RAM per node), ERA can index the entire human genome in less than 9 minutes.[38]

See also

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).
  13. (Gusfield 1999), p.132.
  14. (Gusfield 1999), p.125.
  15. (Gusfield 1999), p.144.
  16. (Gusfield 1999), p.166.
  17. (Gusfield 1999), Chapter 8.
  18. (Gusfield 1999), p.196.
  19. (Gusfield 1999), p.200.
  20. (Gusfield 1999), p.198.
  21. (Gusfield 1999), p.201.
  22. (Gusfield 1999), p.204.
  23. (Gusfield 1999), p.205.
  24. (Gusfield 1999), pp.197–199.
  25. 25.0 25.1 Allison, L.. "Suffix Trees". http://www.allisons.org/ll/AlgDS/Tree/Suffix/. 
  26. First introduced by (Zamir Etzioni).
  27. Apostolico et al. (1988).
  28. Hariharan (1994).
  29. Sahinalp & Vishkin (1994).
  30. Farach & Muthukrishnan (1996).
  31. Iliopoulos & Rytter (2004).
  32. Shun & Blelloch (2014).
  33. Smyth (2003).
  34. 34.0 34.1 (Tata Hankins).
  35. 35.0 35.1 (Phoophakdee Zaki).
  36. 36.0 36.1 36.2 (Barsky Stege).
  37. 37.0 37.1 (Barsky Stege).
  38. Mansour et al. (2011).

References

External links




Licensed under CC BY-SA 3.0 | Source: https://handwiki.org/wiki/Suffix_tree
9 views | Status: cached on August 04 2024 11:36:06
↧ Download this article as ZWI file
Encyclosphere.org EncycloReader is supported by the EncyclosphereKSF