Upload
syed-owais-ali-chishti
View
178
Download
3
Embed Size (px)
Citation preview
Yet another string matching algorithm
PRESENTED BYP14-6011P14-6016
Rabin Karp
Proposed in 1987 Michael O. Rabin Richard M. Karp
Improved Naive String Matching By HASHING
Average Running Time O(m+n)
Introduction
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
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
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
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
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
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
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