Next:
Up:
Previous:
Rabin-Karp Algorithm
Let characters be digits in radix-
notation.
Choose a prime number
q
such that
fits within a computer word to speed computations.
Algorithm:
Compute (P mod q)
Compute (T[s+1, .., s+m] mod q) for s = 0 .. n-m
Test against P only those sequences in T having the same (mod q) value
(T[s+1, .., s+m] mod q) can be incrementally computed by subtracting the high-order digit, shifting, adding the low-order bit, all in modulo q arithmetic.
Example
= {0, 1, .., 9}
P = 12, P mod 3 = 0
q = 3
Next:
Up:
Previous: