Rabin-Karp Algorithm Hash Calculation - AOA MODULE 6

Rabin-Karp Algorithm Hash Calculation

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 Steps

Compute 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:

hash value(Ptrn) = (r × bl-i-1) mod m

Where:

  • r: ranking of the character
  • l: length of the pattern
  • i: index of the character in the pattern

For Ptrn = "BCD":

h(Ptrn) = ((4 × 10²) + (5 × 10¹) + (6 × 10⁰)) mod 13
= (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.

h(XZY) = ((7 × 10²) + (8 × 10¹) + (9 × 10⁰)) mod 13
= (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:

New Hash = ((Old Hash - Old Character × bl-1) × b + New Character) mod m

Remove 'X', add 'A', and update the hash. Continue this process for each new substring until a match is found.

Comments

Popular posts from this blog

Analysis of algorithms viva questions / Interview questions - set1 /sorting algorithms

Operating System Viva questions/interview questions 2025

Recommendation System viva questions/ Interview questions