140 likes | 211 Views
Learn about the Witness Tables algorithm for pattern matching in texts. Witness Tables are arrays used to find matching patterns efficiently. See examples and construction methods in this lecture.
E N D
Witness Tables Tsvi Kopelowitz Modified by Orgad Keller
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.
Witness Tables • A witness table is an array W of size m • W[i]= • * if p1p2…pm-i+1= pipi+1…pm • k if p1p2…pk-i= pipi+1…pk-1 and pk pk-i+1 • Example:
Example: P: * 7 8 3 8 6 5 2 W:
Construction • Naïve: O(m2) • 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]} • 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?
A Few Notes • W[1] isn’t interesting. • If i>1, and W[i]=*, then we can treat W[i] as being m+1.
Construction • So we compared alignment 1 and alignment k, but we will also want to compare alignments k and i. We already compared these. We want to compare these. W[k] i i-k Also already compared these. k-1 i-k i-1
Three (+1) Cases • Case 0 (?): i>W[k] • Compare from i. W[k] i k-1 i-1
Three Cases • Case 1: W[i-k+1]+k-1<W[k] • We found the first mismatch. • W[i]=W[i-k+1]+k-1 iff W[i-k+1]+k-1<W[k] W[i] W[i-k+1] W[k] i W[i-k+1]+k-1 i-k k-1 i-k i-1
Three Cases • Case 2: W[i-k+1]+k-1>W[k] • W[i]=W[k]! W[i-k+1] W[k] i i-k k-1 i-1
? ? = Three Cases We have already “scanned” the pattern till W[k] • Case 3: W[i-k+1]+k-1=W[k] • If W[i-k+1]+k-1=W[k] compare from W[k] • Total of O(m) construction time. W[k] W[i] W[i-k+1] i i-k ? k-1 i-1 ?
Example: k W[i-k+1]+k-1=W[5-4+1]+4-1=W[2]+3=5<6 W[i-k+1]+k-1=W[6-4+1]+4-1=W[3]+3=6=6=W[k] W: