1 / 71

Suffix trees

Suffix trees. Trie. A tree representing a set of strings. c. a. { aeef ad bbfe bbfg c }. b. e. b. d. e. f. f. e. g. Trie (Cont). Assume no string is a prefix of another. c. 1) Each edge is labeled by a letter,

arlo
Download Presentation

Suffix trees

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Suffix trees

  2. Trie • A tree representing a set of strings. c a { aeef ad bbfe bbfg c } b e b d e f f e g

  3. Trie (Cont) • Assume no string is a prefix of another c 1) Each edge is labeled by a letter, 2) No two edges outgoing from the same node are labeled the same. 3) Each string corresponds to a leaf. a b e b d e f f e g

  4. Compressed Trie • Compress unary nodes, label edges by strings c  c a a b e b bbf d d eef e f f e g e g

  5. Suffix tree Given a string s a suffix tree of s is a compressed trie of all suffixes of s To make these suffixes prefix-free we add a special character, say $, at the end of s

  6. a b b $ a a $ b b $ $ Suffix tree (Example) Let s=abab,a suffix tree of s is a compressed trie of all suffixes of s=abab$ $ { $ b$ ab$ bab$ abab$ } Note that a suffix tree has O(n) nodes n = |s| (why?)

  7. Trivial algorithm to build a Suffix tree a b Put the largest suffix in a b $ a b b a Put the suffix bab$ in a b b $ $

  8. a b b a a b b $ $ Put the suffix ab$ in a b b a b $ a $ b $

  9. a b b a b $ a $ b $ Put the suffix b$ in a b b $ a a $ b b $ $

  10. a b b $ a a $ b b $ $ $ Put the suffix $ in a b b $ a a $ b b $ $

  11. $ a b b $ a a $ b b $ $ We will also label each leaf with the starting point of the corres. suffix. $ a b 5 b $ a a $ b 4 b $ $ 3 2 1

  12. Analysis Takes O(n2) time to build. It is possible to construct it in O(n) time But, how come? does it take O(n) space ?

  13. Linear space ? • Consider the string aaaaaabbbbbb $ a b $ bbbbbb$ a b $ bbbbbb$ b a $ b bbbbbb$ a $ b bbbbbb$ c $ a b$ bbbbbb$ abbbbbb$

  14. To use only O(n) space encode the edge-labels • Consider the string aaaaaabbbbbb $ a b $ (7,13) a b $ bbbbbb$ b a $ b bbbbbb$ a $ b bbbbbb$ c $ a b$ bbbbbb$ abbbbbb$

  15. To use only O(n) space encode the edge-labels • Consider the string aaaaaabbbbbb (13,13) (1,1) (7,7) (7,13) (1,1) (7,7) (13,13) (7,13) (7,7) (1,1) (13,13) (7,7) (7,13) (1,1) (13,13) (7,7) (7,13) c (13,13) (1,1) (12,13) (7,13) (6,13)

  16. What can we do with it ? Exact string matching: Given a text T, |T| = n, preprocess it such that when a pattern P, |P|=m, arrives you can quickly decide if it occurs in T. We may also want to find all occurrences of P in T

  17. Exact string matching In preprocessing we just build a suffix tree in O(n) time $ a b 5 b $ a a $ b 4 b $ $ 3 2 1 Given a pattern P = ab we traverse the tree according to the pattern.

  18. $ a b 5 b $ a a $ b 4 b $ $ 3 2 1 If we did not get stuck traversing the pattern then the pattern occurs in the text. Each leaf in the subtree below the node we reach corresponds to an occurrence. By traversing this subtree we get all k occurrences in O(m+k) time

  19. Generalized suffix tree Given a set of strings S a generalized suffix tree of S is a compressed trie of all suffixes of s  S To associate each suffix with a unique string in S add a different special char to each s

  20. Generalized suffix tree (Example) Let s1=abab and s2=aab here is a generalized suffix tree for s1 and s2 # $ { $ # b$ b# ab$ ab# bab$ aab# abab$ } a b 4 5 # $ a b a 3 b b 4 $ # # a $ 2 1 b $ 3 2 1

  21. So what can we do with it ? Matching a pattern against a database of strings

  22. Longest common substring (of two strings) Every node with a leaf descendant from string s1 and a leaf descendant from string s2 represents a common substring. A maximal common substring corresponds to such node. # $ a b 4 5 # $ a b a 3 b b 4 $ # # a $ 2 1 b $ 3 2 1 Find such node with largest “string depth”

  23. Lowest common ancetors A lot more can be gained from the suffix tree if we preprocess it so that we can answer LCA queries on it

  24. Why? The LCA of two leaves represents the longest common prefix (LCP) of these 2 suffixes # $ a b 4 5 # $ a b a 3 b b 4 $ # # a $ 2 1 b $ 3 2 1

  25. Lowest common ancestors

  26. 0 Shallowest node 12 8 3 9 1 2 11 4 7 10 5 6 Write an Euler tour of the tree 3 3 2 3 1 4 1 7 1 3 5 6 5 3 5 1 2 4 6 7 LCA(1,5) = 3

  27. 0 minimum 12 8 3 9 1 2 11 4 7 10 5 6 3 5 1 2 4 6 7 3 2 3 1 4 1 7 1 3 5 6 5 3 Node id Level 0 1 0 1 2 1 2 1 0 1 2 1 0

  28. 0 minimum 12 8 3 9 1 2 11 4 7 10 5 6 Range minimum Preprocess an array, such that given i,j you can find the minimum in [i,j] fast 3 5 Reduction takes linear time 1 2 4 6 7 3 2 3 1 4 1 7 1 3 5 6 5 3 0 1 0 1 2 1 2 1 0 1 2 1 0

  29. Trivial algorithms for RMQ • O(n) space, O(n) query time • O(n2) space, O(1) query time

  30. Less trivial algorithms to RMQ • Try to use O(nlog(n)) space to do a query in O(1) time.. • Simpler: O(n + sqrt(n)) to do a query in sqrt(n)

  31. Lowest common ancetors A lot more can be gained from the suffix tree if we preprocess it so that we can answer LCA queries on it

  32. Why? The LCA of two leaves represents the longest common prefix (LCP) of these 2 suffixes # $ a b 4 5 # $ a b a 3 b b 4 $ # # a $ 2 1 b $ 3 2 1

  33. Finding maximal palindromes • A palindrome: caabaac, cbaabc • Want to find all maximal palindromes in a string s Let s = cbaaba The maximal palindrome with center between i-1 and i is the LCP of the suffix at position i of s and the suffix at position n-i+2 of sr

  34. Maximal palindromes algorithm Prepare a generalized suffix tree for s = cbaaba$ and sr = abaabc# For every i, find the LCA of suffix i of s and suffix n-i+2 of sr

  35. Let s = cbaaba$ then sr = abaabc# a # b $ c 7 7 a b $ b a c # baaba$ c # 6 c # a $ a b 6 a $ 4 abc # 5 5 $ 3 3 c # a $ 4 1 2 2 1

  36. Analysis O(n) time to identify all palindromes

  37. Suffix trees for compression

  38. Compression S[1..n] = aabcbbabbsbabcbbbbabbabcbbabbabbsb… aabcbbabbsb(2,5)bbabbabcbbabbabbsb…

  39. Compression S[1..n] = aabcbbabbsbabcbbbbabbabcbbabbabbsb… aabcbbabbsb(2,5)bbabbabcbbabbabbsb… Let Li be the length of the longest prefix of S[i..n]that is a substring of S[1..i-1] Letsithe previous position of this prefix Let priori=S[i..i+Li-1] = S[si..si+Li-1] be this prefix

  40. Compression prior12 S[1..n]= aabcbbabbsbabcbbbbabbabcbbabbabbsb… i=12 aabcbbabbsb(2,5)bbabbabcbbabbabbsb… (s12,L12)

  41. “Ziv-Lempel” compression For (i=1 ; i<=n ;;) {Compute (si,Li) if Li>1 {output(si,Li); i=i+Li}; else {output(S[i]); i=i+1}} aabcbbabbsbabbbbbabbabcbbabbabbsb… aabcbb bs ?(si,Li) איך לחשב את (2,2) (6,4)

  42. Implementation using suffix tree Before compression: • Build a suffix tree T for S. • For each node v, compute cv : • the smallest leaf index in v’s subtree. • the starting position of the leftmost copy of the substring that labels the path from the root to v. • O(n) time.

  43. root computing (si,Li): Let v be the “first” node such that: string-len(v) + cv≥ i v S[i...n] cv i string-len(v) set si←cv set Li←i-cv leaf i

  44. Example S = abababab 1 2 3 4 5 6 7 8 i=1 Li=0  a i=2 Li=0  b i=3 Li=2 si=1  (1,2) i=5 Li=4 si=1  (1,4) 1 a b b 2 v1 1 a a b b v2 2 1 a a b $ $ b 2 $ $ 1 a a b b $ $ $ $ 2 4 6 8 7 5 3 1

  45. Suffix array • We loose some of the functionality but we save space. Let s = abab Sort the suffixes lexicographically: ab, abab, b, bab The suffix array gives the indices of the suffixes in sorted order 2 0 3 1

  46. How do we build it ? • Build a suffix tree • Traverse the tree in in-order, lexicographically picking edges outgoing from each node and fill the suffix array. • O(n) time • Can also build it directly

  47. 10 7 4 1 0 9 8 6 3 5 2 Example Let S = mississippi i L ippi issippi Let P = issa ississippi mississippi pi M ppi sippi sisippi ssippi ssissippi R

  48. How do we search for a pattern ? • If P occurs in T then all its occurrences are consecutive in the suffix array. • Do a binary search on the suffix array • Takes O(mlogn) time • Can also do it in O(m+log(n)) with an additional array

  49. Computing the suffix array • Can do it in linear time without constructing the suffix tree ?

  50. Divide into triples y a b b a b o d a b a d $ abb ada bba do$

More Related