From cfdb8c546225483e2a5160a278b55e304be14181 Mon Sep 17 00:00:00 2001 From: tackhwa <55059307+tackhwa@users.noreply.github.com> Date: Sun, 31 Jul 2022 00:30:06 +0800 Subject: [PATCH 1/5] Create a new Rabin-Karp Algorithm This is a Rabin-Karp Algorithm for Pattern Searching --- .../thealgorithms/searches/RabinKarpSearch | 84 +++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 src/main/java/com/thealgorithms/searches/RabinKarpSearch diff --git a/src/main/java/com/thealgorithms/searches/RabinKarpSearch b/src/main/java/com/thealgorithms/searches/RabinKarpSearch new file mode 100644 index 000000000000..684f14e85dd9 --- /dev/null +++ b/src/main/java/com/thealgorithms/searches/RabinKarpSearch @@ -0,0 +1,84 @@ +package com.thealgorithms.searches; +// Following program is a Java implementation +// of Rabin Karp Algorithm given in the CLRS book + +public class RabinKarpAlgorithm +{ + // d is the number of characters in the input alphabet + public final static int d = 256; + + /* pat -> pattern + txt -> text + q -> A prime number + */ + static void search(String pat, String txt, int q) + { + int M = pat.length(); + int N = txt.length(); + int i, j; + int p = 0; // hash value for pattern + int t = 0; // hash value for txt + int h = 1; + + // The value of h would be "pow(d, M-1)%q" + for (i = 0; i < M-1; i++) + h = (h*d)%q; + + // Calculate the hash value of pattern and first + // window of text + for (i = 0; i < M; i++) + { + p = (d*p + pat.charAt(i))%q; + t = (d*t + txt.charAt(i))%q; + } + + // Slide the pattern over text one by one + for (i = 0; i <= N - M; i++) + { + + // Check the hash values of current window of text + // and pattern. If the hash values match then only + // check for characters one by one + if ( p == t ) + { + /* Check for characters one by one */ + for (j = 0; j < M; j++) + { + if (txt.charAt(i+j) != pat.charAt(j)) + break; + } + + // if p == t and pat[0...M-1] = txt[i, i+1, ...i+M-1] + if (j == M) + System.out.println("Pattern found at index " + i); + } + + // Calculate hash value for next window of text: Remove + // leading digit, add trailing digit + if ( i < N-M ) + { + t = (d*(t - txt.charAt(i)*h) + txt.charAt(i+M))%q; + + // We might get negative value of t, converting it + // to positive + if (t < 0) + t = (t + q); + } + } + } + + /* Driver Code */ + public static void main(String[] args) + { + String txt = "This is an example for rabin karp algorithmn"; + String pat = "for"; + + // A prime number + int q = 101; + + // Function Call + search(pat, txt, q); + } +} + +// This code is contributed by nuclode From e6769c95f413847d82a933bd99518eeb986e751a Mon Sep 17 00:00:00 2001 From: tackhwa <55059307+tackhwa@users.noreply.github.com> Date: Thu, 4 Aug 2022 00:09:27 +0800 Subject: [PATCH 2/5] Update and rename RabinKarpSearch to RabinKarpAlgorithm.java --- ...abinKarpSearch => RabinKarpAlgorithm.java} | 21 +++++++------------ 1 file changed, 7 insertions(+), 14 deletions(-) rename src/main/java/com/thealgorithms/searches/{RabinKarpSearch => RabinKarpAlgorithm.java} (84%) diff --git a/src/main/java/com/thealgorithms/searches/RabinKarpSearch b/src/main/java/com/thealgorithms/searches/RabinKarpAlgorithm.java similarity index 84% rename from src/main/java/com/thealgorithms/searches/RabinKarpSearch rename to src/main/java/com/thealgorithms/searches/RabinKarpAlgorithm.java index 684f14e85dd9..1da9bbc1f477 100644 --- a/src/main/java/com/thealgorithms/searches/RabinKarpSearch +++ b/src/main/java/com/thealgorithms/searches/RabinKarpAlgorithm.java @@ -11,8 +11,9 @@ public class RabinKarpAlgorithm txt -> text q -> A prime number */ - static void search(String pat, String txt, int q) + public int search(String pat, String txt, int q) { + int index = -1; //note: -1 here represent not found, it is not an index int M = pat.length(); int N = txt.length(); int i, j; @@ -49,8 +50,11 @@ static void search(String pat, String txt, int q) } // if p == t and pat[0...M-1] = txt[i, i+1, ...i+M-1] - if (j == M) + if (j == M) { System.out.println("Pattern found at index " + i); + index= i; + return index ; + } } // Calculate hash value for next window of text: Remove @@ -65,20 +69,9 @@ static void search(String pat, String txt, int q) t = (t + q); } } + return index; // return -1 if pattern does not found } - /* Driver Code */ - public static void main(String[] args) - { - String txt = "This is an example for rabin karp algorithmn"; - String pat = "for"; - - // A prime number - int q = 101; - - // Function Call - search(pat, txt, q); - } } // This code is contributed by nuclode From 18fbb0dc3d1301b94a883bf8d62ea2dafede34ee Mon Sep 17 00:00:00 2001 From: tackhwa <55059307+tackhwa@users.noreply.github.com> Date: Thu, 4 Aug 2022 00:17:47 +0800 Subject: [PATCH 3/5] JUnit test for Rabin Karp Algorithm Add a j-unit test for Rabin Karp Algorithm --- .../searches/RabinKarpAlgorithmTest | 85 +++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 src/test/java/com/thealgorithms/searches/RabinKarpAlgorithmTest diff --git a/src/test/java/com/thealgorithms/searches/RabinKarpAlgorithmTest b/src/test/java/com/thealgorithms/searches/RabinKarpAlgorithmTest new file mode 100644 index 000000000000..ab567988132c --- /dev/null +++ b/src/test/java/com/thealgorithms/searches/RabinKarpAlgorithmTest @@ -0,0 +1,85 @@ +package com.thealgorithms.searches; + +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.*; + + + + +class RabinKarpAlgorithmTest { + + + RabinKarpAlgorithm RKA= new RabinKarpAlgorithm(); + int q= 101; + + + + + @Test + // valid test case + public void RabinKarpAlgorithmTestExample() { + String txt = "This is an example for rabin karp algorithmn"; + String pat = "algorithmn"; + int value = RKA.search(pat, txt, q); + System.out.println(value); + assertEquals(value,34); + + } + + + + + @Test + // valid test case + public void RabinKarpAlgorithmTestFront() { + String txt = "AAABBDDG"; + String pat = "AAA"; + int value = RKA.search(pat, txt, q); + System.out.println(value); + assertEquals(value, 0); + + } + + + + + @Test + // valid test case + public void RabinKarpAlgorithmTestMiddle() { + String txt = "AAABBCCBB"; + String pat = "BBCC"; + int value = RKA.search(pat, txt, q); + System.out.println(value); + assertEquals(value, 3); + + } + + + + + @Test + // valid test case + public void RabinKarpAlgorithmTestLast() { + String txt = "AAAABBBBCCC"; + String pat = "CCC"; + int value = RKA.search(pat, txt, q); + System.out.println(value); + assertEquals(value, 8); + + } + + + + + @Test + // valid test case + public void RabinKarpAlgorithmTestNotFound() { + String txt = "ABCBCBCAAB"; + String pat = "AADB"; + int value = RKA.search(pat, txt, q); + System.out.println(value); + assertEquals(value, -1); + + } + +} From d57b635f0c3908a81d5f83d71b0cffad9184a2f3 Mon Sep 17 00:00:00 2001 From: tackhwa <55059307+tackhwa@users.noreply.github.com> Date: Thu, 4 Aug 2022 01:36:34 +0800 Subject: [PATCH 4/5] Update RabinKarpAlgorithmTest --- .../java/com/thealgorithms/searches/RabinKarpAlgorithmTest | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/test/java/com/thealgorithms/searches/RabinKarpAlgorithmTest b/src/test/java/com/thealgorithms/searches/RabinKarpAlgorithmTest index ab567988132c..528ad53938de 100644 --- a/src/test/java/com/thealgorithms/searches/RabinKarpAlgorithmTest +++ b/src/test/java/com/thealgorithms/searches/RabinKarpAlgorithmTest @@ -21,7 +21,6 @@ class RabinKarpAlgorithmTest { String txt = "This is an example for rabin karp algorithmn"; String pat = "algorithmn"; int value = RKA.search(pat, txt, q); - System.out.println(value); assertEquals(value,34); } @@ -35,7 +34,6 @@ class RabinKarpAlgorithmTest { String txt = "AAABBDDG"; String pat = "AAA"; int value = RKA.search(pat, txt, q); - System.out.println(value); assertEquals(value, 0); } @@ -49,7 +47,6 @@ class RabinKarpAlgorithmTest { String txt = "AAABBCCBB"; String pat = "BBCC"; int value = RKA.search(pat, txt, q); - System.out.println(value); assertEquals(value, 3); } @@ -63,7 +60,6 @@ class RabinKarpAlgorithmTest { String txt = "AAAABBBBCCC"; String pat = "CCC"; int value = RKA.search(pat, txt, q); - System.out.println(value); assertEquals(value, 8); } @@ -77,7 +73,6 @@ class RabinKarpAlgorithmTest { String txt = "ABCBCBCAAB"; String pat = "AADB"; int value = RKA.search(pat, txt, q); - System.out.println(value); assertEquals(value, -1); } From 303d3bb4f65ff58166eb83b69ef7e6f7bcb067ea Mon Sep 17 00:00:00 2001 From: tackhwa <55059307+tackhwa@users.noreply.github.com> Date: Fri, 5 Aug 2022 00:46:35 +0800 Subject: [PATCH 5/5] Update and rename RabinKarpAlgorithmTest to RabinKarpAlgorithmTest.java --- ...orithmTest => RabinKarpAlgorithmTest.java} | 28 ++----------------- 1 file changed, 3 insertions(+), 25 deletions(-) rename src/test/java/com/thealgorithms/searches/{RabinKarpAlgorithmTest => RabinKarpAlgorithmTest.java} (94%) diff --git a/src/test/java/com/thealgorithms/searches/RabinKarpAlgorithmTest b/src/test/java/com/thealgorithms/searches/RabinKarpAlgorithmTest.java similarity index 94% rename from src/test/java/com/thealgorithms/searches/RabinKarpAlgorithmTest rename to src/test/java/com/thealgorithms/searches/RabinKarpAlgorithmTest.java index 528ad53938de..0bc0ab1f7b3b 100644 --- a/src/test/java/com/thealgorithms/searches/RabinKarpAlgorithmTest +++ b/src/test/java/com/thealgorithms/searches/RabinKarpAlgorithmTest.java @@ -4,17 +4,12 @@ import static org.junit.jupiter.api.Assertions.*; - - class RabinKarpAlgorithmTest { RabinKarpAlgorithm RKA= new RabinKarpAlgorithm(); int q= 101; - - - - + @Test // valid test case public void RabinKarpAlgorithmTestExample() { @@ -22,11 +17,7 @@ public void RabinKarpAlgorithmTestExample() { String pat = "algorithmn"; int value = RKA.search(pat, txt, q); assertEquals(value,34); - } - - - @Test // valid test case @@ -35,12 +26,8 @@ public void RabinKarpAlgorithmTestFront() { String pat = "AAA"; int value = RKA.search(pat, txt, q); assertEquals(value, 0); - } - - - @Test // valid test case public void RabinKarpAlgorithmTestMiddle() { @@ -48,12 +35,8 @@ public void RabinKarpAlgorithmTestMiddle() { String pat = "BBCC"; int value = RKA.search(pat, txt, q); assertEquals(value, 3); - } - - - - + @Test // valid test case public void RabinKarpAlgorithmTestLast() { @@ -61,12 +44,8 @@ public void RabinKarpAlgorithmTestLast() { String pat = "CCC"; int value = RKA.search(pat, txt, q); assertEquals(value, 8); - } - - - - + @Test // valid test case public void RabinKarpAlgorithmTestNotFound() { @@ -74,7 +53,6 @@ public void RabinKarpAlgorithmTestNotFound() { String pat = "AADB"; int value = RKA.search(pat, txt, q); assertEquals(value, -1); - } }