Text processing Pattern Matching

Procedure RABIN-KARP-MATCHER(T,P,d,q) Input : Text T, pattern P, radix d ( which is typically =), and the prime q. Output : valid shifts s where P matches n  length[T]; m  length[P]; h  dm-1 mod q; p  0; t0  0; for i  1 to m do { p  (dp + P[i] mod q; t0  (dt0 +T[i] mod q; } for s  0 to n-m do if (p = ts ) if (P[1.m] = T[s+1.s+m]) “pattern occurs with shift ‘s’ else if (s < n-m) ts+1  (d(ts –T[s+1]h)+ T[s+m+1]) mod q;

ppt20 trang | Chia sẻ: tuanhd28 | Lượt xem: 1758 | Lượt tải: 0download
Bạn đang xem nội dung tài liệu Text processing Pattern Matching, để tải tài liệu về máy bạn click vào nút DOWNLOAD ở trên
Pattern Matching*Pattern MatchingText processingPattern Matching*Outline and ReadingStrings (§9.1.1)Pattern matching algorithmsBrute-force algorithm (§9.1.2)Boyer-Moore algorithm (§9.1.3)Knuth-Morris-Pratt algorithm (§9.1.4)Pattern Matching*StringsA string is a sequence of charactersExamples of strings:Java programHTML documentDNA sequenceDigitized imageAn alphabet S is the set of possible characters for a family of stringsExample of alphabets:ASCIIUnicode{0, 1}{A, C, G, T}Let P be a string of size m A substring P[i .. j] of P is the subsequence of P consisting of the characters with ranks between i and jA prefix of P is a substring of the type P[0 .. i]A suffix of P is a substring of the type P[i ..m - 1] Given strings T (text) and P (pattern), the pattern matching problem consists of finding a substring of T equal to PApplications:Text editorsSearch enginesBiological researchPattern Matching*Brute-Force AlgorithmThe brute-force pattern matching algorithm compares the pattern P with the text T for each possible shift of P relative to T, until eithera match is found, orall placements of the pattern have been triedBrute-force pattern matching runs in time O(nm) Example of worst case:T = aaa ahP = aaahmay occur in images and DNA sequencesunlikely in English textAlgorithm BruteForceMatch(T, P) Input text T of size n and pattern P of size m Output starting index of a substring of T equal to P or -1 if no such substring exists for i  0 to n - m { test shift i of the pattern } j  0 while j n - 1return -1 { no match }Case 2: 1 + l  jPattern Matching*ExamplePattern Matching*AnalysisBoyer-Moore’s algorithm runs in time O(n+m + s)Example of worst case:T = aaa aP = baaaThe worst case may occur in images and DNA sequences but is unlikely in English textBoyer-Moore’s algorithm is significantly faster than the brute-force algorithm on English textPattern Matching*The KMP Algorithm - MotivationKnuth-Morris-Pratt’s algorithm compares the pattern to the text in left-to-right, but shifts the pattern more intelligently than the brute-force algorithm. When a mismatch occurs, what is the most we can shift the pattern so as to avoid redundant comparisons?Answer: the largest prefix of P[0..j] that is a suffix of P[1..j]xj..abaab.....abaabaabaabaNo need torepeat thesecomparisonsResumecomparingherePattern Matching*KMP Failure FunctionKnuth-Morris-Pratt’s algorithm preprocesses the pattern to find matches of prefixes of the pattern with the pattern itselfThe failure function F(j) is defined as the size of the largest prefix of P[0..j] that is also a suffix of P[1..j]Knuth-Morris-Pratt’s algorithm modifies the brute-force algorithm so that if a mismatch occurs at P[j]  T[i] we set j  F(j - 1)j012345P[j]abaabaF(j)001123Pattern Matching*The KMP AlgorithmThe failure function can be represented by an array and can be computed in O(m) timeAt each iteration of the while-loop, eitheri increases by one, orthe shift amount i - j increases by at least one (observe that F(j - 1) 0 j  F[j - 1] else i  i + 1return -1 { no match }Pattern Matching*Computing the Failure FunctionThe failure function can be represented by an array and can be computed in O(m) timeThe construction is similar to the KMP algorithm itselfAt each iteration of the while-loop, eitheri increases by one, orthe shift amount i - j increases by at least one (observe that F(j - 1) 0 then {use failure function to shift P} j  F[j - 1] else F[i]  0 { no match } i  i + 1Pattern Matching*Examplej012345P[j]abacabF(j)001012Rabin-Karp AlgorithmLet  = {0,1,2, . . .,9}.We can view a string of k consecutive characters as representing a length-k decimal number. Let p denote the decimal number for P[1..m]Let ts denote the decimal value of the length-m substring T[s+1..s+m] of T[1..n] for s = 0, 1, . . ., n-m.ts = p if and only if T[s+1..s+m] = P[1..m].p = P[m] + 10(P[m-1] +10(P[m-2]+ . . . +10(P[2]+10(P[1]))We can compute p in O(m) time.Similarly we can compute t0 from T[1..m] in O(m) time.Pattern Matching*Example 6378 = 8 + 10 (7 + 10 (3 + 10(6))) = 8 + 7  10 + 3  102 + 6  103 = 8 + 70 + 300 + 6000Pattern Matching*Compute Tsts+1 can be computed from ts in constant time.ts+1 = 10(ts –10m-1 T[s+1])+ T[s+m+1]Example : T = 314152ts = 31415, s = 0, m= 5 and T[s+m+1] = 2ts+1= 10(31415 –10000*3) +2 = 14152Thus p and t0, t1, . . ., tn-m can all be computed in O(n+m) time. And all occurences of the pattern P[1..m] in the text T[1..n] can be found in time O(n+m). However, p and ts may be too large to work with conveniently. Pattern Matching*Computation of p and t0 using modulus qWith a d-ary alphabet {0,1,,d-1}, q is chosen such that dq fits within a computer word. The recurrence equation can be rewritten as ts+1 = (d(ts –T[s+1]h)+ T[s+m+1]) mod q, where h = dm-1(mod q) is the value of the digit “1” in the high order position of an m-digit text window. Note that ts  p mod q does not imply that ts = p. However, if ts is not equivalent to p mod q , then ts p, and the shift s is invalid. We use ts  p mod q as a fast heuristic test to rule out the invalid shifts. Further testing is done to eliminate spurious hits Test to check whether P[1..m] = T[s+1..s+m]Pattern Matching*Example ts+1 = (d(ts –T[s+1]h)+ T[s+m+1]) mod qh = dm-1(mod q)Example :d=10, alphabet = {09}T = 31415; P = 26, n = 5, m = 2, q = 11We have:p = 26 mod 11 = 4t0 = 31 mod 11 = 9t1 = (10(9 - 3(10) mod 11 ) + 4) mod 11 = (10 (9- 8) + 4) mod 11 = 14 mod 11 = 3Pattern Matching*Rabin-Karp ImplementationProcedure RABIN-KARP-MATCHER(T,P,d,q)Input : Text T, pattern P, radix d ( which is typically =), and the prime q.Output : valid shifts s where P matches n  length[T]; m  length[P]; h  dm-1 mod q; p  0; t0  0; for i  1 to m do { p  (dp + P[i] mod q;t0  (dt0 +T[i] mod q; } for s  0 to n-m do if (p = ts ) if (P[1..m] = T[s+1..s+m]) “pattern occurs with shift ‘s’else if (s < n-m) ts+1  (d(ts –T[s+1]h)+ T[s+m+1]) mod q;Pattern Matching*

Các file đính kèm theo tài liệu này:

  • pptchuong_5_patternmatching_tcde_suu_tam_9335.ppt
Tài liệu liên quan