Module9_08
Module9_08
Algorithms
Topics
Basics of Strings
Brute-force String Matcher
KMP Algorithm
1
In string matching problems, it is required to find the
occurrences of a pattern in a text.
Procedure BF_String_Matcher(T,P)
1. n length [T];
2. m length[P];
3. for s 0 to n-m
4. do if P[1..m] = T[s+1..s+m]
5. then shift s is valid
a a b a a b
a c a a b c
a a b
a c a a b c matches
a a b
CSE5311 Kumar
5
Rabin-Karp Algorithm
ts = p if and only if
T[s+1..s+m] = P[1..m], and s is a valid shift.
CSE5311 Kumar
7
ts+1 can be computed from ts in constant time.
Example : T = 314152
ts = 31415, s = 0, m= 5 and T[s+m+1] = 2
conveniently.
Computation of p and t0 and the recurrence is done using modulus q.
T = 31415; P = 26, n = 5, m = 2, q = 11
p = 26 mod 11 = 4
t0 = 31 mod 11 = 9
t1 = (10(9 - 3(10) mod 11 ) + 4) mod 11
= (10 (9- 8) + 4) mod 11 = 14 mod 11 = 3
CSE5311 Kumar
10
Procedure RABIN-KARP-MATCHER(T,P,d,q)
Input : Text T, pattern P, radix d ( which is typically =),
and the prime q.
Output : valid shifts s where P matches
1. n length[T];
2. m length[P];
3. h dm-1 mod q;
4. p 0;
5. t0 0;
6. for i 1 to m
7. do p (dp + P[i] mod q;
8. t0 (dt0 +T[i] mod q;
9. for s 0 to n-m
10. do if p = ts
11. then if P[1..m] = T[s+1..s+m]
12. then “pattern occurs with shift
‘s’
13. if s < n-m
CSE5311 Kumar
11
14. then ts+1 (d(ts –T[s+1]h)+ T[s+m+1])
Comments on Rabin-Karp Algorithm
CSE5311 Kumar
12
Exercises
-- Home work
Study KMP Algorithm for String Matching
-- Knuth Morris Pratt (KMP)
Study Boyer-Moore Algorithm for String matching
The input is two strings of characters A = a1, a2,…, an and B = b1, b2, …,
bn. Design an O(n) time algorithm to determine whether B is a cyclic shift of
A. In other words, the algorithm should determine whether there exists an
index k, 1 k n such that ai = b(k+i) mod n , for all i, 1 i n.
CSE5311 Kumar
13