Implementing Rabin Karp Algorithm Using Rolling Hash in Java
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
Pattern is found at the indicies : 4 , 9 ,
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.
Input: txt = "THIS IS A TEST TEXT" pat = "TEST" Output: Pattern found at index 10 Input: txt = "AABAACAADAABAABA" pat = "AABA" Output: Pattern found at index 0 Pattern found at index 9 Pattern found at index 12
- 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.
Simple Rolling algorithm assuming the pattern of length 2
- Initialize temp=4(1+3)
- Roll the hash value to the next element.
- Iterate the loop ‘i’ <= Array.length-pattern.length
- Remove the first element from the temp variable and add next element in the temp variable. temp = temp-Array[i]+Array[i.pattern.length()]
0 9 12