9
Yet another string matching algorithm PRESENTED BY P14-6011 P14-6016 Rabin Karp

Rabin Karp - String Matching Algorithm

Embed Size (px)

Citation preview

Page 1: Rabin Karp - String Matching Algorithm

Yet another string matching algorithm

PRESENTED BYP14-6011P14-6016

Rabin Karp

Page 2: Rabin Karp - String Matching Algorithm

Proposed in 1987 Michael O. Rabin Richard M. Karp

Improved Naive String Matching By HASHING

Average Running Time O(m+n)

Introduction

Page 3: Rabin Karp - String Matching Algorithm

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

Page 4: Rabin Karp - String Matching Algorithm

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

Page 5: Rabin Karp - String Matching Algorithm

H10 E20 L30 L30 O40 50 W60 O40 R70 L30 D80

Example

tHash = Hash(“HEL”) = 10+20+30 = 60pHash = Hash(“LLO”) = 30+30+40 = 100tHash == pHash FALSE

H10 E20 L30 L30 O40 50 W60 O40 R70 L30 D80

tHash = Hash(“ELL”) = 20+30+30 = 80tHash == pHash FALSE

H10 E20 L30 L30 O40 50 W60 O40 R70 L30 D80

tHash = Hash(“LLO”) = 30+30+40 = 100tHash == pHash TRUE

Page 6: Rabin Karp - String Matching Algorithm

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

Page 7: Rabin Karp - String Matching Algorithm

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

Page 8: Rabin Karp - String Matching Algorithm

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

Page 9: Rabin Karp - String Matching Algorithm

1. CLRS – 2nd Edition, Page 911-916 2. slideshare.net/GajanandSharma1/rabin-karp-string-matching-

algorithm3. stoimen.com/blog/2012/04/02/computer-algorithms-rabin-karp-

string-searching/4. en.wikipedia.org/wiki/Rabin%E2%80%93Karp_algorithm5. cs.princeton.edu/courses/archive/fall04/cos226/lectures/

string.4up.pdf

References