





















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!






















-^ Given a text
T^ [1 ..^ n
] of length
n, a^ pattern
P [1 ..^ m
] of
length^ m
≤^ n , both as arrays.
-^ Further assume that elements of
P^ and^ T
are characters
drawn from a finite set of alphabets
-^ Now for 0
≤^ s^ ≤^ n - m if T [s + j] = P[ j],
^ j^ {1, 2,.. ., m} then p occurs in T with shift s, and we call s as a
valid
shift ; otherwise, s an
invalid shift
-^ Now our objective is “finding all valid shifts with whicha given pattern
P^ occurs in a text
a^ a^ b
c s^ = 0a^ a^ b
n^ ←^ length
[ T ] = 6 m^ ←^ length
[ P ] = 3 for^ s^ ←^ 0 to n – m (6 - 3 = 3)^ P [1] =
T [ s^ + 1] P [1] = T [1]^
(As a = a) P [2] =^ T [
s^ + 2] But^ P [2]^
^ T [2]^
(As a^ ^ c)
a^ c^ a
a^ b^
c a^ a s^ = 1
for^ s^ ←^1^ P [1] =
T [ s^ + 1]But P [1]^ ^ T [2]
(As a
^ c)
b
a^ c^ s = 3 a^ a^ b
c
for^ s^ ←^3^ P [1] =
T [ s^ + 1] P [1] = T [4]^
(As a = a) P [2] =^ T [
s^ + 2] But^ P [2] ^ T [5]^
(As a^ ^ b)
a^ a^ b
Special Case•^ Given a text
T^ [1 ..^ n
] of length
n, a^ pattern
P [1 ..^ m
] of
length^ m
≤^ n , both as arrays.
-^ Assume that elements of
P^ and^ T
are characters drawn
from a finite set of alphabets
-^ Where
Σ^ = {0, 1, 2,... , 9}, so that each character is adecimal digit.
-^ Now our objective is “finding all valid shifts with whicha given pattern
P^ occurs in a text
Example: Horner’s rule[3, 4, 5] = 5 + 10(4 + 10(3)) = 5 + 10(4 + 30) = 5 340 = 345 p^ =^ P [^3
Formula•^ We can compute
p^ in time
Θ( m ) using this rule as
p^ =^ P [ m ] + 10 ( P
[ m -1] + 10(
P [ m -2] + … + 10(
P [2] + 10
P [1]) ))
-^ Similarly
t can be computed from^0
T^ [1 ..^ m
] in time
Θ( m ).
-^ To compute
t ,^ t ,... ,^1
t^ in time n - m^
Θ( n^ -^ m
), it suffices to
observe that
t^ can be computed from s+^
t^ in constant time. s^
-^ Text = [3, 1, 4, 1, 5, 2]; t
= 31415 0
-^ m = 5; Shift = 0•^ Old higher-order digit = 3•^ New low-order digit = 2•^ t= 10.(31415 – 10^1 4. T(1)) + T(5+1) = 10.(31415 – 10
4 .3) + 2 = 10(1415) + 2 = 14152• t= 10 ( ts+1 s^
- T [ s +^
m -11] 10 )^ + T [ s + m +
1])
-^ t= 10^1
( t– T [1] 10^0
4 )^ + T [
+ 5 +^ 1])
-^ Now^
t ,^ t ,... ,^1
tcan be computed n-m^
in^ Θ( n^ 3 1 4 1 5 2 -^ m )
mod 13 7
≡^ 7 (mod 13)
≡^ 7 (mod 13)
0 2 3 5
9 0 2
3 1 4
1 5 2
6 7 3
9 9 2
1^ mod 13
8 9 3 11
1 7
8 4 5 10 11 7
… 9 11 … …
Spurious hit Valid match 1 2 3
4 5 6
7 8 9
10 11 12 13 14 15 16 17 18 19
1.^ Compute the lengths of pattern P and text T2.^ Compute p and t
under modulo q using Horner’s Rules
3.^ For any shift
s^ for which
t^ ≡^ p^ (mod s^
q ), must be tested
further to see if
s^ is really valid shift or a
spurious hit
4.^ This testing can be done by checking the condition:^ P [ ..
m ] =^ T^
[ s^ + 1 ..
s^ +^ m ]. If these strings are equal s is a valid shift otherwise spurious hit.5. If for shift
s, t^ ≡^ ps^
(mod^ q
) is false, compute t
ands+^
replace it with t
and repeat the step 3.s^ Note•^ As^
t^ ≡^ p^ (mod s^
q ) does not imply that
t^ =^ p , hence text s^
RABIN-KARP-MATCHER(
T ,^ P ,^ d ,^ q ) 1 n^ ←^ length
[ T ] 2 m^ ←^ length
[ P ] m 3 h ← d -1^ mod^ q 4 p^ ←^05 t ←^00 6 for^ i^ ←
1 to^ m^
^ Preprocessing.
7 do^ p
←^ ( dp^ +^ P
[ i ]) mod^ q 8 t
←^ ( dt +^0 T [ i ]) mod^ q 9 for^ s^ ←
0 to^ n^ -^ m^
^ Matching.
10 do if
p^ =^ t^ s 11
then if^ P [ ..
m ] =^ T^ [ s^ + 1 ..
s^ +^ m ]
12
then^ print "Pattern occurs with shift"
s
13
if^ s^ <^ n^ -^ m 14
then^ t^ ← s +^
( d ( t^ -^ T [ s^ s^
s^ +^ m^ + 1]) mod
q
-^ A^ finite automaton
M^ is a 5-tuple (
Q ,^ q ,^ A^0
,^ Σ,^ δ ), where
-^ Q^ is a finite set of
states ,
-^ q ^0
Q^ is the start state
,
-^ A^ ^ Q
is a distinguished set of
accepting states
,
-^ Σ^ is a finite
input alphabet
,
-^ δ^ is a function from
Q^ ×^ Σ^ into^ Q , called the
transition
function
of^ M.
-^ String-matching automata are very efficient because itexamines each character
exactly once
, taking constant time.
-^ The matching time used-after preprocessing the pattern tobuild the automaton-is therefore
Θ( n ).
1.^ Empty string is both a suffix and a prefix of every string.2.^ For any strings
x^ and^ y and any character
a , we have
x^
y^ if and only if
xa^ ^ ya
.
3.^ Also it can be proved that
^ and^
are transitive
relations.Proof :Property 3• Suppose that x
^ y and y
^ z, we have to prove that x
z. • x^ ^ y^
w^ ^ Σ^1
1
(A)
-^ y^
z^ ^ w
^ Σ* such that z = yw 2
2
(B)
-^ From (A) and (B)z = yw
= xw^ w