Questions tagged [rabin-karp]

The Rabin-Karp string matching algorithm is a string matching algorithm that employs a rolling hash function to speed up the search.

Wiki

In computer science, the Rabin–Karp algorithm is a string searching algorithm. For a text of length n and p patterns of combined length m, its average and best case running time is O(n+m) in space O(p), but its worst-case time is O(nm).

Algorithm pseudocode

function RabinKarp(string s[1..n], string sub[1..m])
  hsub := hash(sub[1..m]);  hs := hash(s[1..m])        
  for i from 1 to n-m+1
    if hs = hsub
      if s[i..i+m-1] = sub
        return i
    hs := hash(s[i+1..i+m])
  return not found

Tag usage

The tag can be used for programming related problems in implementing Rabin-Karp algorithm in any programming language. Please avoid theoretical and conceptual questions on StackOverflow using the tag .

Read more

107 questions
3
votes
1 answer

Rabin-Karp not working for large primes (gives wrong output)

So I was solving this problem (Rabin Karp's algorithm) and wrote this solution: private static void searchPattern(String text, String pattern) { int txt_len = text.length(), pat_len = pattern.length(); int hash_pat = 0, hash_txt = 0; // hash…
Yashv
  • 33
  • 5
3
votes
1 answer

Rabin fingerprinting tables

I've been researching rabin fingerprinting for the past couple of days. While the general idea is simple enough I'm having significant troubles understanding the implementations that are circulating around the net. In particular all of them seem to…
lrd dsk
  • 31
  • 1
3
votes
3 answers

What are the available string matching algorithms besides Knuth-Morris-Pratt, Rabin-Karp and likes of it?

What are the available string matching algorithms besides Knuth-Morris-Pratt, Rabin-Karp and likes of it?
BGV
  • 221
  • 1
  • 3
  • 5
3
votes
0 answers

Rabin Karp for multiple Pattern different length

I know Rabin Karp is faster for multiple Pattern but it should be same length. But now i have 10 Pattern different length and need to search in text. How it work effective ? Thanks!
3
votes
1 answer

How to choose modulus value in Rabin-Karp algorithm?

I have a question about choosing the q and d in Rabin-Karp algorithm for searching strings. The algorithm uses the values q as modulus and d as hash function. If I choose q as number of power 2 and d=q-1 or d=q+1 How can these choices affect…
prilia
  • 996
  • 5
  • 18
  • 41
3
votes
2 answers

Slicing a file with rabin karp algorithm

I've written a c program that's supposed to slice a file into chunks with Rabin Karp algorithm. This is an adaptation of a c# program that you can find Here. It seems to work, but a problem remains. average chunks size is not what is expected. Usage…
2
votes
1 answer

Implementation of Rabin-Karp Algorithm with PHP

Hi I am writing a PHP class to implement Rabin-Karp algorithm. I have issue with re-hashing part. This code doesn't include matching part of the characters. I had to stop since it never matching hash codes due to the issue with re-hashing. Someone…
Prasad Rajapaksha
  • 6,118
  • 10
  • 36
  • 52
2
votes
1 answer

Rabin Karp Implementation too slow in Ruby

I have been working on a small Plagiarism detection engine which uses Idea from MOSS. I need a Rolling Hash function, I am inspired from Rabin-Karp Algorithm. Code I wrote --> #!/usr/bin/env ruby #Designing a rolling hash function. #Inspired from…
Nitish Upreti
  • 6,312
  • 9
  • 50
  • 92
2
votes
1 answer

Cormen String matching Rabin-Karp

I am reading Rabin-Karp algorithm by Introduction to Algorithms by Cormen etc. www.cs.uml.edu/~kdaniels/courses/ALG_503_F08/503_lecture11.ppt Note here == is used as mod operator Above notes on slide 13 i.e., Eq 34.2, which is attached as picture…
venkysmarty
  • 11,099
  • 25
  • 101
  • 184
2
votes
1 answer

Rabin-Karp string matching algorithm by rolling hash

Here is an implementation of Rabin-Karp String matching algorithm in C#... static void Main(string[] args) { string A = "String that contains a pattern."; string B = "pattern"; ulong siga = 0; ulong sigb = 0; ulong Q = 100007; ulong D =…
Rdx
  • 195
  • 4
  • 11
2
votes
3 answers

Rabin-Karp string search algorithm

My previous question pertained to the general string search algorithm. I am researching the Rabin-Karp algorithm and I have a function template like: RabinKarpMatch(char *Text, char *Search_phrase,int radix,int prime) I wanted to know how the…
Prasanth Madhavan
  • 12,657
  • 15
  • 62
  • 94
2
votes
1 answer

C++ strange results - brute force is quicker than Rabin-Karp...?

Currently working on a string search program for a uni module, and I've managed to implement the algorithms successfully, at least to the point where they're finding the string consistently. I implemented Boyer Moore and Rabin Karp. I also threw in…
2
votes
1 answer

Rolling hash overflow/negative result protection

This question is very similar to rolling-hash, but there are some specifics regarding overflow/negative result which are still not clear to me. I have as well checked out this Rabin-Karp implementation and have issues with the line bellow: txtHash =…
John
  • 5,189
  • 2
  • 38
  • 62
2
votes
0 answers

String matched for some cases and not for other using rabin karp algorithm?

// n -> length of the text // m -> length of the pattern void rabin_karp_check(int n, int m){ int h_p = hash_value(pattern, m, 0); int h_t = hash_value(text, m, 0); int x = 0; int i = 0,k; while(i…