Presentation is loading. Please wait.

Presentation is loading. Please wait.

Suffix trees.

Similar presentations


Presentation on theme: "Suffix trees."— Presentation transcript:

1 Suffix trees

2 Trie A tree representing a set of strings. c { a aeef b ad bbfe bbfg e

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

4 Compressed Trie Compress unary nodes, label edges by strings c c a a b
c a a b e b d bbf d eef e f c f c 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 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$ } a b b $ a a $ b b $ $

7 Trivial algorithm to build a Suffix tree
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 b 5 $ a a $ b 4 b $ $ 3 2 1

12 Analysis Takes O(n2) time to build. You can do 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 $

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 when 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 b 5 $ a a $ b 4 b $ $ 3 2 1 Given a pattern P = ab we traverse the tree according to the pattern.

18 By traversing this subtree we get all k occurrences in O(n+k) time
$ a b b 5 $ 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(n+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 5 4 # $ a b a b 3 b $ 4 # a # $ 2 b 1 $ 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 maximal common substring and vice versa. # $ a b 5 4 # $ a b a b 3 b 4 # $ a # $ 2 b 1 Find such node with largest “string depth” $ 3 2 1

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 5 4 # $ a b a b 3 b 4 # $ a # $ 2 b 1 $ 3 2 1

25 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 m-i+1 of sr

26 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 m-i+1 of sr

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

28 Analysis O(n) time to identify all palindromes

29 Can we construct a suffix tree in linear time ?

30 Ukkonen’s linear time construction
ACTAATC A A 1

31 ACTAATC AC A 1

32 ACTAATC AC AC 1

33 ACTAATC AC AC C 1 2

34 ACTAATC ACT AC C 1 2

35 ACTAATC ACT ACT C 1 2

36 ACTAATC ACT ACT CT 1 2

37 ACTAATC ACT ACT CT T 1 2 3

38 ACTAATC ACTA ACT CT T 1 2 3

39 ACTAATC ACTA ACTA CT T 1 2 3

40 ACTAATC ACTA ACTA CTA T 1 2 3

41 ACTAATC ACTA ACTA CTA TA 1 2 3

42 ACTAATC ACTAA ACTA CTA TA 1 2 3

43 ACTAATC ACTAA ACTAA CTA TA 1 2 3

44 ACTAATC ACTAA ACTAA TA CTAA 1 2 3

45 ACTAATC ACTAA ACTAA TAA CTAA 1 2 3

46 ACTAATC ACTAA A TAA CTAA 2 3 A CTAA 4 1

47 Phases & extensions Phase i is when we add character i
In phase i we have i extensions of suffixes

48 ACTAATC ACTAAT A TAA CTAA 2 3 A CTAA 4 1

49 ACTAATC ACTAAT A TAA CTAA 2 3 A CTAAT 4 1

50 ACTAATC ACTAAT A TAA CTAAT 2 3 A CTAAT 4 1

51 ACTAATC ACTAAT A TAAT CTAAT 2 3 A CTAAT 4 1

52 ACTAATC ACTAAT A TAAT CTAAT 2 3 AT CTAAT 4 1

53 ACTAATC ACTAAT A TAAT CTAAT 2 3 AT T CTAAT 4 1 5

54 Extension rules Rule 1: The suffix ends at a leaf, you add a character on the edge entering the leaf Rule 2: The suffix ended internally and the extended suffix does not exist, you add a leaf and possibly an internal node Rule 3: The suffix exists and the extended suffix exists, you do nothing

55 ACTAATC ACTAATC A TAAT CTAAT 2 3 AT T CTAAT 4 1 5

56 ACTAATC ACTAATC A TAAT CTAAT 2 3 AT T CTAATC 4 1 5

57 ACTAATC ACTAATC A TAAT CTAATC 2 3 AT T CTAATC 4 1 5

58 ACTAATC ACTAATC A TAATC CTAATC 2 3 AT T CTAATC 4 1 5

59 ACTAATC ACTAATC A TAATC CTAATC 2 3 ATC T CTAATC 4 1 5

60 ACTAATC ACTAATC A TAATC CTAATC 2 3 ATC TC CTAATC 4 1 5

61 ACTAATC ACTAATC A T CTAATC 2 ATC TC C AATC CTAATC 4 1 5 3 6

62 Skip forward.. ACTAATCAC 4 2 7 3 6 1 5 A T C AC TCAC ATCAC CAC TAATCAC

63 ACTAATCACT 4 7 3 6 1 5 2 A T C AC TCAC ATCAC CAC TAATCAC CTAATCAC

64 ACTAATCACT 4 7 3 6 1 5 2 A T C AC TCAC ATCAC CAC TAATCAC CTAATCACT

65 ACTAATCACT 4 7 3 6 1 5 2 A T C AC TCAC ATCAC CAC TAATCACT CTAATCACT

66 ACTAATCACT 4 7 3 6 1 5 2 A T C AC TCAC ATCAC CAC TAATCACT CTAATCACT

67 ACTAATCACT 4 7 3 6 1 5 2 A T C AC TCAC ATCACT CAC TAATCACT CTAATCACT

68 ACTAATCACT 4 7 3 6 1 5 2 A T C AC TCACT ATCACT CAC TAATCACT CTAATCACT

69 ACTAATCACT 4 7 3 6 1 5 2 A T C AC TCACT ATCACT CACT TAATCACT CTAATCACT

70 ACTAATCACT 4 7 3 6 1 5 2 A T C ACT TCACT ATCACT CACT TAATCACT

71 ACTAATCACTG 4 7 3 6 1 5 2 A T C ACT TCACT ATCACT CACT TAATCACT

72 ACTAATCACTG 4 7 3 6 1 5 2 A T C ACT TCACT ATCACT CACT TAATCACT

73 ACTAATCACTG 4 7 3 6 1 5 2 A T C ACT TCACT ATCACT CACT TAATCACTG

74 ACTAATCACTG 4 7 3 6 1 5 2 A T C ACT TCACT ATCACT CACT TAATCACTG

75 ACTAATCACTG 4 7 3 6 1 5 2 A T C ACT TCACT ATCACTG CACT TAATCACTG

76 ACTAATCACTG 4 7 3 6 1 5 2 A T C ACT TCACTG ATCACTG CACT TAATCACTG

77 ACTAATCACTG 4 7 3 6 1 5 2 A T C ACT TCACTG ATCACTG CACTG TAATCACTG

78 ACTAATCACTG 4 7 3 6 1 5 2 A T C ACTG TCACTG ATCACTG CACTG TAATCACTG

79 ACTAATCACTG 4 2 7 3 6 5 1 8 A T C ACTG TCACTG ATCACTG CACTG TAATCACTG

80 ACTAATCACTG 4 7 3 6 5 1 8 2 9 A T C ACTG TCACTG ATCACTG CACTG T CT

81 ACTAATCACTG 11 4 7 3 6 10 5 1 8 2 9 A T G C ACTG TCACTG ATCACTG CACTG

82 Observations i At the first extension we must end at a leaf because no longer suffix exists (rule 1) i At the second extension we still most likely to end at a leaf. We will not end at a leaf only if the second suffix is a prefix of the first

83 Say at some extension we do not end at a leaf
Then this suffix is a prefix of some other suffix (suffixes) We will not end at a leaf in subsequent extensions Is there a way to continue using ith character ? (Is it a prefix of a suffix where the next character is the ith character ?) Rule 3 Rule 2

84 Rule 3 Rule 2 If we apply rule 3 then in all subsequent extensions we will apply rule 3 Otherwise we keep applying rule 2 until in some subsequent extension we will apply rule 3 Rule 3

85 In terms of the rules that we apply a phase looks like:
We have nothing to do when applying rule 3, so once rule 3 happens we can stop We don’t really do anything significant when we apply rule 1 (the structure of the tree does not change)

86 Representation We do not really store a substring with each edge, but rather pointers into the starting position and ending position of the substring in the text With this representaion we do not really have to do anything when rule 1 applies

87 How do phases relate to each other
i The next phase we must have: /3 So we start the phase with the extension that was the first where we applied rule 3 in the previous phase

88 Suffix Links ACTAATCACTG 11 4 7 3 6 10 5 1 8 2 9 A T G C ACTG TCACTG

89 ACTAATCACTG 11 4 7 3 6 10 5 1 8 2 9 A T G C ACTG TCACTG ATCACTG CACTG

90 ACTAATCACTG 11 4 7 3 6 10 5 1 8 2 9 A T G C ACTG TCACTG ATCACTG CACTG

91 ACTAATCACTG 11 4 7 3 6 10 5 1 8 2 9 A T G C ACTG TCACTG ATCACTG CACTG

92 ACTAATCACTG 11 4 7 3 6 10 5 1 8 2 9 A T G C ACTG TCACTG ATCACTG CACTG

93 Suffix Links From an internal node that corresponds to the string aβ to the internal node that corresponds to β (if there is such node) β

94 Is there such a node ? β aβ x.. y
Suppose we create v applying rule 2. Then there was a suffix aβx… and now we add aβy β x.. v y So there was a suffix βx…

95 Is there such a node ? β aβ x.. z.. x.. y
Suppose we create v applying rule 2. Then there was a suffix aβx… and now we add aβy β x.. z.. x.. v y So there was a suffix βx… If there was also a suffix βz… Then a node corresponding to β is there

96 Is there such a node ? β aβ x.. y x.. y
Suppose we create v applying rule 2. Then there was a suffix aβx… and now we add aβy β x.. y x.. v y So there was a suffix βx… If there was also a suffix βz… Then a node corresponding to β is there Otherwise it will be created in the next extension when we add βy

97 You are at the (internal) node corresponding to the last extension
Inv: All suffix links are there except (possibly) of the last internal node added You are at the (internal) node corresponding to the last extension i i Remember: we apply rule 2 You start a phase at the last internal node of the first extension in which you applied rule 3 in the previous iteration

98 1) Go up one node (if needed) to find a suffix link
2) Traverse the suffix link 3) If you went up in step 1 along an edge that was labeled δ then go down consuming a string δ

