0% found this document useful (0 votes)
25 views9 pages

Se - 32

Uploaded by

22r11a6259
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views9 pages

Se - 32

Uploaded by

22r11a6259
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 9

18CS22707S

Analysis and Design of


Algorithms

Rabin Karp string matching algorithm


Session 32
Rabin-Karp string matching algorithm
• Rabin and Karp proposed a string-matching
algorithm to find the pattern in a more efficient way.
• It also checks the pattern by moving window one by
one, but without checking all characters for all cases,
it finds the hash value.
• When the hash value is matched, then only it tries to
check each character.
• This procedure makes the algorithm more efficient
compared to naïve string matching algorithm.
How Rabin-Karp works
• Given a pattern P [1..m], let p denote its corresponding
hash value and a text T [1..n], let ts denote the hash value
of the length-m substring T T[s+1..s+m],for s=0,1..n-m.
• Certainly, s is a valid shift iff ts = p and T[s+1..s+m] =
P[1..m] otherwise s is a invalid shift.
• Let characters in both arrays T and P be digits in radix-
notation. (
• Choose a prime number q such that fits within a
computer word to speed computations.
• Compute (p mod q)
– The value of p mod q is what we will be using to find all matches
of the pattern P in T.
Rabin-Karp : Algorithm
RABIN-KARP-MATCHER(T, P, d, q)
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 do
7. p = (dp + P[i]) mod q
8. t0 =(dt0 + T[i]) mod q
Rabin-Karp : Algorithm(continued)
9. for s =0 to n – m do
10. if p == ts then
11. if P[1 . . m] = T[s + 1 . . s + m] then
12. Print "Pattern occurs with shift" s
13. if s < n – m then
14. ts+1 =(d(ts - T[s + 1]h) + T[s + m + 1]) mod q
A Rabin-Karp example
• Given T = 31415926535 and P = 26
• We choose q = 11
• P mod q = 26 mod 11 = 4

3 1 4 1 5 9 2 6 5 3 5
31 mod 11 = 9 not equal to 4

3 1 4 1 5 9 2 6 5 3 5

14 mod 11 = 3 not equal to 4

3 1 4 1 5 9 2 6 5 3 5

41 mod 11 = 8 not equal to 4


Rabin-Karp example continued
3 1 4 1 5 9 2 6 5 3 5
15 mod 11 = 4 equal to 4 -> spurious hit

3 1 4 1 5 9 2 6 5 3 5
59 mod 11 = 4 equal to 4 -> spurious hit

3 1 4 1 5 9 2 6 5 3 5
92 mod 11 = 4 equal to 4 -> spurious hit

3 1 4 1 5 9 2 6 5 3 5

26 mod 11 = 4 equal to 4 -> an exact match!!


3 1 4 1 5 9 2 6 5 3 5
65 mod 11 = 10 not equal to 4
Rabin-Karp example continued
3 1 4 1 5 9 2 6 5 3 5
53 mod 11 = 9 not equal to 4

3 1 4 1 5 9 2 6 5 3 5
35 mod 11 = 2 not equal to 4

As we can see, when a match is found, further testing is


done to insure that a match has indeed been found.
Complexity
• The running time of the Rabin-Karp algorithm in the
worst-case scenario is O(n-m+1)m but it has a good
average-case running time.
• If the expected number of valid shifts is small O(1)
and the prime q is chosen to be quite large, then the
Rabin-Karp algorithm can be expected to run in time
O(n+m) plus the time to required to process spurious
hits.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy