lecture 15 suffix trees suffix arrays and their
play

Lecture 15: Suffix trees, suffix arrays, and their - PowerPoint PPT Presentation

Lecture 15: Suffix trees, suffix arrays, and their applica8ons Spring 2017 April 4, 2017 1 Trie A tree represen8ng a set of strings. {aeef, ad,


  1. Lecture ¡15: ¡Suffix ¡trees, ¡suffix ¡arrays, ¡and ¡ their ¡applica8ons ¡ Spring ¡2017 ¡ April ¡4, ¡2017 ¡ 1

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

  3. Trie ¡ Assume ¡no ¡string ¡is ¡a ¡prefix ¡of ¡another ¡ Each ¡edge ¡is ¡labeled ¡by ¡a ¡leIer, ¡ no ¡two ¡edges ¡outgoing ¡from ¡ c a the ¡same ¡node ¡have ¡the ¡same ¡ b label. ¡ e ¡ b d Each ¡string ¡corresponds ¡to ¡a ¡ leaf. ¡ e f f e g

  4. Compressed ¡Trie ¡ Compress ¡nodes ¡with ¡single ¡outgoing ¡edge, ¡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 ¡T, ¡a ¡suffix ¡tree ¡of ¡T ¡is ¡a ¡compressed ¡ trie ¡of ¡all ¡suffixes ¡of ¡T ¡ ¡ To ¡make ¡these ¡suffixes ¡prefix-­‑free ¡we ¡add ¡a ¡special ¡ character, ¡say ¡$, ¡at ¡the ¡end ¡of ¡T ¡ ¡ ¡

  6. Example ¡ Suffix ¡tree ¡for ¡the ¡string ¡T=abab$ ¡ {$, ¡b$, ¡ab$,bab$, ¡abab$} ¡ $ a b b $ a a $ b b $ $

  7. Suffix ¡tree ¡ A ¡suffix ¡tree ¡for ¡an ¡m-­‑character ¡string ¡T: ¡ A ¡rooted ¡directed ¡tree ¡with ¡exactly ¡m ¡leaves ¡numbered ¡from ¡1 ¡to ¡m. ¡ ¡ q Each ¡internal ¡node, ¡other ¡than ¡root, ¡has ¡at ¡least ¡two ¡children ¡and ¡each ¡ q edge ¡is ¡labeled ¡with ¡a ¡non-­‑empty ¡substring ¡of ¡T. ¡ ¡ No ¡two ¡edges ¡out ¡of ¡a ¡node ¡can ¡have ¡edge-­‑labels ¡beginning ¡with ¡the ¡ q same ¡character. ¡ ¡ For ¡any ¡leaf ¡i, ¡the ¡concatena8on ¡of ¡the ¡edge-­‑labels ¡on ¡the ¡path ¡from ¡ q the ¡root ¡to ¡leaf ¡i ¡exactly ¡spells ¡out ¡the ¡suffix ¡of ¡T ¡that ¡starts ¡at ¡posi8on ¡ i, ¡that ¡is, ¡spells ¡out ¡T[i,…,m]. ¡ $ a b b $ a a $ b b $ $ 7

  8. Naïve ¡algorithm ¡to ¡build ¡suffix ¡tree ¡ Construc8ng ¡suffix ¡tree ¡for ¡T=abab$ ¡ ¡ a b Insert ¡the ¡longest ¡suffix ¡ a b $ a b b a Next ¡longest ¡ a b b $ $

  9. a b b a a b b $ $ Insert ¡the ¡suffix ¡ab$ ¡ a b b a b $ a $ b $

  10. a b b a b $ a $ b $ Insert ¡the ¡suffix ¡b$ ¡ ¡ a b b $ a a $ b b $ $

  11. a b b $ a a $ b b $ $ $ Insert ¡the ¡suffix ¡$ ¡ a b b $ a a $ b b $ $

  12. $ a b b $ a a $ b b $ $ label ¡each ¡leaf ¡with ¡the ¡star8ng ¡posi8on ¡of ¡the ¡corresponding ¡ suffix. ¡ ¡ $ a b 5 b $ a a $ b 4 b $ $ 3 2 1

  13. Analysis ¡ How ¡long ¡does ¡it ¡take ¡to ¡construct ¡a ¡suffix ¡ tree ¡for ¡a ¡text ¡of ¡length ¡m? ¡ ¡ ¡ ¡ ¡

  14. Analysis ¡ How ¡long ¡does ¡it ¡take ¡to ¡construct ¡a ¡suffix ¡ tree ¡for ¡a ¡text ¡of ¡length ¡m? ¡ ¡ O(m) ¡to ¡insert ¡a ¡given ¡suffix, ¡so ¡O(m 2 ) ¡overall. ¡ ¡ There ¡are ¡algorithms ¡that ¡do ¡it ¡in ¡O(m)! ¡ ¡ (Weiner ¡1973, ¡McCreight ¡1976, ¡Ukkonen ¡ 1975) ¡ ¡ ¡

  15. What ¡can ¡we ¡do ¡with ¡it? ¡ Exact ¡string ¡matching: ¡ Given ¡a ¡Text ¡T, ¡|T| ¡= ¡m, ¡and ¡a ¡string ¡s, ¡|s| ¡= ¡n, ¡ does ¡s ¡occur ¡in ¡T? ¡ ¡ Naïve ¡solu8on: ¡ ¡search ¡for ¡occurrence ¡of ¡s ¡at ¡ every ¡posi8on ¡in ¡T. ¡ Running ¡8me? ¡ ¡ Using ¡suffix ¡trees: ¡ ¡Construct ¡suffix ¡tree ¡for ¡T. ¡ ¡ How ¡do ¡we ¡now ¡check ¡if ¡s ¡occurs ¡in ¡T? ¡ ¡

  16. Exact ¡string ¡matching ¡ Is ¡aba ¡a ¡substring ¡of ¡T=abab? ¡ $ a b 5 b $ a a $ b 4 b $ $ 3 2 1

  17. Exact ¡string ¡matching ¡ Is ¡aba ¡a ¡substring ¡of ¡T=abab? ¡ Traverse ¡the ¡tree ¡using ¡the ¡string. ¡ ¡ ¡ $ a b 5 b $ a a $ b 4 b $ $ 3 2 1

  18. $ a b 5 b $ a a $ b 4 b $ $ 3 2 1 If ¡we ¡did ¡not ¡get ¡stuck ¡traversing ¡the ¡paIern ¡then ¡the ¡ paIern ¡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(n+k) ¡ 8me. ¡

  19. Another ¡example ¡ T: ¡xabxac ¡ s: ¡xa ¡ 19

  20. Running ¡8me ¡ q Build ¡the ¡suffix ¡tree: ¡O(m) ¡ ¡ q Match ¡P ¡to ¡the ¡unique ¡path: ¡O(n) ¡ q Traverse ¡the ¡tree ¡below ¡the ¡last ¡matching ¡ point: ¡O(k), ¡where ¡k ¡is ¡the ¡number ¡of ¡ occurrences, ¡i.e., ¡the ¡number ¡of ¡leaves ¡ below ¡the ¡last ¡matching ¡point. ¡ q Overall ¡O(m+n+k). ¡ 20

  21. A ¡collec8on ¡of ¡strings ¡ Problem: ¡ ¡Matching ¡a ¡string ¡against ¡a ¡database ¡of ¡strings ¡

  22. Generalized ¡suffix ¡tree ¡ A ¡generalized ¡suffix ¡tree ¡for ¡t 1 =abab ¡and ¡t 2 =aab ¡ # $ { a b $ # 4 5 b$ b# # $ a ab$ ab# b a 3 b b bab$ aab# 4 $ # abab$ # a $ 2 } 1 b $ 3 2 1

  23. Generalized ¡suffix ¡tree ¡ Given ¡a ¡ set ¡of ¡strings ¡T ¡a ¡generalized ¡suffix ¡tree ¡of ¡ T ¡is ¡a ¡compressed ¡trie ¡of ¡all ¡suffixes ¡of ¡t ¡ ∈ ¡T. ¡ ¡ ¡ To ¡associate ¡each ¡suffix ¡with ¡a ¡unique ¡string ¡in ¡T ¡ add ¡a ¡different ¡special ¡character ¡to ¡each ¡string ¡in ¡ T. ¡ ¡ ¡

  24. More ¡applica8on ¡ Inexact ¡string ¡matching; ¡used ¡in ¡mo8f ¡ • finding ¡(e.g. ¡the ¡Weeder ¡algorithm) ¡ ¡ Longest ¡common ¡substring ¡ • ¡ Repeats/tandem ¡repeats ¡ • ¡ Maximal ¡palindromes ¡ • ¡ 24

  25. Drawbacks ¡ Suffix ¡trees ¡consume ¡a ¡lot ¡of ¡space: ¡ It ¡is ¡O(m) ¡but ¡the ¡constant ¡is ¡quite ¡big. ¡ ¡Cannot ¡ fit ¡a ¡large ¡mammalian ¡genome ¡in ¡memory. ¡ ¡ No8ce ¡that ¡if ¡we ¡indeed ¡want ¡to ¡traverse ¡an ¡ edge ¡in ¡O(1) ¡8me ¡then ¡we ¡need ¡an ¡array ¡of ¡ pointers ¡of ¡size ¡|Σ| ¡in ¡each ¡node. ¡ ¡Otherwise, ¡ use ¡a ¡linked ¡list, ¡and ¡traversal ¡8me ¡depends ¡on ¡ |Σ|. ¡

  26. Suffix ¡arrays ¡ Use ¡less ¡space, ¡but ¡not ¡as ¡fast. ¡ ¡ Let ¡ ¡T ¡= ¡abab. ¡ Sort ¡the ¡suffixes ¡lexicographically: ¡ ¡ ab, ¡abab, ¡b, ¡bab ¡ The ¡suffix ¡array ¡gives ¡the ¡indices ¡of ¡the ¡ suffixes ¡in ¡sorted ¡order. ¡ ¡ ¡ 3 1 4 2

  27. Example ¡ T ¡= ¡mississippi ¡ i L 11 ippi 8 issippi s ¡= ¡issa ¡ 5 ississippi 2 mississippi 1 pi M 10 ppi 9 sippi 7 sisippi 4 ssippi 6 ssissippi R 3

  28. Searching ¡a ¡suffix ¡array ¡ If ¡s ¡occurs ¡in ¡T ¡then ¡all ¡its ¡occurrences ¡are ¡ consecu8ve ¡in ¡the ¡suffix ¡array. ¡ ¡ Do ¡a ¡binary ¡search ¡on ¡the ¡suffix ¡array. ¡ ¡ Takes ¡O(n ¡log ¡m) ¡8me. ¡

  29. Construc8ng ¡a ¡suffix ¡array ¡ Build ¡a ¡suffix ¡tree. ¡Traverse ¡the ¡tree ¡in ¡DFS, ¡ lexicographically ¡picking ¡edges ¡outgoing ¡from ¡ each ¡node ¡and ¡fill ¡the ¡suffix ¡array: ¡O(m) ¡8me. ¡ ¡ ¡ But ¡if ¡our ¡objec8ve ¡was ¡to ¡save ¡space, ¡do ¡it ¡ directly: ¡O(mr ¡ log ¡m) ¡8me, ¡r ¡= ¡longest ¡repeat ¡ length. ¡

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend