Presentation is loading. Please wait.

Presentation is loading. Please wait.

Improved string matching with k mismatches (The Kangaroo Method) Galil, R. Giancarlo SIGACT News, Vol. 17, No. 4, 1986, pp. 52–54 Original: Moshe Lewenstein.

Similar presentations


Presentation on theme: "Improved string matching with k mismatches (The Kangaroo Method) Galil, R. Giancarlo SIGACT News, Vol. 17, No. 4, 1986, pp. 52–54 Original: Moshe Lewenstein."— Presentation transcript:

1 Improved string matching with k mismatches (The Kangaroo Method) Galil, R. Giancarlo SIGACT News, Vol. 17, No. 4, 1986, pp. 52–54 Original: Moshe Lewenstein Modified by: Hsing-Yen Ann Date: Nov. 26, 2004

2 Exact String Matching Input: T = t 1... t n P = p 1 … p m Output: All locations i of T where P appears Example: P = A B C A A B T = A B A B C A A B C A A B C A A B A A …

3 Input: T = t 1... t n P = p 1 … p m Output: All locations i of T where P appears Example: P = A B C A A B T = A B A B C A A B C A A B C A A B A A … 3 Exact String Matching

4 Input: T = t 1... t n P = p 1 … p m Output: All locations i of T where P appears Example: P = A B C A A B T = A B A B C A A B C A A B C A A B A A … 3 7 Exact String Matching

5 Input: T = t 1... t n P = p 1 … p m Output: All locations i of T where P appears Example: P = A B C A A B T = A B A B C A A B C A A B C A A B A A … 3 7 11 Exact String Matching

6 Input: T = t 1... t n P = p 1 … p m Output: All locations i of T where P appears Example: P = A B C A A B T = A B A B C A A B C A A B C A A B A A … Answer: {3,7,11,..} Exact String Matching

7 Approximate String Matching Idea: Find all text locations where distance from pattern is sufficiently small. distance metric:HAMMING DISTANCE Let S = s 1 s 2 … s m R = r 1 r 2 … r m Ham(S,R) = The number of locations j where s j r j Example: S = ABCABC R = ABBAAC Ham(S,R) = 2

8 String Matching with Mismatches Input: T = t 1... t n P = p 1 … p m Output: For each i in T Ham(P, t i t i+1 … t i+m-1 ) Example: P = A B B A A C T = A B C A A B C A C …

9 String Matching with Mismatches Input: T = t 1... t n P = p 1 … p m Output: For each i in T Ham(P, t i t i+1 … t i+m-1 ) Example: P = A B B A A C T = A B C A A B C A C … 2 Ham(P,T 1 ) = 2

10 String Matching with Mismatches Input: T = t 1... t n P = p 1 … p m Output: For each i in T Ham(P, t i t i+1 … t i+m-1 ) Example: P = A B B A A C T = A B C A A B C A C … 2, 4 Ham(P,T 2 ) = 4

11 String Matching with Mismatches Input: T = t 1... t n P = p 1 … p m Output: For each i in T Ham(P, t i t i+1 … t i+m-1 ) Example: P = A B B A A C T = A B C A A B C A C … 2, 4, 6 Ham(P,T 3 ) = 6

12 String Matching with Mismatches Input: T = t 1... t n P = p 1 … p m Output: For each i in T Ham(P, t i t i+1 … t i+m-1 ) Example: P = A B B A A C T = A B C A A B C A C … 2, 4, 6, 2 Ham(P,T 4 ) = 2

13 String Matching with Mismatches Input: T = t 1... t n P = p 1 … p m Output: For each i in T Ham(P, t i t i+1 … t i+m-1 ) Example: P = A B B A A C T = A B C A A B C A C … 2, 4, 6, 2, …

14 Input: T = t 1... t n, P = p 1 … p m String Matching with k Mismatches Output: Every i in T s.t. Ham(P, t i t i+1 … t i+m-1 ) k Example: k = 2 P = A B B A A C T = A B C A A B C A C … 2, 4, 6, 2, …

15 Input: T = t 1... t n, P = p 1 … p m String Matching with k Mismatches Output: Every i in T s.t. Ham(P, t i t i+1 … t i+m-1 ) k Example: k = 2 P = A B B A A C T = A B C A A B C A C … 2, 4, 6, 2, …

16 Input: T = t 1... t n, P = p 1 … p m String Matching with k Mismatches Output: Every i in T s.t. Ham(P, t i t i+1 … t i+m-1 ) k Example: k = 2 P = A B B A A C T = A B C A A B C A C … 2, 4, 6, 2, … Y,N,N,Y, …

17 Naïve Algorithm (for counting mismatches or k-mismatches problem) Running Time: O(nm) n = |T|, m = |P| - Goto each location of text and compute hamming distance of P and T i

18 The Kangaroo Method (for k-mismatches) Landau – Vishkin 1986 Galil – Giancarlo 1986

19 Trie A tree representing a set of strings. a b c e e f d b f e g { aeef ad bbfe bbfg c }

20 Trie (Cont) Assume no string is a prefix of another a b c e e f d b f e g Each string corresponds to a leaf.

21 Compressed Trie Compress unary nodes, label edges by strings a b c e e f d b f e g a bbf c eef d e g 

22 Suffix tree Suffix tree of string s: a compressed trie of all suffixes of s Prefix-free: add a special character, say $, at the end of s

23 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 a b $ a b $ b $ $ $

24 Suffix Tree properties - Succint in space - O(n). - Can be built in O(n) time. McCreight, Weiner, Ukkonen, Farach-Colton b 1 2 a b a b $ a b $ 3 $ 4 $ 5 $

25 Exact string matching 1 2 a b a b $ a b $ b 3 $ 4 $ 5 $ Given a pattern P = ab we traverse the tree according to the pattern. s=abab$

26 Exact string matching 1 2 a b a b $ a b $ b 3 $ 4 $ 5 $ Leaves correspond to locations of appearance! s=abab$ 1 3

27 Exact string matching 1 2 a b a b $ a b $ b 3 $ 4 $ 5 $ Prepare Tree: O(n) time Find matches: O(m + occ) time occ = # of matches s=abab$ 1 3

28 Lowest common ancestors A lot more can be gained from the suffix tree if we preprocess it so that we can answer LCA queries on it

29 Why? The LCA of two leaves represents the longest common prefix (LCP) of these 2 suffixes s = abbaab$ 1 3 a b a a b a b $ b 5 $ 2 b 4 b $ a 6 $ 7 $ b $ a a a b $

30 Why? The LCA of two leaves represents the longest common prefix (LCP) of these 2 suffixes 1 3 a b a a b a b $ b 5 $ 2 b 4 b $ a 6 $ 7 $ b $ a a a b $ s = abbaab$ aab$

31 Why? The LCA of two leaves represents the longest common prefix (LCP) of these 2 suffixes 1 3 a b a a b a b $ b 5 $ 2 b 4 b $ a 6 $ 7 $ b $ a a a b $ s = abbaab$ aab$ abbaab$

32 Why? The LCA of two leaves represents the longest common prefix (LCP) of these 2 suffixes 1 3 a b a a b a b $ b 5 $ 2 b 4 b $ a 6 $ 7 $ b $ a a a b $ s = abbaab$ aab$ abbaab$

33 LCA/LCP properties a 1 3 b a a b a b $ b 5 $ 2 b 4 b $ a 6 $ 7 $ b $ a a a b $ Preprocesssing time : O(n) Query Time: O(1) Harel & Tarjan 1984, Schieber & Vishkin 1988, Berkman & Vishkin 1993

34 The Kangaroo Method (for k-mismatches) - Create suffix tree for: s = P#T$ -Check P at each location i of T by kangrooing Example: P = A B A B A A B A C A B T = A B B A C A B A B A B C A B B C A B C A … i Finding LCP(s, P 0, T i )

35 The Kangaroo Method (for k-mismatches) - Create suffix tree for: s = P#T$ -Check P at each location i of T by kangrooing Example: P = A B A B A A B A C A B T = A B B A C A B A B A B C A B B C A B C A … i Length of LCP(s, P 0, T i ) = 4

36 The Kangaroo Method (for k-mismatches) - Create suffix tree for: s = P#T$ -Check P at each location i of T by kangrooing Example: P = A B A B A A B A C A B T = A B B A C A B A B A B C A B B C A B C A … i Kangrooing distance = LCP(s, P 0, T i ) +1 = 5

37 The Kangaroo Method (for k-mismatches) - Create suffix tree for: s = P#T$ -Check P at each location i of T by kangrooing Example: P = A B A B A A B A C A B T = A B B A C A B A B A B C A B B C A B C A … i Finding LCP(s, P 5, T i+5 )

38 The Kangaroo Method (for k-mismatches) - Create suffix tree for: s = P#T$ -Check P at each location i of T by kangrooing Example: P = A B A B A A B A C A B T = A B B A C A B A B A B C A B B C A B C A … i Length of LCP(s, P 5, T i+5 ) = 2

39 The Kangaroo Method (for k-mismatches) - Create suffix tree for: s = P#T$ -Check P at each location i of T by kangrooing Example: P = A B A B A A B A C A B T = A B B A C A B A B A B C A B B C A B C A … i Kangrooing distance = LCP(s, P 5, T i+5 ) +1 = 3

40 The Kangaroo Method (for k-mismatches) - Create suffix tree for: s = P#T$ -Check P at each location i of T by kangrooing Example: P = A B A B A A B A C A B T = A B B A C A B A B A B C A B B C A B C A … i Finding LCP(s, P 8, T i+8 )

41 The Kangaroo Method (for k-mismatches) - Create suffix tree for: s = P#T$ -Check P at each location i of T by kangrooing Example: P = A B A B A A B A C A B T = A B B A C A B A B A B C A B B C A B C A … i Length of LCP(s, P 8, T i+8 ) = 3

42 The Kangaroo Method (for k-mismatches) - Create suffix tree for: s = P#T$ -Check P at each location i of T by kangrooing Example: P = A B A B A A B A C A B T = A B B A C A B A B A B C A B B C A B C A … i Next iteration: i = i + 1

43 The Kangaroo Method (for k-mismatches) Preprocess: Build suffix tree of both P and T - O(n+m) time LCA preprocessing - O(n+m) time Check P at given text location Kangroo jump till next mismatch - O(k) time Overall time for naïve approach: O(nk)

44 2004/11/22Hsing-Yen Ann Faster Algorithms for Four Different Cases Large alphabet At least 2k different alphabets in pattern P. O(n) Small alphabet At most different alphabets in pattern P. General alphabets - many frequent symbols At least frequent symbols General alphabets - few frequent symbols Less than frequent symbols


Download ppt "Improved string matching with k mismatches (The Kangaroo Method) Galil, R. Giancarlo SIGACT News, Vol. 17, No. 4, 1986, pp. 52–54 Original: Moshe Lewenstein."

Similar presentations


Ads by Google