99 β δ δ x.. v y Create the new internal node if necessary

100 β δ δ x.. v y Create the new internal node if necessary

101 β δ δ y x.. v y Create the new internal node if necessary, add the suffix

102 β δ δ y x.. v y Create the new internal node if necessary, add the suffix and install a suffix link if necessary

103 Analysis Handling all extensions of rule 1 and all extensions of rule 3 per phase take O(1) time  O(n) total How many times do we carry out rule 2 in all phases ? O(n) Does each application of rule 2 takes constant time ? No ! (going up and traversing the suffix link takes constant time, but then we go down possibly on many edges..)

104 β δ δ y x.. v y

105 So why is it a linear time algorithm ?
How much can the depth change when we traverse a suffix link ? It can decrease by at most 1

106 Punch line Each time we go up or traverse a suffix link the depth decreases by at most 1 When starting the depth is 0, final depth is at most n So during all applications of rule 2 together we cannot go down more than 3n times THM: The running time of Ukkonen’s algorithm is O(n)

107 Another application Suppose we have a pattern P and a text T and we want to find for each position of T the length of the longest substring of P that matches there. How would you do that in O(n) time ?

108

109 Drawbacks of suffix trees
Suffix trees consume a lot of space It is O(n) but the constant is quite big Notice that if we indeed want to traverse an edge in O(1) time then we need an array of ptrs. of size |Σ| in each node

110 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 3 1 4 2

111 How do we build it ? Build a suffix tree
Traverse the tree in DFS, lexicographically picking edges outgoing from each node and fill the suffix array. O(n) time

112 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

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

114 How do we accelerate the search ?
Maintain l = LCP(P,L) l L Maintain r = LCP(P,R) If l = r then start comparing M to P at l + 1 M R r

115 How do we accelerate the search ?
If l > r then Suppose we know LCP(L,M) If LCP(L,M) < l we go left If LCP(L,M) > l we go right If LCP(L,M) = l we start comparing at l + 1 M R r

116 Analysis of the acceleration
If we do more than a single comparison in an iteration then max(l, r ) grows by 1 for each comparison  O(logn + m) time


Download ppt "Suffix trees."

Similar presentations


Ads by Google