mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-08 10:15:51 +08:00
code-clean-up (#4519)
* code-clean-up * style: make `RabinKarpAlgorithm` a proper utility class --------- Co-authored-by: arintripathi1 <arint@trainee.nrifintech.com> Co-authored-by: vil02 <vil02@o2.pl> Co-authored-by: Piotr Idzik <65706193+vil02@users.noreply.github.com>
This commit is contained in:
@ -1,50 +1,48 @@
|
|||||||
package com.thealgorithms.searches;
|
package com.thealgorithms.searches;
|
||||||
|
|
||||||
// Following program is a Java implementation
|
// Implementation of Rabin Karp Algorithm
|
||||||
// of Rabin Karp Algorithm given in the CLRS book
|
|
||||||
|
|
||||||
public class RabinKarpAlgorithm {
|
public final class RabinKarpAlgorithm {
|
||||||
|
private RabinKarpAlgorithm() {
|
||||||
|
}
|
||||||
|
|
||||||
// d is the number of characters in the input alphabet
|
// d is the number of characters in the input alphabet
|
||||||
public static final int d = 256;
|
private static final int d = 256;
|
||||||
|
|
||||||
/* pat -> pattern
|
public static int search(String pattern, String text, int primeNumber) {
|
||||||
txt -> text
|
|
||||||
q -> A prime number
|
int index = -1; // -1 here represents not found
|
||||||
*/
|
int patternLength = pattern.length();
|
||||||
public int search(String pat, String txt, int q) {
|
int textLength = text.length();
|
||||||
int index = -1; // note: -1 here represent not found, it is not an index
|
int hashForPattern = 0;
|
||||||
int M = pat.length();
|
int hashForText = 0;
|
||||||
int N = txt.length();
|
|
||||||
int i, j;
|
|
||||||
int p = 0; // hash value for pattern
|
|
||||||
int t = 0; // hash value for txt
|
|
||||||
int h = 1;
|
int h = 1;
|
||||||
|
|
||||||
// The value of h would be "pow(d, M-1)%q"
|
// The value of h would be "pow(d, patternLength-1)%primeNumber"
|
||||||
for (i = 0; i < M - 1; i++) h = (h * d) % q;
|
for (int i = 0; i < patternLength - 1; i++) h = (h * d) % primeNumber;
|
||||||
|
|
||||||
// Calculate the hash value of pattern and first
|
// Calculate the hash value of pattern and first
|
||||||
// window of text
|
// window of text
|
||||||
for (i = 0; i < M; i++) {
|
for (int i = 0; i < patternLength; i++) {
|
||||||
p = (d * p + pat.charAt(i)) % q;
|
hashForPattern = (d * hashForPattern + pattern.charAt(i)) % primeNumber;
|
||||||
t = (d * t + txt.charAt(i)) % q;
|
hashForText = (d * hashForText + text.charAt(i)) % primeNumber;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Slide the pattern over text one by one
|
// Slide the pattern over text one by one
|
||||||
for (i = 0; i <= N - M; i++) {
|
for (int i = 0; i <= textLength - patternLength; i++) {
|
||||||
// Check the hash values of current window of text
|
/* Check the hash values of current window of text
|
||||||
// and pattern. If the hash values match then only
|
and pattern. If the hash values match then only
|
||||||
// check for characters one by one
|
check for characters one by one*/
|
||||||
if (p == t) {
|
|
||||||
|
int j = 0;
|
||||||
|
if (hashForPattern == hashForText) {
|
||||||
/* Check for characters one by one */
|
/* Check for characters one by one */
|
||||||
for (j = 0; j < M; j++) {
|
for (j = 0; j < patternLength; j++) {
|
||||||
if (txt.charAt(i + j) != pat.charAt(j)) break;
|
if (text.charAt(i + j) != pattern.charAt(j)) break;
|
||||||
}
|
}
|
||||||
|
|
||||||
// if p == t and pat[0...M-1] = txt[i, i+1, ...i+M-1]
|
// if hashForPattern == hashForText and pattern[0...patternLength-1] = text[i, i+1, ...i+patternLength-1]
|
||||||
if (j == M) {
|
if (j == patternLength) {
|
||||||
System.out.println("Pattern found at index " + i);
|
|
||||||
index = i;
|
index = i;
|
||||||
return index;
|
return index;
|
||||||
}
|
}
|
||||||
@ -52,12 +50,11 @@ public class RabinKarpAlgorithm {
|
|||||||
|
|
||||||
// Calculate hash value for next window of text: Remove
|
// Calculate hash value for next window of text: Remove
|
||||||
// leading digit, add trailing digit
|
// leading digit, add trailing digit
|
||||||
if (i < N - M) {
|
if (i < textLength - patternLength) {
|
||||||
t = (d * (t - txt.charAt(i) * h) + txt.charAt(i + M)) % q;
|
hashForText = (d * (hashForText - text.charAt(i) * h) + text.charAt(i + patternLength)) % primeNumber;
|
||||||
|
|
||||||
// We might get negative value of t, converting it
|
// handling negative hashForText
|
||||||
// to positive
|
if (hashForText < 0) hashForText = (hashForText + primeNumber);
|
||||||
if (t < 0) t = (t + q);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return index; // return -1 if pattern does not found
|
return index; // return -1 if pattern does not found
|
||||||
|
@ -6,12 +6,11 @@ import org.junit.jupiter.params.ParameterizedTest;
|
|||||||
import org.junit.jupiter.params.provider.CsvSource;
|
import org.junit.jupiter.params.provider.CsvSource;
|
||||||
|
|
||||||
class RabinKarpAlgorithmTest {
|
class RabinKarpAlgorithmTest {
|
||||||
RabinKarpAlgorithm RKA = new RabinKarpAlgorithm();
|
|
||||||
|
|
||||||
@ParameterizedTest
|
@ParameterizedTest
|
||||||
@CsvSource({"This is an example for rabin karp algorithmn, algorithmn, 101", "AAABBDDG, AAA, 137", "AAABBCCBB, BBCC, 101", "AAABBCCBB, BBCC, 131", "AAAABBBBCCC, CCC, 41", "ABCBCBCAAB, AADB, 293", "Algorithm The Algorithm, Algorithm, 101"})
|
@CsvSource({"This is an example for rabin karp algorithmn, algorithmn, 101", "AAABBDDG, AAA, 137", "AAABBCCBB, BBCC, 101", "AAABBCCBB, BBCC, 131", "AAAABBBBCCC, CCC, 41", "ABCBCBCAAB, AADB, 293", "Algorithm The Algorithm, Algorithm, 101"})
|
||||||
void RabinKarpAlgorithmTestExample(String txt, String pat, int q) {
|
void RabinKarpAlgorithmTestExample(String txt, String pat, int q) {
|
||||||
int indexFromOurAlgorithm = RKA.search(pat, txt, q);
|
int indexFromOurAlgorithm = RabinKarpAlgorithm.search(pat, txt, q);
|
||||||
int indexFromLinearSearch = txt.indexOf(pat);
|
int indexFromLinearSearch = txt.indexOf(pat);
|
||||||
assertEquals(indexFromOurAlgorithm, indexFromLinearSearch);
|
assertEquals(indexFromOurAlgorithm, indexFromLinearSearch);
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user