In this tutorial we shall understand how Rabin Karp algorithm will work. This is the 2ndkind of algorithm we study in pattern matching algorithm set.
Rabin karp algorithm uses a hash function and brute force approach to check if a pattern is present inside a string.
Below is the high level working of the algorithm.
- Get the hash of the pattern. Suppose the length of the pattern is ‘n’.
- Get the hash of the whole string, by dividing the string in to “n” characters. And get the hash of those individual sub strings.
- Check if any hash value from substring matches our pattern hash.
- If it matches, do a brute force check and check if all characters ae same as pattern.
- If it did not match, then calculate the hash for the next “n” pattern.
Below is the basic working of algorithm with example:
Consider the String “S” as abdcabcde
Pattern “P” “abc”
Now what we do is, we calculate the hash value of the string for the size that is equal to our pattern.
As out pattern is “abc”, the length is 3. Hence for our string “abdcabcde” we calculate the hash as:
hash(abd) => u
hash(bdc) => v
hash(dca) => w
hash(abc) => x
hash(bcd) => y
hash(cde) => x
Here we are sending an input to hash, it is giving an hashed output.
Now we shall calculate hash value for our search pattern:
Hash(abc) = x.
Now that we have calculated hash for the string and for pattern, we shall if there is any hash matches our pattern hash.
Yes, there is a match. When there is a match, we shall do brute force search of the sub-string and pattern. Then one of below 2 things can happen:
- If all the characters in the string are same, then it is called as valid hit.
- If the character are not same, it is called as spurious hit. Because the hash value matches, but strings did not match.
In our example, the hash value for our pattern is ‘x’.In the hashes calculated, we have 2 substrings where the hash value is ‘x’.
hash(abc) = x
hash(cde) = x.
Here “abc” is a valid hit.
“cde” is a spurious hit.
To avoid these kind of situations, it is recommended to have the hash key of larger value.
Now we shall see on how to actually solve by using a hash function and followed by rolling hash function.
Consider the string s = abdcabcd
Pattern p = abc
Our hash function will be as below:
Hash_function = first_char + second_char*prime_number^1 + third_character * prime_number^2.
As out pattern is of length 3, hash function will also take input of 3 character.
Let our prime number be 3. Note that the prime number can be random. Larger the prime number, less chances of getting same output from hash function.
So for our pattern “abc”, we shall consider
a -> 1
b -> 2
c -> 3
d -> 4
z -> 26
So for our pattern abc, hash will be calculated as below:
- 1 + 2*3 + 3*3*3
- 1 + 6 + 27
The output will be 34. New we shall calculate hash for our string and try to match with out pattern.
Now for the string “abd” => 1 + 2*3 +4*9
- 1 + 6 + 36
Now for the string “bdc” => 2 + 4*3 +3*9
- 2 + 12 + 27
In the above 2 steps, we are actually rolling over character by character, at any point we would have already calculated value for 2 characters.
As we would have calculated the hash value of “bd” already, there is no need to calculate again. Here we need to calculate only for the new character i.e “c”.
This can be calculated by 3 step process.
Let x = old hash value – value of (old_char)
new Hash value = x + prime^n-1 * valueof(new_character)
i.e for “abd” we have 43.
Now for the string “bdc”
- 43 – value of (a)
- 43 -1 = 42
- 42/3 = 14
- 14 + 3*9 = 41
The value is same as we calculated earlier individually. This method is called as rolling hash method.
Now for the string “dca”
- 41 – value of (b)
- 41 -2 = 39
- 39/3 = 13
- 13 + 1*9 = 22
Now for the string “cab”
- 22 – value of (d)
- 22 – 4 = 18
- 18/3 = 6
- 6 + 2*9 = 24
Now for the string “abc”
- 24 – value of (c)
- 24 – 3 = 21
- 21/3 = 7
- 7 + 3*9 = 34
Now for the string “bcd”
- 34 – value of (a)
- 34 – 1 = 33
- 33/3 = 11
- 11 + 4*9 = 47
Now we have calculated all the hash value for the string, we now shall compare with the pattern hash.
When we find a match, we check if all the characters are same. If same we got out search pattern.
If the hash value is same but strings are different, we conclude that it is a spurious hit and check if there exist another hash value that is same as our pattern hash.
AJ’s definitive guide for DS and Algorithms. Click here to study the complete list of algorithm and data structure tutorial. 85+ chapters to study from.