Witness Tables Tsvi Kopelowitz Modified by Orgad Keller
Witness Tables Tsvi Kopelowitz Modified by Orgad Keller 1
Patten Matching • Input: A (large) text and a (smaller) pattern . • Output: All locations where pattern matches text, i. e. all locations where • In the lecture you will see an algorithm using a Witness Table. 2
Witness Tables • A witness table is an array W of size m • W[i]= – * if p 1 p 2…pm-i+1= pipi+1…pm – k if p 1 p 2…pk-i= pipi+1…pk-1 and pk pk-i+1 – Example: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 P a b c a b d b a b c a b b a b c W * 15 18 2 3 6 5 8 7 8 9 15 11 12 14 15 * 17 3
Example: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 P: a b c a b a b d b a b c a b b a b c 1 W: 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 * 236 5 8 78 1 2 3 4 5 6 7 8 17 18 P a b c a b d b a b c a b b a b c W * 15 18 2 3 6 5 8 7 8 9 15 11 12 14 15 * 17 4
Another Example 5
Construction • Naïve: O(m 2) • Idea: when calculating W[i], use W[1]…W[i-1] • Let W[k] be the maximum of {W[2], W[3], …, W[i-1]} 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 P a b c a b d b a b c a b b a b c W * 15 18 2 3 6 5 8 7 8 9 15 11 12 14 15 * 17 • What is W[k] for i=5? i=10? i=12? i=18? • What is k for i=5? i=10? i=12? i=18? 6
A Few Notes • W[1] isn’t interesting. • If i>1, and W[i]=*, then we can treat W[i] as being m+1. 7
Construction i i-k W[k] We want to We already compared these. compare these. Also already compared these. k-1 i-k • So we compared alignment 1 and alignment k, but we will also want to compare alignments k and i. 8
Three (+1) Cases • Case 0 (? ): i>W[k] i k-1 i-1 • Compare from i. 9
Three Cases • Case 1: W[i-k+1]+k-1<W[k] W[i-k+1] i W[i] W[i-k+1]+k-1 W[k] i-k k-1 i-k • We found the first mismatch. • W[i]=W[i-k+1]+k-1 iff W[i-k+1]+k-1<W[k] 10
Three Cases • Case 2: W[i-k+1]+k-1>W[k] i W[i-k+1] W[k] i-k k-1 i-1 • W[i]=W[k]! 11
Three Cases • Case 3: W[i-k+1]+k-1=W[k] W[i-k+1] i i-k W[k] We have already “scanned” the pattern till W[k] W[i] ? k-1 i-1 ? ? = ? • If W[i-k+1]+k-1=W[k] compare from W[k] • Total of O(m) construction time. 12
Example: a b c a b a b d b a b c a b b a b c a b c a b d b a b c a b b a W[i-k+1]+k-1=W[5 -4+1]+4 -1=W[2]+3=5<6 a b c a b d b a b c a b b W[i-k+1]+k-1=W[6 -4+1]+4 -1=W[3]+3=6=6=W[k] a b c a b d b a b c a b W: * 2 3 6 5 8 7 8 k c b c a b c b a b c 13
Complexity • We “scan” the pattern only once and have additional O(1) work per alignment. • Thus, O(m) total construction time. 14
Circular Shifted String • T 15
Detection Algorithm • T T T 16
Detection Algorithm • T is circualr shifted if and only if there exists cell with W value with distance at least |T|. 17
- Slides: 17