Implementing Rabin Karp Algorithm Using Rolling Hash in Java
Last Updated :
02 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:
Java
import java.io.*;
public class GFG {
static void search(String s, String pattern)
{
for ( int i = 0 ; i <= s.length() - pattern.length();
++i) {
int check = 1 ;
for ( int j = 0 ; j < pattern.length(); ++j) {
if (s.charAt(i + j) != pattern.charAt(j)) {
check = 0 ;
break ;
}
}
if (check == 1 ) {
System.out.print(i + " , " );
}
}
}
public static void main(String[] args)
{
String s = "AABACACAACAC" ;
String pattern = "CAC" ;
System.out.print(
"Pattern is found at the indices : " );
search(s, pattern);
}
}
|
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.
Illustration:
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
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
- 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()]
Java
import java.io.*;
class GFG {
static void search(String S, String pattern)
{
int hash1 = 0 ;
int hash2 = 0 ;
for ( int i = 0 ; i < pattern.length(); ++i) {
hash1 += pattern.charAt(i) - 'A' ;
hash2 += S.charAt(i) - 'A' ;
}
int j = 0 ;
for ( int i = 0 ; i <= S.length() - pattern.length();
++i) {
if (hash2 == hash1) {
for (j = 0 ; j < pattern.length(); ++j) {
if (pattern.charAt(j)
!= S.charAt(i + j)) {
break ;
}
}
}
if (j == pattern.length()) {
System.out.println(i);
}
if (i == S.length() - pattern.length())
break ;
hash2 = ( int )((hash2) - (S.charAt(i) - 'A' ))
+ S.charAt(i + pattern.length()) - 'A' ;
}
}
public static void main(String[] args)
{
String S = "AABAACAADAABAABA" ;
String pattern = "AABA" ;
search(S, pattern);
}
}
|
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...