You are on page 1of 1

Rabin-Karp string matching algorithm

Monday, September 27, 2021 2:55 PM

T = a1…an over ∑.
P = b1…bm over ∑.
Pattern matching is a_i….a_{i+m-1} = P
In other words, a_{i+1}….a_{i+m} = P
|∑| = d, ∑= {0,1,…,d-1}.

We are going to associate a no. to the Pattern P and as well to texts.

Consider a subarray of T

A single arithmetic operation cannot be considered as a unit/constant time operation if we are dealing
with a very larger no. They even might not be representable in constant time.

Then,

Define, a_1 = 0, n1` = a_1….a_m (mod q).

Computation of n_p' (hash no. for Pattern P):


n_p' = 0
For i = 1 to m
n_p' = (d x n_p') mod q + b_i) (mod q)
Return n_p'
Computation of n_1' (hash no. for texts):
n_1' = 0
For i = 1 to m
n_1' = (d x n_1') mod q + a_i) (mod q)
Return n_1'

New Section 91 Page 1

You might also like