Search code examples
algorithmrabin-karp

What does "h" represent in rabin karp algorithm?


t(s+1) = (d*(t(s) -T[s+1]h) + T[s+m+1])mod q

d is the size of alphabet
T[1...n] is the text to be searched
P[1...m] is the pattern (m is the size of pattern)
q is a prime number

h = d^m-1 (mod q) is the value of digit "1" in the higher order position of an m-digit text window.

What does this line mean ? What does h represent?


Solution

  • You should first look at an easier case where d is 10 and the text only contains numbers between 0 and 9.

    h is value you use for shifting left the high-order digit. For example, let's assume that m is 3 and T = 2345. From 234, you can calculate 345 as follows:

    345 = 10*(234 - 2*100) + 5
    

    You can see in this case h = 100 is used for shifting 2 by 2 digits before subtracting it from 234. Note that the value of h is h = 103-1

    Now, you can generalize the idea for any d, and get h = dm-1.

    Then, by doing modulo operations, you just add mod q every time you calculate any values.