<s>
In	O
computer	B-General_Concept
science	I-General_Concept
,	O
the	O
Rabin	B-Algorithm
–	I-Algorithm
Karp	I-Algorithm
algorithm	I-Algorithm
or	O
Karp	B-Algorithm
–	I-Algorithm
Rabin	I-Algorithm
algorithm	I-Algorithm
is	O
a	O
string-searching	B-Algorithm
algorithm	I-Algorithm
created	O
by	O
that	O
uses	O
hashing	B-Error_Name
to	O
find	O
an	O
exact	O
match	O
of	O
a	O
pattern	O
string	O
in	O
a	O
text	O
.	O
</s>
<s>
It	O
uses	O
a	O
rolling	B-Error_Name
hash	I-Error_Name
to	O
quickly	O
filter	O
out	O
positions	O
of	O
the	O
text	O
that	O
cannot	O
match	O
the	O
pattern	O
,	O
and	O
then	O
checks	O
for	O
a	O
match	O
at	O
the	O
remaining	O
positions	O
.	O
</s>
<s>
although	O
its	O
worst-case	B-General_Concept
time	I-General_Concept
complexity	I-General_Concept
is	O
the	O
product	O
of	O
the	O
two	O
lengths	O
.	O
</s>
<s>
In	O
contrast	O
,	O
the	O
Aho	B-Algorithm
–	I-Algorithm
Corasick	I-Algorithm
algorithm	I-Algorithm
can	O
find	O
all	O
matches	O
of	O
multiple	O
patterns	O
in	O
worst-case	O
time	O
and	O
space	O
linear	O
in	O
the	O
input	O
length	O
and	O
the	O
number	O
of	O
matches	O
(	O
instead	O
of	O
the	O
total	O
length	O
of	O
the	O
matches	O
)	O
.	O
</s>
<s>
A	O
naive	O
string	B-Algorithm
matching	I-Algorithm
algorithm	I-Algorithm
compares	O
the	O
given	O
pattern	O
against	O
all	O
positions	O
in	O
the	O
given	O
text	O
.	O
</s>
<s>
Several	O
string-matching	O
algorithms	O
,	O
including	O
the	O
Knuth	O
–	O
Morris	O
–	O
Pratt	O
algorithm	O
and	O
the	O
Boyer	B-Algorithm
–	I-Algorithm
Moore	I-Algorithm
string-search	I-Algorithm
algorithm	I-Algorithm
,	O
reduce	O
the	O
worst-case	O
time	O
for	O
string	B-Algorithm
matching	I-Algorithm
by	O
extracting	O
more	O
information	O
from	O
each	O
mismatch	O
,	O
allowing	O
them	O
to	O
skip	O
over	O
positions	O
of	O
the	O
text	O
that	O
are	O
guaranteed	O
not	O
to	O
match	O
the	O
pattern	O
.	O
</s>
<s>
The	O
Rabin	B-Algorithm
–	I-Algorithm
Karp	I-Algorithm
algorithm	I-Algorithm
instead	O
achieves	O
its	O
speedup	O
by	O
using	O
a	O
hash	B-Error_Name
function	I-Error_Name
to	O
quickly	O
perform	O
an	O
approximate	O
check	O
for	O
each	O
position	O
,	O
and	O
then	O
only	O
performing	O
an	O
exact	O
comparison	O
at	O
the	O
positions	O
that	O
pass	O
this	O
approximate	O
check	O
.	O
</s>
<s>
A	O
hash	B-Error_Name
function	I-Error_Name
is	O
a	O
function	O
which	O
converts	O
every	O
string	O
into	O
a	O
numeric	O
value	O
,	O
called	O
its	O
hash	B-Error_Name
value	I-Error_Name
;	O
for	O
example	O
,	O
we	O
might	O
have	O
hash("hello" )	O
=	O
5	O
.	O
</s>
<s>
If	O
two	O
strings	O
are	O
equal	O
,	O
their	O
hash	B-Error_Name
values	I-Error_Name
are	O
also	O
equal	O
.	O
</s>
<s>
For	O
a	O
well-designed	O
hash	B-Error_Name
function	I-Error_Name
,	O
the	O
inverse	O
is	O
true	O
,	O
in	O
an	O
approximate	O
sense	O
:	O
strings	O
that	O
are	O
unequal	O
are	O
very	O
unlikely	O
to	O
have	O
equal	O
hash	B-Error_Name
values	I-Error_Name
.	O
</s>
<s>
The	O
Rabin	B-Algorithm
–	I-Algorithm
Karp	I-Algorithm
algorithm	I-Algorithm
proceeds	O
by	O
computing	O
,	O
at	O
each	O
position	O
of	O
the	O
text	O
,	O
the	O
hash	B-Error_Name
value	I-Error_Name
of	O
a	O
string	O
starting	O
at	O
that	O
position	O
with	O
the	O
same	O
length	O
as	O
the	O
pattern	O
.	O
</s>
<s>
If	O
this	O
hash	B-Error_Name
value	I-Error_Name
equals	O
the	O
hash	B-Error_Name
value	I-Error_Name
of	O
the	O
pattern	O
,	O
it	O
performs	O
a	O
full	O
comparison	O
at	O
that	O
position	O
.	O
</s>
<s>
In	O
order	O
for	O
this	O
to	O
work	O
well	O
,	O
the	O
hash	B-Error_Name
function	I-Error_Name
should	O
be	O
selected	O
randomly	O
from	O
a	O
family	O
of	O
hash	B-Error_Name
functions	I-Error_Name
that	O
are	O
unlikely	O
to	O
produce	O
many	O
false	O
positives	O
,	O
that	O
is	O
,	O
positions	O
of	O
the	O
text	O
which	O
have	O
the	O
same	O
hash	B-Error_Name
value	I-Error_Name
as	O
the	O
pattern	O
but	O
do	O
not	O
actually	O
match	O
the	O
pattern	O
.	O
</s>
<s>
Additionally	O
,	O
the	O
hash	B-Error_Name
function	I-Error_Name
used	O
should	O
be	O
a	O
rolling	B-Error_Name
hash	I-Error_Name
,	O
a	O
hash	B-Error_Name
function	I-Error_Name
whose	O
value	O
can	O
be	O
quickly	O
updated	O
from	O
each	O
position	O
of	O
the	O
text	O
to	O
the	O
next	O
.	O
</s>
<s>
Recomputing	O
the	O
hash	B-Error_Name
function	I-Error_Name
from	O
scratch	O
at	O
each	O
position	O
would	O
be	O
too	O
slow	O
.	O
</s>
<s>
However	O
,	O
line	O
2	O
is	O
only	O
executed	O
once	O
,	O
and	O
line	O
6	O
is	O
only	O
executed	O
if	O
the	O
hash	B-Error_Name
values	I-Error_Name
match	O
,	O
which	O
is	O
unlikely	O
to	O
happen	O
more	O
than	O
a	O
few	O
times	O
.	O
</s>
<s>
Naively	O
computing	O
the	O
hash	B-Error_Name
value	I-Error_Name
for	O
the	O
substring	O
1	O
..	O
i	O
requires	O
O(m )	O
time	O
because	O
each	O
character	O
is	O
examined	O
.	O
</s>
<s>
Since	O
the	O
hash	B-Error_Name
computation	O
is	O
done	O
on	O
each	O
loop	O
,	O
the	O
algorithm	O
with	O
a	O
naive	O
hash	B-Error_Name
computation	O
requires	O
O(mn )	O
time	O
,	O
the	O
same	O
complexity	O
as	O
a	O
straightforward	O
string	B-Algorithm
matching	I-Algorithm
algorithm	I-Algorithm
.	O
</s>
<s>
For	O
speed	O
,	O
the	O
hash	B-Error_Name
must	O
be	O
computed	O
in	O
constant	O
time	O
.	O
</s>
<s>
The	O
trick	O
is	O
the	O
variable	O
hs	O
already	O
contains	O
the	O
previous	O
hash	B-Error_Name
value	I-Error_Name
of	O
i	O
..	O
i	O
.	O
</s>
<s>
If	O
that	O
value	O
can	O
be	O
used	O
to	O
compute	O
the	O
next	O
hash	B-Error_Name
value	I-Error_Name
in	O
constant	O
time	O
,	O
then	O
computing	O
successive	O
hash	B-Error_Name
values	I-Error_Name
will	O
be	O
fast	O
.	O
</s>
<s>
The	O
trick	O
can	O
be	O
exploited	O
using	O
a	O
rolling	B-Error_Name
hash	I-Error_Name
.	O
</s>
<s>
A	O
rolling	B-Error_Name
hash	I-Error_Name
is	O
a	O
hash	B-Error_Name
function	I-Error_Name
specially	O
designed	O
to	O
enable	O
this	O
operation	O
.	O
</s>
<s>
A	O
trivial	O
(	O
but	O
not	O
very	O
good	O
)	O
rolling	B-Error_Name
hash	I-Error_Name
function	O
just	O
adds	O
the	O
values	O
of	O
each	O
character	O
in	O
the	O
substring	O
.	O
</s>
<s>
This	O
rolling	B-Error_Name
hash	I-Error_Name
formula	O
can	O
compute	O
the	O
next	O
hash	B-Error_Name
value	I-Error_Name
from	O
the	O
previous	O
value	O
in	O
constant	O
time	O
:	O
</s>
<s>
This	O
simple	O
function	O
works	O
,	O
but	O
will	O
result	O
in	O
statement	O
5	O
being	O
executed	O
more	O
often	O
than	O
other	O
more	O
sophisticated	O
rolling	B-Error_Name
hash	I-Error_Name
functions	O
such	O
as	O
those	O
discussed	O
in	O
the	O
next	O
section	O
.	O
</s>
<s>
Good	O
performance	O
requires	O
a	O
good	O
hashing	B-Error_Name
function	I-Error_Name
for	O
the	O
encountered	O
data	O
.	O
</s>
<s>
If	O
the	O
hashing	B-Error_Name
is	O
poor	O
(	O
such	O
as	O
producing	O
the	O
same	O
hash	B-Error_Name
value	I-Error_Name
for	O
every	O
input	O
)	O
,	O
then	O
line	O
6	O
would	O
be	O
executed	O
O(n )	O
times	O
(	O
i.e.	O
</s>
<s>
The	O
key	O
to	O
the	O
Rabin	B-Algorithm
–	I-Algorithm
Karp	I-Algorithm
algorithm	I-Algorithm
's	O
performance	O
is	O
the	O
efficient	O
computation	O
of	O
hash	B-Error_Name
values	I-Error_Name
of	O
the	O
successive	O
substrings	O
of	O
the	O
text	O
.	O
</s>
<s>
The	O
Rabin	B-Algorithm
fingerprint	I-Algorithm
is	O
a	O
popular	O
and	O
effective	O
rolling	B-Error_Name
hash	I-Error_Name
function	O
.	O
</s>
<s>
The	O
hash	B-Error_Name
function	I-Error_Name
described	O
here	O
is	O
not	O
a	O
Rabin	B-Algorithm
fingerprint	I-Algorithm
,	O
but	O
it	O
works	O
equally	O
well	O
.	O
</s>
<s>
See	O
hash	B-Error_Name
function	I-Error_Name
for	O
a	O
much	O
more	O
detailed	O
discussion	O
.	O
</s>
<s>
The	O
essential	O
benefit	O
achieved	O
by	O
using	O
a	O
rolling	B-Error_Name
hash	I-Error_Name
such	O
as	O
the	O
Rabin	B-Algorithm
fingerprint	I-Algorithm
is	O
that	O
it	O
is	O
possible	O
to	O
compute	O
the	O
hash	B-Error_Name
value	I-Error_Name
of	O
the	O
next	O
substring	O
from	O
the	O
previous	O
one	O
by	O
doing	O
only	O
a	O
constant	O
number	O
of	O
operations	O
,	O
independent	O
of	O
the	O
substrings	O
 '	O
lengths	O
.	O
</s>
<s>
For	O
example	O
,	O
if	O
we	O
have	O
text	O
"	O
abracadabra	O
"	O
and	O
we	O
are	O
searching	O
for	O
a	O
pattern	O
of	O
length	O
3	O
,	O
the	O
hash	B-Error_Name
of	O
the	O
first	O
substring	O
,	O
"	O
abr	O
"	O
,	O
using	O
256	O
as	O
the	O
base	O
,	O
and	O
101	O
as	O
the	O
prime	O
modulus	O
is	O
:	O
</s>
<s>
//	O
ASCII	B-Protocol
a	O
=	O
97	O
,	O
b	O
=	O
98	O
,	O
r	O
=	O
114	O
.	O
</s>
<s>
We	O
can	O
then	O
compute	O
the	O
hash	B-Error_Name
of	O
the	O
next	O
substring	O
,	O
"	O
bra	O
"	O
,	O
from	O
the	O
hash	B-Error_Name
of	O
"	O
abr	O
"	O
by	O
subtracting	O
the	O
number	O
added	O
for	O
the	O
first	O
'	O
a	O
 '	O
of	O
"	O
abr	O
"	O
,	O
i.e.	O
</s>
<s>
Because	O
we	O
know	O
all	O
hashes	B-Error_Name
for	O
prime	O
modulus	O
$p$	O
,	O
we	O
can	O
ensure	O
no	O
underflow	O
by	O
adding	O
p	O
to	O
the	O
old	O
hash	B-Error_Name
before	O
subtracting	O
the	O
value	O
corresponding	O
to	O
the	O
old	O
'	O
a	O
 '	O
(	O
mod	O
p	O
)	O
.	O
</s>
<s>
If	O
we	O
are	O
matching	O
the	O
search	B-Algorithm
string	I-Algorithm
"	O
bra	O
"	O
,	O
using	O
similar	O
calculation	O
of	O
hash("abr" )	O
,	O
</s>
<s>
If	O
the	O
substrings	O
in	O
question	O
are	O
long	O
,	O
this	O
algorithm	O
achieves	O
great	O
savings	O
compared	O
with	O
many	O
other	O
hashing	B-Error_Name
schemes	O
.	O
</s>
<s>
multiplying	O
together	O
ASCII	B-Protocol
values	I-Protocol
of	O
all	O
characters	O
so	O
that	O
shifting	O
substring	O
would	O
only	O
entail	O
dividing	O
the	O
previous	O
hash	B-Error_Name
by	O
the	O
first	O
character	O
value	O
,	O
then	O
multiplying	O
by	O
the	O
new	O
last	O
character	O
's	O
value	O
.	O
</s>
<s>
The	O
limitation	O
,	O
however	O
,	O
is	O
the	O
limited	O
size	O
of	O
the	O
integer	O
data	O
type	O
and	O
the	O
necessity	O
of	O
using	O
modular	O
arithmetic	O
to	O
scale	O
down	O
the	O
hash	B-Error_Name
results	O
,	O
(	O
see	O
hash	B-Error_Name
function	I-Error_Name
article	O
)	O
.	O
</s>
<s>
Meanwhile	O
,	O
naive	O
hash	B-Error_Name
functions	I-Error_Name
do	O
not	O
produce	O
large	O
numbers	O
quickly	O
,	O
but	O
,	O
just	O
like	O
adding	O
ASCII	B-Protocol
values	I-Protocol
,	O
are	O
likely	O
to	O
cause	O
many	O
hash	B-Algorithm
collisions	I-Algorithm
and	O
hence	O
slow	O
down	O
the	O
algorithm	O
.	O
</s>
<s>
Hence	O
the	O
described	O
hash	B-Error_Name
function	I-Error_Name
is	O
typically	O
the	O
preferred	O
one	O
in	O
the	O
Rabin	B-Algorithm
–	I-Algorithm
Karp	I-Algorithm
algorithm	I-Algorithm
.	O
</s>
<s>
The	O
Rabin	B-Algorithm
–	I-Algorithm
Karp	I-Algorithm
algorithm	I-Algorithm
is	O
inferior	O
for	O
single	O
pattern	O
searching	O
to	O
Knuth	O
–	O
Morris	O
–	O
Pratt	O
algorithm	O
,	O
Boyer	B-Algorithm
–	I-Algorithm
Moore	I-Algorithm
string-search	I-Algorithm
algorithm	I-Algorithm
and	O
other	O
faster	O
single	O
pattern	O
string	B-Algorithm
searching	I-Algorithm
algorithms	I-Algorithm
because	O
of	O
its	O
slow	O
worst	O
case	O
behavior	O
.	O
</s>
<s>
To	O
find	O
any	O
of	O
a	O
large	O
number	O
,	O
say	O
k	O
,	O
fixed	O
length	O
patterns	O
in	O
a	O
text	O
,	O
a	O
simple	O
variant	O
of	O
the	O
Rabin	B-Algorithm
–	I-Algorithm
Karp	I-Algorithm
algorithm	I-Algorithm
uses	O
a	O
Bloom	B-Algorithm
filter	I-Algorithm
or	O
a	O
set	O
data	O
structure	O
to	O
check	O
whether	O
the	O
hash	B-Error_Name
of	O
a	O
given	O
string	O
belongs	O
to	O
a	O
set	O
of	O
hash	B-Error_Name
values	I-Error_Name
of	O
patterns	O
we	O
are	O
looking	O
for	O
:	O
</s>
<s>
In	O
contrast	O
,	O
the	O
above	O
algorithm	O
can	O
find	O
all	O
k	O
patterns	O
in	O
O( n+km''	O
)	O
expected	O
time	O
,	O
assuming	O
that	O
a	O
hash	B-Error_Name
table	O
check	O
works	O
in	O
O(1 )	O
expected	O
time	O
.	O
</s>
