Special course in Computer Science: Advanced Text Algorithms

Similar documents
Lecture 6: Suffix Trees and Their Construction

Special course in Computer Science: Advanced Text Algorithms

Exact String Matching Part II. Suffix Trees See Gusfield, Chapter 5

An introduction to suffix trees and indexing

Exact Matching Part III: Ukkonen s Algorithm. See Gusfield, Chapter 5 Visualizations from

Lecture 5: Suffix Trees

4. Suffix Trees and Arrays

Data structures for string pattern matching: Suffix trees

Computing the Longest Common Substring with One Mismatch 1

4. Suffix Trees and Arrays

Suffix Trees and its Construction

String Matching. Pedro Ribeiro 2016/2017 DCC/FCUP. Pedro Ribeiro (DCC/FCUP) String Matching 2016/ / 42

Non-context-Free Languages. CS215, Lecture 5 c

Solution to Problem 1 of HW 2. Finding the L1 and L2 edges of the graph used in the UD problem, using a suffix array instead of a suffix tree.

Data Structures and Algorithms Dr. Naveen Garg Department of Computer Science and Engineering Indian Institute of Technology, Delhi.

Suffix links are stored for compact trie nodes only, but we can define and compute them for any position represented by a pair (u, d):

Algorithms Theory. 15 Text Search (2)

Special course in Computer Science: Advanced Text Algorithms

An Efficient Algorithm for Identifying the Most Contributory Substring. Ben Stephenson Department of Computer Science University of Western Ontario

V Advanced Data Structures

Introduction to Suffix Trees

Applications of Suffix Tree

Lower Bound on Comparison-based Sorting

Suffix Tree and Array

EE 368. Weeks 5 (Notes)

Suffix trees and applications. String Algorithms

Disjoint-set data structure: Union-Find. Lecture 20

1 Introduciton. 2 Tries /651: Algorithms CMU, Spring Lecturer: Danny Sleator

How many leaves on the decision tree? There are n! leaves, because every permutation appears at least once.

11/5/09 Comp 590/Comp Fall

V Advanced Data Structures

12/5/17. trees. CS 220: Discrete Structures and their Applications. Trees Chapter 11 in zybooks. rooted trees. rooted trees

Lecture 14: Lower Bounds for Tree Resolution

Bálint Márk Vásárhelyi Suffix Trees and Their Applications

Binary Trees, Binary Search Trees

March 20/2003 Jayakanth Srinivasan,

Let the dynamic table support the operations TABLE-INSERT and TABLE-DELETE It is convenient to use the load factor ( )

Optimization I : Brute force and Greedy strategy

Algorithms Dr. Haim Levkowitz

11/5/13 Comp 555 Fall

17 dicembre Luca Bortolussi SUFFIX TREES. From exact to approximate string matching.

Parallel and Sequential Data Structures and Algorithms Lecture (Spring 2012) Lecture 16 Treaps; Augmented BSTs

Advanced Algorithms: Project

Priority Queues. 1 Introduction. 2 Naïve Implementations. CSci 335 Software Design and Analysis III Chapter 6 Priority Queues. Prof.

Quiz 1 Solutions. (a) f(n) = n g(n) = log n Circle all that apply: f = O(g) f = Θ(g) f = Ω(g)

Computing optimal total vertex covers for trees

Lecture Notes: External Interval Tree. 1 External Interval Tree The Static Version

Lecture 6: Analysis of Algorithms (CS )

Given a text file, or several text files, how do we search for a query string?

Questions from the material presented in this lecture

15.4 Longest common subsequence

BUNDLED SUFFIX TREES

4 Basics of Trees. Petr Hliněný, FI MU Brno 1 FI: MA010: Trees and Forests

Graphs. Part I: Basic algorithms. Laura Toma Algorithms (csci2200), Bowdoin College

Distributed and Paged Suffix Trees for Large Genetic Databases p.1/18

Treaps. 1 Binary Search Trees (BSTs) CSE341T/CSE549T 11/05/2014. Lecture 19

CSCI2100B Data Structures Trees

Exact String Matching. The Knuth-Morris-Pratt Algorithm

