The RabinKarpAlgorithmThe RabinKarp string matching algorithm calculates a hash value for the pattern, as well as for each Mcharacter subsequences of text to be compared. If the hash values are unequal, the algorithm will determine the hash value for next Mcharacter sequence. If the hash values are equal, the algorithm will analyze the pattern and the Mcharacter sequence. In this way, there is only one comparison per text subsequence, and character matching is only required when the hash values match. RABINKARPMATCHER (T, P, d, q) 1. n ← length [T] 2. m ← length [P] 3. h ← d^{m1} mod q 4. p ← 0 5. t_{0} ← 0 6. for i ← 1 to m 7. do p ← (dp + P[i]) mod q 8. t_{0} ← (dt_{0}+T [i]) mod q 9. for s ← 0 to nm 10. do if p = t_{s} 11. then if P [1.....m] = T [s+1.....s + m] 12. then "Pattern occurs with shift" s 13. If s < nm 14. then t_{s+1} ← (d (t_{s}T [s+1]h)+T [s+m+1])mod q Example: For string matching, working module q = 11, how many spurious hits does the RabinKarp matcher encounters in Text T = 31415926535....... Solution: Complexity:The running time of RABINKARPMATCHER in the worst case scenario O ((nm+1) m but it has a good average case running time. If the expected number of strong shifts is small O (1) and prime q is chosen to be quite large, then the RabinKarp algorithm can be expected to run in time O (n+m) plus the time to require to process spurious hits.
Next TopicString Matching with Finite Automata
