{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

kmp-bm

# kmp-bm - More Exact Matching(Following Guseld Chapter 2...

This preview shows pages 1–7. Sign up to view the full content.

More Exact Matching (Following Gusfield Chapter 2)

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Knuth-Morris-Pratt
Knuth-Morris-Pratt (KMP) Shift by more than 1 place, if possible, upon mismatch. Def. spm i ( P ) = the length of the longest substring of P that ends at i > 1 and matches a prefix of P and such that P [ i +1] P [ spm i + 1]. (“ spm ” stands for suffix, prefix, mismatch.) i P : spm i spm i x y P [ i+ 1] x T : P : spm i spm i y can shift by: i - spm i KMP Algorithm: Suppose mismatch at i +1 of P : a

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
KMP p x T : P : spm p-1 spm p-1 y can set new p to spm p -1 +1 c = p = 1 // ptrs into T and P, respectively while c |T| - |P| + p: while P[p] = T[c] and p n: // compare P and T p++ c++ if p = n + 1: print “Found at”, c - n // if found if p = 1: // failure at start means inc c c++ else : p = spm p-1 + 1 // “shift” by n - spm p-1 (even if p=n+1) c new p
KMP Running Time Pseudocode runs in O(| T |) time (making at most 2| T | comparisons): In each iteration of the outer while loop, at most one character is compared that was compared in a previous iteration. Total comparisons: | T | + s , where s = # of times through the outer while loop. s |T| since P is shifted by 1 each time. Therefore: O( |T| ) for the pseudocode on previous page.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Recall: Fundamental Preprocessing P = “a a rdv a rk”: Z 2 = 1, Z 6 = 1 P = “alf alfa ”: Z 4 = 4 P = “photo
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}