Suffix Trees and Arrays

Lecture 4 Feb 21, 2007

15.4 Longest common subsequence

Randomized incremental construction. Trapezoidal decomposition: Special sampling idea: Sample all except one item

Friday Four Square! 4:15PM, Outside Gates

Notes on Binary Dumbbell Trees

M-ary Search Tree. B-Trees. B-Trees. Solution: B-Trees. B-Tree: Example. B-Tree Properties. Maximum branching factor of M Complete tree has height =

MA513: Formal Languages and Automata Theory Topic: Context-free Grammars (CFG) Lecture Number 18 Date: September 12, 2011

Balanced Trees Part Two

SFU CMPT Lecture: Week 9

CE 221 Data Structures and Algorithms

Priority Queues and Heaps. Heaps and Priority Queues 1

Outline. Definition. 2 Height-Balance. 3 Searches. 4 Rotations. 5 Insertion. 6 Deletions. 7 Reference. 1 Every node is either red or black.

CSE 5095 Topics in Big Data Analytics Spring 2014; Homework 1 Solutions

Lecture 7 February 26, 2010

Treewidth and graph minors

9/29/2016. Chapter 4 Trees. Introduction. Terminology. Terminology. Terminology. Terminology

1 Minimum Cut Problem

Introduction. for large input, even access time may be prohibitive we need data structures that exhibit times closer to O(log N) binary search tree

Lecture 9 March 4, 2010

Trapezoidal Maps. Notes taken from CG lecture notes of Mount (pages 60 69) Course page has a copy

M-ary Search Tree. B-Trees. Solution: B-Trees. B-Tree: Example. B-Tree Properties. B-Trees (4.7 in Weiss)

(2,4) Trees. 2/22/2006 (2,4) Trees 1

CSE 417 Dynamic Programming (pt 5) Multiple Inputs

Algorithm Design and Analysis

16 Greedy Algorithms

A Context-Tree Branch-Weighting Algorithm

Figure 1. The Suffix Trie Representing "BANANAS".

Uses for Trees About Trees Binary Trees. Trees. Seth Long. January 31, 2010

Algorithms. Deleting from Red-Black Trees B-Trees

CS 310 B-trees, Page 1. Motives. Large-scale databases are stored in disks/hard drives.

6. Finding Efficient Compressions; Huffman and Hu-Tucker

18.3 Deleting a key from a B-tree

Lecture L16 April 19, 2012

Figure 4.1: The evolution of a rooted tree.

ITEC2620 Introduction to Data Structures

Algorithms and Theory of Computation. Lecture 7: Priority Queue

CS24 Week 8 Lecture 1

CMPSCI 250: Introduction to Computation. Lecture #14: Induction and Recursion (Still More Induction) David Mix Barrington 14 March 2013

Knowledge Discovery from Web Usage Data: Research and Development of Web Access Pattern Tree Based Sequential Pattern Mining Techniques: A Survey

Context-Free Languages and Parse Trees

Chapter 7. Space and Time Tradeoffs. Copyright 2007 Pearson Addison-Wesley. All rights reserved.

Transcription:

Special course in Computer Science: Advanced Text Algorithms Lecture 4: Suffix trees Elena Czeizler and Ion Petre Department of IT, Abo Akademi Computational Biomodelling Laboratory http://www.users.abo.fi/ipetre/textalg

Suffix trees Suffix tree = data structure exposing the internal structure of a string. Suffix trees store the suffixes of a text. Any factor of a string can be extended to a suffix of the text. By storing efficiently the suffixes of a text, we get direct access to all the factors of that text.

Suffix trees Suffix trees have numerous applications, often providing linear-time solutions to challenging string problems, e.g.,: Exact pattern matching The longest common substring The longest repeated substring inside a given text Frequent common substrings of a set of strings, i.e., that occur in a large number of distinct strings Suffix arrays Genome-scale projects 3

Definitions A suffix tree T for a string S[..m] of length m is a directed rooted tree with: exactly m leaves numbered,..,m at least two children for each internal node (with the root as a possible exception) each edge labeled by a nonempty factor of S no two edges out of a node beginning with the same character. Let L(v) denote the label of a node v, i.e., the concatenation, in order, of the strings labelling the edges on the path from the root to the node v. Key feature of suffix trees: L(i)=S[i..m] for each leaf i. 4

Example of a suffix tree For the string xabxac we can associate the following suffix tree: bxac xa a bxac c 6 c bxac c 4 5 3 L(5)=ac, i.e., the suffix starting on position 5. 5

Question: Does a suffix tree always exist? Answer: No! Suffix trees If a suffix of S matches a prefix of another suffix, then we cannot construct a suffix tree obeying all the previous rules. Example: If we cut the last character from the string of the previous example, i.e., we take xabxa,then the suffix xa is also a prefix of the suffix xabxa. So, the path labelled by xa will not end at a leaf. 6

Suffix trees To avoid this problem, assume that the string S has a termination character that occurs only at the end of S (or insert one, if needed) By doing this we make sure that: no suffix can appear as a prefix of another suffix suffixes label complete paths leading from the root of the graph to the leaves 7

First application of suffix trees Suffix trees can be used to solve the exact pattern matching problem:. Construct the suffix tree T for the text[..n] (We ll discuss later how to do this in O(n) time). Match the characters of pat along the unique path from the root: The pattern pat occurs in the text at position j if and only if pat is a prefix of text[j..n] That is, pat labels an initial part of the path from the root of the graph to the leaf labelled by j The matching path is unique since the edges leaving from a node are labelled by strings beginning with different characters. The time to match pat to a path is in O(m), with m= pat 8

First application of suffix trees a) If pat can be fully matched, let k be the number of leaves below the path labelled by pat. Each of these leaves indicates the start position of an occurrence of pat, and they can be collected in time O(k). Traverse the tree starting at the end of the matching path using, e.g., a depth-first approach, and gather all the encountered leaves. b) If pat doesn t match completely, then pat does not occur in the text The total time complexity of the algorithm is O(n+m+k) 9

A naive approach to construct a suffix tree We start with a root and a leaf numbered, connected by an edge labeled S$. We introduce in the graph successively the suffixes S[i..m]$ with i increasing from to m. To insert the suffix K i = S[i..m]$, we follow the path from the root matching the characters of K i until the first mismatch, at character K i [j] (which is bound to happen since no suffix of S$ is a prefix of another suffix) a) If the matching cannot continue from a node, denote that node by w b) Otherwise the mismatch occurs in the middle of an edge, say (u,v), which has to be split 0

A naive approach to construct a suffix tree Suppose the mismatch occurs in the middle of an edge e = (u, v), and let the label of that edge be a...a l If the mismatch occurred on character a k, i.e., a k K i [j], then create a new node w, and replace the edge e by edges (u,w) and (w,v) labeled by a...a k- and a k...a l Finally, in both cases (a) and (b), we create a new leaf numbered i, and connect w to it by an edge labelled by K i [j.. K i ]

Example We want to build the suffix tree associated to the string S=xabxac We start with the graph: When we add the suffixes S[..6]=abxac and S[3..6]=bxac,the mismatches occur in the root of the graph. So, we add nodes and edges: xabxac xabxac abxac bxac 3

Example () Entering S[4..6] = xac causes the first edge to split: xabxac abxac xa bxac c 4 bxac 3 bxac abxac 3 The same happens for the second edge when we introduce S[5..6] = ac in the graph 3

Example (3) After entering also the last suffix S[6]=c we obtain the suffix tree: xa a bxac Introducing a suffix S[i..m]$ in the tree is done in So the total time complexity of the algorithm is c 3 6 bxac c bxac m+ ΘΣ ( i= i) =Θ( m ) c Θ( Sim [.. ]$ ) 4 5 4

Ukkonen s algorithm Ukkonen s algorithm constructs a suffix tree for the string S[..m] in linear time (O(m)). We begin with a high-level description of the method, and then describe how to implement it to run in linear time. 5

Ukkonen s algorithm The method builds, as intermediate results, implicit suffix trees for each prefix S[]; S[..];..; S[..m] The implicit suffix tree of a string S is obtained by constructing the suffix tree for the string S$ and then remove: All copies of the terminal symbol $ from the edge labels All edges that have no labels All nodes having only one child All suffixes are included in an implicit suffix tree, but not necessarily as labels of complete paths leading to the leaves. 6

Example of implicit suffix tree Take the string S=xabxa having the following suffix tree. bxa$ $ 4 xa bxa$ a $ $ 5 bxa xa a bxa 4 5 bxa xa a bxa xabxa abxa bxa$ 6 bxa 6 bxa bxa 3 3 ) Cut all $ from the ) Cut all edges 3) Cut all nodes edge labels without labels having only one child some suffixes occur as labels of incomplete paths (not leading to a leaf, or even to an internal node) 3 3 7

Implicit suffix tree If S ends with a character that occurs also somewhere else in S, then the implicit suffix tree for S has fewer leaves than the suffix tree of S$. If S ends with a character that does not occur anywhere else in S, then the implicit suffix tree is essentially the same as the suffix tree (only without $). xa a bxac c 3 6 bxac c bxac c 4 5 bxac$ xa a c$ 6 $ 3 bxac$ c$ bxac$ 7 c$ 4 5 8

Implicit Suffix Trees of Prefixes Denote by I j the implicit suffix tree of the prefix S[..j] I is just a single edge labeled by S[] leading to leaf Example: The implicit suffix tree for the first prefix of the word axabxb: I : a 9

Implicit Suffix Trees of Prefixes I j contains each suffix S[..j]; S[..j];... ; S[j] of S[..j] as a label of some path (possibly ending in the middle of an edge) Let us call (string) paths the labels of such (partial) paths That is, a (string) path is a string that can be matched along the edges, starting from the root, or equivalently a prefix of any node label 0

Ukkonen s Algorithm on a High Level Given a string S[..m], we start with T := I I : S[] Then update T to trees I,...,I m+ in m phases. S[m+] is the terminal symbol $, so the final value of T is a true suffix tree, which contains all suffixes of S (extended with $) Phase i+ updates T from I i (containing all suffixes of S[..i]) to I i+ (containing all suffixes of S[..i+]) Each phase i+ consists of extensions j =,..,i+; Extension j ensures that suffix S[j..i+] is in I i+

Extension j of Phase i + Phase i+ starts with T = I i, containing all suffixes of S[..i] For each j i+, the algorithm finds the end of the path S[j..i] and extends it (if necessary) to ensure that the suffix S[j..i+] is in the tree. Question: How do we extend the path? After locating the end of the path S[j..i], we have tree possible cases, and we have extension rule for each case:

Suffix Extension Rules Consider phase 6 for the string S[..6]=axabxb; Now T=I 5 contains all suffixes of S[..5] = axabx: Rule : If in the current tree the path S[j..i] ends at a leaf. Then we concatenate S[i+] to its edge label. 4 I 5 : bx a xabx xabx bx 3 4 bx xabx a bx xabxb 3 Extension : we want to locate the end of string S[..5]=axabx and then extend it by S[6] 3

Suffix Extension Rules T=I 5 contains all suffixes of S[..5] = axabx: 4 bx xabx a bx xabxb 3 4 bx xabxb a bx xabxb 3 Rule : If in the current tree the path S[j..i] ends at a leaf. Then we concatenate S[i+] to its edge label. Extension : we want to locate the end of string S[..5]=xabx and then extend it by S[6]=b 4

T=I 5 contains all suffixes of S[..5] = axabx: 4 bx xabxb a bx xabxb 3 4 bxb xabxb a bxb xabxb 3 Suffix Extension Rules Rule : If in the current tree the path S[j..i] ends at a leaf. Then we concatenate S[i+] to its edge label. Extensions 3,4 (for the strings S[3..5]=abx, S[4..5] =bx) are done similarly using Rule. 5

Suffix Extension Rules Extension 5: locate the end of S[5]=x and then extend it by S[6]=b, 4 bxb xabxb a bxb xabxb 3 4 bxb x abxb a b 5 bxb xabxb 3 create a node at the end of S[5] and a leaf labeled 5 create an edge from the new node to the leaf labeled by S[6]=b Rule : If no path from the end of S[j..i] starts with character S[i+], but there is at least one labeled path from the end of S[j..i]. Then, we create a new leaf labeled j, and an edge starting from the end of S[j..i] to the new leaf labeled by S[i+]. If the path ended at the middle of an edge, we split the edge and insert a new node as a parent of leaf j. 6

Suffix Extension Rules Extension 6: we locate the end of S[6..5]=ε and then we extend it by S[6]=b 4 bxb x abxb I 6 : a b 5 bxb xabxb 3 Rule 3: Some path from the end of S[j..i] starts with S[i+]. Then, we do nothing since the suffix S[j..i+] is already in the tree. 7

Algorithm complexity The key issue in implementing Ukkonen s algorithm is locating the end of all the i+ suffixes of S[..i]. Once the end of such a suffix is located, the execution of any of the extension rules takes constant time. So, applying them once in each extension takes time Locating the ends of the paths S[..i];..; S[i+..i] by i traversing them explicitly takes time ΘΣ ( l= 0l) =Θ( i ) So, the total time for all phases i=;..;m + is m+ 3 ΘΣ ( l= i ) =Θ( m ) Θ() i 8

How to reduce this complexity? Reducing the Complexity To get total time down to Θ(m ) we need to avoid or speed up path traversals. Spending even constant time for each extension m+ requires time ΘΣ ( i= i) =Θ( m ) To get total time down to Θ(m) we also need to avoid performing some extensions at all. We will consider first speeding up the path traversals. 9

Locating Ends of Paths The extensions of phase i+ need to locate the ends of all the i+ suffixes of S[..i] How to do this efficiently? Let xα denote an arbitrary string, where x is just a character while α denotes a (possibly empty) string. For an internal node v of T labeled by xα, if there is another node s(v) labeled by α, then a pointer from v to s(v) is called a suffix link. NB: If node v is labeled by a single character (x), then α=ε and s(v) is the root 30

Example of Suffix Links In the graph below we have suffix links one from the node labeled xa to the one labeled a one from the node labeled a to the root xa a bxac c 3 6 bxac The root is not considered internal node so we do not have suffix links leaving from the root. c bxac c 4 5 What are suffix links good for? 3

Intuitive Motivation for Suffix Links Extension j (of phase i + ) finds the end of the path S[j..i] in the tree (and extends it with char S[i + ]) Extension j+ similarly finds the end of the path S[j+..i] Let v be an internal node labeled by S[j]α on the path S[j..i]. Then we can avoid traversing path α when locating the end of path S[j+..i], by starting from node s(v) we know that if we have a suffix link from v to s(v), then s(v) is the node labeled by α Do suffix links always exist? Yes, and each suffix link (v,s(v)) is easy to set. 3

Computation of Suffix Links Observation: If an internal node v, labeled by xα,is created during extension j (of phase i+), then extension j+ will identify the corresponding node s(v) An internal node v can only be created by extension rule. That is, v is inserted at the end of path S[j..i], which continued by some character c S[i+]. In extension j+, there is a path labeled α which certainly continues with character c, maybe also other characters. If path α continues only with c, then extension rule creates node s(v) at the end of path α. Otherwise, i.e., the path α continues with at least different characters, then node s(v) already exists in the tree 33

Computation of Suffix Links In Ukkonen s algorithm any newly created internal node will have a suffix link from it by the end of the next extension. This is true for I, since I does not have internal nodes Suppose it is true through the end of phase i, and consider phase i+ If a node v is created during extension j, the correct node s(v) ending the suffix link from v is either found or created during extension j+ (see the previous Observation) No new internal node is created in the last extension of a phase Thus, for all internal nodes created during phase i+, the suffix links starting from them are known by the end of this phase 34

Speeding up Path Traversals Consider the extensions of phase i+: Locate the suffixes S[j..i] with j from to i+ Extend them with character S[i+] Extension extends path S[..i] with char S[i+] Path S[..i] always ends at leaf, and is thus extended by Rule We can perform extension in constant time, if we maintain a pointer to the node at the end of S[..i] 35

Locating Subsequent Paths Let S[..i]=xα, where x is just a char, and α is a string Extension : find the end of S[..i]=α Let (v,) be the tree-edge entering leaf, labeled by γ If v is the root, then, to find the end of α, we just walk down the tree following the path labeled α Otherwise, v is an internal node, and we know it has a suffix link to a node s(v) (see the earlier Observation) v γ 36 s(v)

Locating Subsequent Paths Since the label of node v is a prefix of xα,the label of s(v) is a prefix of α α=βγ, S[..i]=xα=xβγ So, the end of the string α is somewhere in the subtree s(v) To find the end of α,we do not need to walk the entire path from the root To find the end of α we need to: Walk up from leaf to node v Follow the suffix link to node s(v) Walk from the node s(v) down the path labeled by γ γ xα xβ β v s(v) The green line depicts the suffix link from v to s(v) The orange lines only depict the string labeling of paths (they are neither tree-edges, nor suffix links) 37

Locating Subsequent Paths What about subsequent extensions j (for j=;..;i)? Extension j- has located the end of the path S[j-..i] Starting from there, walk up at most one node either (a) to the root, or (b) to an internal node v with a suffix link In case (a), traverse path S[j..i] explicitly down-wards from the root 38

Short-cutting Traversals In case (b) let xα be the label of v and let γ be such that S[j-..i]=xαγ. xα α Then follow the suffix link of v, and continue by matching γ down-wards from node s(v) (which is now labeled by α) γ v s(v) Having found the end of path αγ=s[j+..i], apply extension rules to ensure that it extends with S[i+] Finally, if a new internal node w was created in extension j, set its suffix link to point to the end node of path S[j+..i] 39

Speeding up Explicit Traversals Using suffix links is definitely an improvement compared with walking-down from the root in each extension. Using only the suffix links is not enough to improve the time bound. We introduce next a trick that will reduce the worstcase complexity to O(m ). 40

Skip/count Trick In extension j: Starting from the end of the string S[j-..i] go up at most one edge, labeled by γ, to a node v If v is internal node, then it has a suffix link to a node s(v) Follow the suffix link from v to s(v) Walk down from s(v) along a path labeled by γ Let k= γ. All edges leaving from s(v) start with distinct characters. So, the first character of γ occurs as the first character on exactly one edge out of s(v); let k denote the length of its label. 4

k= γ and k =the length of the edge leaving from s(v) and starting with the first letter of γ Example: k=0, k = If k <k, the algorithm simply skips to the node at the end of that edge, and continues in a similar way to find the next edge. If k k, then the algorithm skips to the character k on the edge and stops, since the path labeled by γ ends inside that edge exactly k characters down its label. Skip/count Trick v z a c b e d f γ g h y α end of suffix j- x w α end of suffix j s(v) z a b c γ d e f g h y 4

Bounding the Time of Tree Traversals Thus, the time needed to traverse a path is proportional to the node-length of the path (instead of its string-length). Denote by depth(v) the node-depth of v, i.e., the number of nodes on the path from the root to v. Following a suffix link leads at most one level closer to the root. For any node v with a suffix link to s(v), we have that depth(s(v)) depth(v)- 43

Linear Bound for any Single Phase Theorem: Using suffix links and the skip/count trick, a single phase takes time O(m). Proof: There are i+ m extensions in phase i+ In a single extension the algorithm does the following: first moves at most one level up. then a suffix link may be followed, then it walks down a number of nodes, applies an extension rule maybe adds a suffix link In any extension, all other operations except tree-traversals take constant time. 44

Linear Bound for any Single Phase So, we only need to analyze the time for the down-walks. We do this by investigating how the current node-depth changes through the phase. In an extension, the possible up movement and suffix link traversal decrement current node depth at most twice. So, the current node depth is decremented at most m times during the entire phase. On the other hand, the current node depth cannot exceed m. So, the node-depth is incremented (by following downward edges) at most 3m times over the entire phase. Using the skip/count trick, the time per down-edge traversal is constant. Thus, the total time of a phase is O(m). 45

The total time of a phase is O(m). Algorithm complexity Since there are m phases, the total time of the algorithm is O(m ). A few more tricks are needed to get total time linear 46