Rabin-Karp Algorithm
Rabin-Karp Algorithm
Comparing numbers is easier and cheaper than comparing strings. Rabin Karp algorithm
represents strings in numbers.
We can compute p in O(m) time and all ts can be computed in O(n – m + 1) time.
Rabin Karp algorithm is based on hashing technique. It first computes the hash value of p
and ts.
If hash values are same, i.e. if hash(p) = hash(ts), we check the equality of inverse hash
similar to a naïve method. If hash values are not same, no need to compare actual string.
On the hash match, actual characters of both strings are compared using brute force
approach. If the pattern is found, then it is called hit. Otherwise, it is called a spurious hit.
For example, let us consider the hash value of string T = ABCDE is 38 and hash of string P =
ABCDX is 71. Clearly, hash values are not same, so strings cannot be same. Brute force
approach does five comparisons whereas Rabin Karp dose only one comparison.
However, the same hash value does not ensure the string match. Two different strings can
have same hash values. That is why we need to compare them character by character on
hash hit.
Given pattern P[1…m], we can derive its numeric value p in base d in O(m) time as follow:
p = P[m] + d(P[m – 1] + d(P[m – 2] + … + d(P[2] + dP[1]) … ))
Similarly, we can derive numeric value of first substring t0 of length m from text T[1…n] in
O(m) time. Remaining all ti, i = 1, 2, 3, …, n – m, can be derived in constant time.
= 10(431 – 400 ) + 5
= 315
Values of p and ts may be too large to process. We can reduce these values by taking its
modulo with suitable number q. typically, q is a prime number.
Computing hash value of every subsequence of m character of text may turn out to be time-
consuming. However, a bit of mathematics makes it easier.
Where h = dm – 1 mod q
Two facts :
ts ≠ p mod q, means ts ≠ p
45365 mod 13 = 8
64371 mod 13 = 8
From above example, it is clear that two different strings might have same mod. We can
reject all negative tests to rule out all possible invalid shifts. And all positive tests must be
validated to overcome spurious hits.
Spurious hits reduce with a larger value of q, and it increases with smaller q.
Complexity Analysis:
Rabin Karp algorithm is a randomized algorithm. In most of the cases, it runs in linear time, i.e. in
O(n). However, the worst case of Rabin-Karp algorithm is as bad as a naïve algorithm, i.e. O(mn), but
it’s rare.
It can happen only when prime number used for hashing is very small.
Example: Explain spurious hits in Rabin-Karp string matching algorithm with an example. Working
modulo q = 13, how many spurious hits does the Rabin-Karp matcher encounter in the text T =
2359023141526739921 when looking for the pattern P = 31415 ?
Solution:
The hash value of pattern P is 7. We have two such values in a hash(T). So there may be a
spurious hit or actual string. In given text T, one hit is an actual match and one is spurious
hit.
If the hash of pattern and any substring in the text is same, we have two possibilities:
(i) Hit : Pattern and text are same
(ii) Spurious hit : Hash value is same but pattern and corresponding text is not same
Consider
= (10*23590) – 105 * 2 + 2
= (10*35902) – 105 * 3 + 3
In same way, we can compute the next ts+i using incremental approach.
Rabin Karp algorithm matches hash value, rather than directly comparing actual string value.
If hash value of pattern P and the hash value of subsequence in string T are same, the actual
value of strings is compared using brute force approach. Like ts+1, we can also derive hash
value incrementally as shown below.
If ts is known, the hash value can directly be derived for ts+1 as follow.
= 10(31415 – 9) + 2 mod 13
= 314062 mod 13 = 8