Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Rabin Karp - String Matching Algorithm

1,162 views

Published on

Rabin Karp - String Matching Algorithm

  • Be the first to comment

Rabin Karp - String Matching Algorithm

  1. 1. Yet another string matching algorithm PRESENTED BY P14-6011 P14-6016 Rabin Karp
  2. 2.  Proposed in 1987  Michael O. Rabin  Richard M. Karp  Improved Naive String Matching  By HASHING  Average Running Time O(m+n) Introduction
  3. 3.  Let text string be T of length N  Pattern string be P of length M  Example  T=“hello world”; N=11;  P=“llo”; M=3  Application  Keyword matching in large files  Good for Plagiarism detection Statement
  4. 4.  Calculate Hash of Pattern  As well of M characters of text  If hash is not equal  Calculate hash of next M characters  If hash is equal then  Compare both character by character Note: Only one comparison for sub sequence Working
  5. 5. H10 E20 L30 L30 O40 50 W60 O40 R70 L30 D80 Example tHash = Hash(“HEL”) = 10+20+30 = 60 pHash = Hash(“LLO”) = 30+30+40 = 100 tHash == pHash  FALSE H10 E20 L30 L30 O40 50 W60 O40 R70 L30 D80 tHash = Hash(“ELL”) = 20+30+30 = 80 tHash == pHash  FALSE H10 E20 L30 L30 O40 50 W60 O40 R70 L30 D80 tHash = Hash(“LLO”) = 30+30+40 = 100 tHash == pHash  TRUE
  6. 6. int RabinKarp(string t, string p) { int pHash = Hash(p); int limit = t.size() - p.size() + 1; // n – m + 1 for (int i = 0; i < limit; i++) { string substr = t.substr(i, p.size()); int tHash = Hash(substr); if (pHash == tHash && p == substr) return i; } return -1; } Implementation
  7. 7.  Hash to two string match then it is called Hit  There is possibility  Hash of “LLO” is 100  Hash of “OLL” is 100  This is called Hash Collision  Minimize Collision by  Scaling with index position  30(3)+30(2)+40(1) = 190 | 40(3)+30(2)+30(1) = 210  Taking mod with prime number Hash Collision
  8. 8.  Hash of Pattern  O(m)  Hash Comparison  O(n-m+1) = O(n) ; m < n  Average Running Time  O(m+n)  Worst Case Running Time  m comparison in each iteration  O(mn) Analysis
  9. 9. 1. CLRS – 2nd Edition, Page 911-916 2. slideshare.net/GajanandSharma1/rabin-karp-string-matching-algorithm 3. stoimen.com/blog/2012/04/02/computer-algorithms-rabin-karp-string- searching/ 4. en.wikipedia.org/wiki/Rabin%E2%80%93Karp_algorithm 5. cs.princeton.edu/courses/archive/fall04/cos226/lectures/string.4up.pdf References

×