


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!



Building a suffix tree is an important result of 1973, which builds suffix tree in O(n) time for a string S with n symbols. Suffix tree is leaf-labeled by suffix number at leaves. Edges are labeled with substrings of S. A key property is, from root to leaf, the substrings along the path spell out the suffix on that leaf. Also, no two edges out a node have the same starting symbol. Note that every substring of S can be found on the tree, starting from the root. Now try to build suffix yourself for S = tartar$ and get the first hand taste of what a suffix tree is. Why is suffix tree useful? Suppose we want to find whether pattern P = tar is in S=tartar. We first build a ST. Then search for tar in it. How to proceed? We just match P with the unique path that agree with P. The first thing to note the path is indeed unique: that is, at each node, we will have at most one branch to follow. This is because in ST, each branch starts with different char. If we can not continue matching (while still something left in P), we conclude it is not in. Otherwise, ALL leaves under the current ending position has an exact match of P. Why? The crucial point is that if pattern P starting in T[j], iff suffix T[j..m] starts with P. Thus all positions of T matching P must have the same T. When alphabet size is constant, the search for path to go can be done in constant time. Thus, pattern matching can be done in linear time. Linear-time pattern matching has been around for over 30 years. But the algorithms known till very recently are too complicated, even after much efforts in trying to simplify. Fortunately, something called suffix array comes to rescue. Initially proposed by Manber and Myers as an space- efficient alternative to suffix trees, suffix array has some very interesting results. In particular, it has very simple linear-time algorithm to construct directly, and suffix array is also easily converted to suffix tree. Therefore, nowadays, suffix array is used more often than suffix tree in pattern matching. Suffix tree, however, is still useful in term of getting intuition: you can think in term of suffix tree, and implementation is done by suffix array. So what is: suffix array introduced by Manber and Myers? We start with the same string tartar$ POS is an array of suffix; each cell stores a suffix, and listed in lexicographic order regarding to the corresponding suffix. Here, POS = [ 7, 5, 2, 6, 3, 4, 1]. A main advantage of suffix array is the memory reduction. But I still think suffix tree is more conceptually clearer when thinking of things. First, suffix array can be easily built from suffix tree: just a depth-first search in a lexicographically (well, when the alphabet is constant size). How to build suffix tree from array? We need one additional piece of data: LCP array (also called depth array). The LCP array stores the length of the longest common string for two suffix, next to each other in POS. For our example here S = tartar$: LCP = [0, 0, 2, 0, 1, 0, 3]. Here is how you will generate suffix tree from POS and LCP arrays. Essentially, start from smallest (first) suffix, and when move to the next, create a new branch on the tree, from the current
tree. Where to branch out? Well, depends on the depth array. From the last added suffix si (leaf), backup till depth of LCP(i). O(1) per edge when backing up. Create a new internal node if needed and then branching off an edge. Question: will this violate the property of suffix tree by creating an edge whose label has the same start symbol as another existing edge? Convince yourself it will not. Now how about running time? The main concern is that insertion of new branches leads to traversal of (sometimes multiple) edges. How to bound such traversal? The key is that only the path leading to the rightmost suffix is traversed. Once traversed, it is no longer part of the new rightmost path, and so it will not be traversed again. All other operations are only a single action per suffix. So entire conversion takes O(n). How can do pattern matching in suffix array? The simplest thing is to do binary search. This gives O(nlog(m)) time where m is the pattern size. But there are more advanced techniques, which gives O(n+log(m)). We omit such details. See Gusfield’s notes on LCP arrays, which explain how to build the LCP array efficiently. We now focus on the problem of building the POS array in O(n) time. This problem has puzzled people for ten years, and three groups of people solved it (a little different methods) in 2003. All the three methods use the same high-level idea: that is, first sort part of string (called a sample), and then sort the rest of string from the sorted sample. The three methods differ by the way of choosing samples and how to merge. The main idea is divide and conquer. But how are we going to divide? One may say do first half and second half, but this does not seem to do much. Another is to say odd position or even position. Why is this useful? Sorting suffix is different from sorting a list of numbers. suffix i and i+1 is not independent (unlike sorting an array of numbers)! Look at two positions: i and j. If we know which one is (lexicographically) bigger, then we can decide in constant time which of suffix i+1, j+1 bigger? Why? Compare the first char, and if equal then (i+1,j+1) pair tells us the result. This is roughly the high-level idea this approach takes, but with an important twist: instead of doing odd-even, we do tri-partition: 0,1,2 mod 3. Let us give an example to see how this works. Here we start numbering position from 0. As an example, consider T= y a b b a d a b b a d o. For this string, POS=[12, 1, 6, 4, 9, 3, 8, 2, 7, 5, 10, 11, 0]. We list suffix for 1 mod 3 (in an array called called B 1 ), and list suffix for 2 mod 3 (in an array called called B 2 ). For this example, B 1 =[1,4,7,10], B 2 = [2,5,8,11].
For this purpose, we will sort this string: [abb][ada][bba][do$] | [bba][dab][bad][o$$] Note each triple (encapsulated in []) becomes a new character. This string is longer than the original string, but we just replace the triple with the ranks of these triples in the matrix by their ranks. This is done by radix sort of the letters present in the samples. This turns to: abb 1 ada 2 bba 4 do$ 6 bba 4 dab 5 bad 3 o$$ 7
So we have the new string (by plugging in the ranks) as R’= [1,2,4,6,4,5,3,7]. Then we sort this recursively (and note R’ is shorter than T by 1/3). And suppose we get SA(R’) = [ 8, 0, 1, 6, 4,
We now take a closer look at the binary search. Note that to really reduce the running time of search, it is critical to reduce the number of redundant character comparison. Here, redundant comparison means a symbol in P is compared multiple time. Recall that this is not the case in suffix tree search, where we only match each symbol in P once. In the above speedup trick, we avoided mlr redundant search, but the comparison from mlr to max(l,r) is still redundant. To eliminate these redundant comparison, we need to find a way to start searching from max(l,r) instead. This needs the LCP array. Recall that the LCP array stores the length of the longest common prefix of two adjacent suffix in POS. For the purpose of matching, we need an extension of LCP. For any two positions i and j, we define LCE(i,j) as the length of longest common prefix of suffix POS(i) and POS(j). Note that this means LCP(i) = LCE(i-1, i) when i ≥ 2. In the following, we will assume the LCE values for needed pairs of positions are known. We will address the issue of how to obtain LCE values later. Now consider several cases. First, if l=r, there is no extra redundancy, and so start matching from mlr + l. (Make sure you see why there is no redundancy.) We now assume l > r. (Note the case l < r is similar.) That is, the left suffix matches more of P than the right suffix. We then consider three sub-cases.
It is now clear that with the help of LCE values, we can get rid of redundant character com- parison since the above shows we can start comparing at max(l,r) instead of min(l,r). If you are still not convinced, you should check closely to see how values l, r change, where comparisons occur and so on. The only remaining issue is on LCE values. There are two concerns: first, there may be too many LCE values we need; second, computing LCE values may be slow (not linear time in the length of T). Fortunately, it turns out here are only O(m) LCE pairs needed, and compute these LCE values takes only O(m) time. So we can just do that in the pre-processing stage and these LCE values can be used in later pattern matching. To summarize, pattern matching in suffix array can be done in O(n+log(m)) time (after some additional pre-processing, which takes O(m) time).