SLIDE 1 Lecture ¡15: ¡Suffix ¡trees, ¡suffix ¡arrays, ¡and ¡ their ¡applica8ons ¡
Spring ¡2017 ¡ April ¡4, ¡2017 ¡
1
SLIDE 2
Trie ¡
A ¡tree ¡represen8ng ¡a ¡set ¡of ¡strings. ¡ a b c e e f d b f e g {aeef, ¡ad, ¡bbfe, ¡bbfg, ¡c} ¡
SLIDE 3 Assume ¡no ¡string ¡is ¡a ¡prefix ¡of ¡another ¡ a b c e e f d b f e g Each ¡edge ¡is ¡labeled ¡by ¡a ¡leIer, ¡ no ¡two ¡edges ¡outgoing ¡from ¡ the ¡same ¡node ¡have ¡the ¡same ¡
¡ Each ¡string ¡corresponds ¡to ¡a ¡
Trie ¡
SLIDE 4 Compress ¡nodes ¡with ¡single ¡outgoing ¡edge, ¡label ¡edges ¡by ¡ strings ¡ a b c e e f d b f e g a bbf c eef d e g
è
Compressed ¡Trie ¡
SLIDE 5
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 ¡ ¡ ¡
Suffix ¡tree ¡
SLIDE 6
Suffix ¡tree ¡for ¡the ¡string ¡T=abab$ ¡
{$, ¡b$, ¡ab$,bab$, ¡abab$} ¡ a b a b $ a b $ b $ $ $
Example ¡
SLIDE 7 A ¡suffix ¡tree ¡for ¡an ¡m-‑character ¡string ¡T: ¡
q
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 ¡ edge ¡is ¡labeled ¡with ¡a ¡non-‑empty ¡substring ¡of ¡T. ¡ ¡
q
No ¡two ¡edges ¡out ¡of ¡a ¡node ¡can ¡have ¡edge-‑labels ¡beginning ¡with ¡the ¡ same ¡character. ¡ ¡
q
For ¡any ¡leaf ¡i, ¡the ¡concatena8on ¡of ¡the ¡edge-‑labels ¡on ¡the ¡path ¡from ¡ the ¡root ¡to ¡leaf ¡i ¡exactly ¡spells ¡out ¡the ¡suffix ¡of ¡T ¡that ¡starts ¡at ¡posi8on ¡ i, ¡that ¡is, ¡spells ¡out ¡T[i,…,m]. ¡
7
a b a b $ a b $ b $ $ $
Suffix ¡tree ¡
SLIDE 8
Construc8ng ¡suffix ¡tree ¡for ¡T=abab$ ¡ ¡ Insert ¡the ¡longest ¡suffix ¡ Next ¡longest ¡ a b a b $ a b a b $ a b $ b
Naïve ¡algorithm ¡to ¡build ¡suffix ¡tree ¡
SLIDE 9
Insert ¡the ¡suffix ¡ab$ ¡ a b a b $ a b $ b a b a b $ a b $ b $
SLIDE 10
Insert ¡the ¡suffix ¡b$ ¡ ¡ a b a b $ a b $ b $ a b a b $ a b $ b $ $
SLIDE 11
Insert ¡the ¡suffix ¡$ ¡ a b a b $ a b $ b $ $ a b a b $ a b $ b $ $ $
SLIDE 12 label ¡each ¡leaf ¡with ¡the ¡star8ng ¡posi8on ¡of ¡the ¡corresponding ¡
a b a b $ a b $ b $ $ $
1 2
a b a b $ a b $ b
3
$
4
$
5
$
SLIDE 13
How ¡long ¡does ¡it ¡take ¡to ¡construct ¡a ¡suffix ¡ tree ¡for ¡a ¡text ¡of ¡length ¡m? ¡
¡ ¡ ¡ ¡
Analysis ¡
SLIDE 14
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(m2) ¡overall. ¡ ¡ There ¡are ¡algorithms ¡that ¡do ¡it ¡in ¡O(m)! ¡ ¡ (Weiner ¡1973, ¡McCreight ¡1976, ¡Ukkonen ¡ 1975) ¡ ¡ ¡
Analysis ¡
SLIDE 15 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? ¡
¡
What ¡can ¡we ¡do ¡with ¡it? ¡
SLIDE 16 1 2
a b a b $ a b $ b
3
$
4
$
5
$ Is ¡aba ¡a ¡substring ¡of ¡T=abab? ¡
Exact ¡string ¡matching ¡
SLIDE 17 1 2
a b a b $ a b $ b
3
$
4
$
5
$ Is ¡aba ¡a ¡substring ¡of ¡T=abab? ¡ Traverse ¡the ¡tree ¡using ¡the ¡string. ¡ ¡ ¡
Exact ¡string ¡matching ¡
SLIDE 18 1 2
a b a b $ a b $ b
3
$
4
$
5
$ 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) ¡
SLIDE 19 19
T: ¡xabxac ¡ s: ¡xa ¡
Another ¡example ¡
SLIDE 20 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 ¡
- ccurrences, ¡i.e., ¡the ¡number ¡of ¡leaves ¡
below ¡the ¡last ¡matching ¡point. ¡
q Overall ¡O(m+n+k). ¡
20
Running ¡8me ¡
SLIDE 21
Problem: ¡ ¡Matching ¡a ¡string ¡against ¡a ¡database ¡of ¡strings ¡
A ¡collec8on ¡of ¡strings ¡
SLIDE 22 A ¡generalized ¡suffix ¡tree ¡for ¡t1=abab ¡and ¡t2=aab ¡
{ $ # b$ b# ab$ ab# bab$ aab# abab$ }
1 2
a b a b $ a b $ b
3
$
4
$
5
$
1
b # a
2
#
3
#
4
#
Generalized ¡suffix ¡tree ¡
SLIDE 23 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 ¡
¡
Generalized ¡suffix ¡tree ¡
SLIDE 24
- Inexact ¡string ¡matching; ¡used ¡in ¡mo8f ¡
finding ¡(e.g. ¡the ¡Weeder ¡algorithm) ¡ ¡
- Longest ¡common ¡substring ¡
¡
- Repeats/tandem ¡repeats ¡
¡
¡
24
More ¡applica8on ¡
SLIDE 25
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 ¡ |Σ|. ¡
Drawbacks ¡
SLIDE 26 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
Suffix ¡arrays ¡
SLIDE 27 T ¡= ¡mississippi ¡
i ippi issippi ississippi mississippi pi
8 5 2 1 10 9 7 4 11 6 3
ppi sippi sisippi ssippi ssissippi
L R s ¡= ¡issa ¡ M
Example ¡
SLIDE 28
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. ¡
Searching ¡a ¡suffix ¡array ¡
SLIDE 29 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 ¡
Construc8ng ¡a ¡suffix ¡array ¡