VOOZH about

URL: https://www.geeksforgeeks.org/java/implementing-rabin-karp-algorithm-using-rolling-hash-in-java/

⇱ Implementing Rabin Karp Algorithm Using Rolling Hash in Java - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Implementing Rabin Karp Algorithm Using Rolling Hash in Java

Last Updated : 2 Feb, 2022

There are so many pattern searching algorithms for the string. KMP algorithm, Z algorithm Rabin Karp algorithm, etc these algorithms are the optimization of Naive Pattern searching Algorithm.

Naive Pattern Searching Algorithm:

Input : "AABACACAACAC"
Pattern : "CAC"
Output : [4,9]

AABACACAACAC

Implementation:


Output
Pattern is found at the indices : 4 , 9 , 

Output explanation:

The above algorithm for pattern searching is the basic algorithm the worst as the average time complexity of this algorithm is O(n×m) where n is the pattern and m is the given string. 

How can we reduce the complexity of this algorithm? 


It is possible with the help of rolling hash. Rabin Karp algorithm is one of the optimized algorithms of the naive algorithm that performs searching by rolling over the string and search the pattern.


 

👁 Image


 

Illustration: 

Input: txt[] = "THIS IS A TEST TEXT"
 pat[] = "TEST"
Output: Pattern found at index 10

Input: txt[] = "AABAACAADAAB"
 pat[] = "AABA"
Output: Pattern found at index 0
 Pattern found at index 9
 Pattern found at index 12

Procedure:

  • Calculate the hash value of the pattern (By creating your own hash function or equation to determining an individual hash value for every character)
  • Iterate over the string check the hash value of every substring generated of the size of the pattern if matched check every character of the pattern as well as String if all matched print the starting index of the string.
  • If not matched shift to the next character by skipping the first character and adding the hash value of the next character that we don't calculate the hash value of the next substring in the string only we slide the window skip the first character and add the last character in the window by calculating its hash value i.e Rolling hash.


 Implementation: 

Simple Rolling algorithm assuming the pattern of length 2

  1. Initialize temp=4(1+3)
  2. Roll the hash value to the next element.
  3. Iterate the loop 'i' <= Array.length-pattern.length
  4. Remove the first element from the temp variable and add next element in the temp variable. temp = temp-Array[i]+Array[i.pattern.length()]

Output
0
9
12


 

Comment
Article Tags:
Article Tags: