-3

I understand the following in the Rabin-Karp algorithm

h=d^m-1 but I fail to understand why we write

for(i=0;i<M-1;i++)
    h=(h*d)%q

in the code

greybeard
  • 2,249
  • 8
  • 30
  • 66
poornima
  • 25
  • 1
  • 3

2 Answers2

0

basically , these lines are initialising default values to your hashmaps , which you are going to change later ,and it also work as a hash function , when you will insert values , for more deep ,read below paragragh , also vote and follow my profile . When you were trying to understand concept of hashmaps , everyone teaches you by making some block in series . later , these blocks were initialised some key values but the block were uninitialised before you insert key values . so there may be chances that some blocks were not given any key values . they will contain garbage values , and how can you traverse over them becoz when you need to search something, you need to traverse , so that function is initialising a default value to all blocks . now if you try to run your code without these lines , your code will check for 1st time only . as i know , these code lines are from geeksforgeeks rabinkarp algorithm , try running that code without those line and you wil get checked for 1st value only and not get desired answer

  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Sep 29 '21 at 12:34
0

To be precise, the loop is not calculating ℎ=−1, but ℎ=(−1) mod . This is an important detail, because that means whatever the value of or is, the result will never exceed .

Also, the code block you presented needs to be preceded with h=1 to make sense.

With a loop we can avoid large intermediate results, which could risk crossing the range supported by the integer type being used. Typically the size of the string alphabet () is set to 256. Now if the length of the pattern to search () is like 20, then = 25620 = 2160, a number that does not fit in the typical 32 or 64-bit integer storage. So calculating −1 just like that risks to lead to numeric overflow.

That is why the loop is used. At each iteration the modulo operator is applied, which with normal values of and eliminates the risk of overflow. It is also correct to apply the modulo operator at each iteration.

trincot
  • 317,000
  • 35
  • 244
  • 286