HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

By what criteria is the base value selected in Rabin Karp algorithm?

Submitted by: @import:stackexchange-cs··
0
Viewed 0 times
thecriteriawhatrabinvaluealgorithmselectedkarpbase

Problem

In the Rabin Karp algorithm the rolling hash is calculated as follows:

H1= c1*a^k-1 + c2*a^k-2+c3*a^k-3+…+ck*a^0


where a is a constant. On what basis is this a selected? In Cormen they have used a value 10 and at some other places it is 26. By observation, when the string is of digits they have taken a = 10 and when the string is of English characters then they have taken a = 26. Is it not possible to compute the hash value using a = 10 for English characters. If not what flaws does it introduce or i am assuming it wrong. I would appreciate help.

Solution

You can use any value you want, but it is best to choose a value that is relatively prime to the modulus, as that reduces the number of hash collisions.

For efficiency you might want to choose a value that is small (like 3), as that may make some computations faster.

Context

StackExchange Computer Science Q#93002, answer score: 4

Revisions (0)

No revisions yet.