Rabin-Karp Algorithm Hash Calculation - AOA MODULE 6
Concept of Rabin-Karp Algorithm
The Rabin-Karp algorithm is based on the rolling hash function. Instead of directly comparing substrings character by character (as in brute force), it computes a hash value for the pattern and compares it with the hash values of substrings in the text.
Key StepsCompute the hash of the pattern (P). Compute the hash of the first window of text (T) with the same length as P.
Compare the hash values:
If hash(P) ≠ hash(T) → slide the window to the right and recompute the hash.
If hash(P) = hash(T) → compare character by character to confirm.
Repeat until the entire text is scanned.Calculating Hash Value in Rabin-Karp Algorithm
Step 1: Assign Modulus and a Base Value
Suppose we have a text Txt = "XZYABCDZYA"
and a pattern Ptrn = "BCD"
.
We assign numerical values to the characters of the text based on their ranking. The leftmost character has rank 1 and the rightmost has rank 10.
We use:
- Base (b) = 10 (number of unique characters in the text)
- Modulus (m) = 13 (a prime number to avoid overflow issues)
Step 2: Calculate Hash Value of Pattern
The formula to calculate the hash value of the pattern is:
Where:
- r: ranking of the character
- l: length of the pattern
- i: index of the character in the pattern
For Ptrn = "BCD":
= (400 + 50 + 6) mod 13
= 456 mod 13
= 2
Step 3: Calculate Hash Value of First Text Window
Now, we compute the hash value for the first substring "XZY"
in the text.
= (700 + 80 + 9) mod 13
= 789 mod 13
= 7
Now, compare the hash values of pattern (2) and substring (7). Since they are not equal, move to the next window.
Step 4: Updating the Hash Value
Instead of recalculating from scratch, use the rolling hash formula:
Remove 'X'
, add 'A'
, and update the hash. Continue this process for each new substring until a match is found.
Comments
Post a Comment