Professional Documents
Culture Documents
G-drivelink: https://drive.google.com/file/d/1KlXyM7ZGDifubIeAPz8NHcXM4T_wOegQ/view?usp=sharing
CONTENTS
• What is Rabin Karp Algorithm
• Complexity
• It uses a rolling hash to quickly filter out positions of the text that cannot
match the pattern, and then checks for a match at the remaining
positions. It does not check the string itself, it only checks the value of
the string and then searches for a match.
History of Rabin Karp
• The Rabin–Karp algorithm or Karp–Rabin algorithm is a string searching
algorithm created by Richard M. Karp and Michael O. Rabin in the year
1987, that uses hashing to find an exact match of a pattern string in a text.
• If the hash values are unequal, the algorithm will determine the hash value
for next M-character sequence. If the hash values are equal, the algorithm
will analyze the pattern and the M-character sequence. In this way, there is
only one comparison per text subsequence, and character matching is only
required when the hash values match.
EXAMPLES OF RABIN KARP
Let’s use a problem as an example here:
For string matching, working module q = 11, how many spurious hits does the
Rabin-Karp matcher encounters in Text T = 31415926535.......
Here,
T=31415926535
T.length=11
P=26
Q=11 so,
p mod q=26%11 = 4
We must find the exact match of P mod Q:
T= 3 1 4 1 5 9 2 6 5 3 5
P= 2 6
S=0
33 1 4 1 5 9 2 6 5 3 5
S=1
3 1 4 1 5 9 2 6 5 3 5
S=3
3 1 4 1 5 9 2 6 5 3 5
S=4
3 1 4 1 5 9 2 6 5 3 5