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
40
votes
1 answer

When to use Rabin-Karp or KMP algorithms?

I have generated an string using the following alphabet. {A,C,G,T}. And my string contains more than 10000 characters. I'm searching the following patterns in it. ATGGA TGGAC CCGT I have asked to use a string matching algorithm which has O(m+n)…
Sukeshini
  • 1,241
  • 2
  • 23
  • 48
17
votes
2 answers

Using Rabin-Karp to search for multiple patterns in a string

According to the wikipedia entry on Rabin-Karp string matching algorithm, it can be used to look for several different patterns in a string at the same time while still maintaining linear complexity. It is clear that this is easily done when all the…
MAK
  • 26,140
  • 11
  • 55
  • 86
16
votes
7 answers

Java indexOf function more efficient than Rabin-Karp? Search Efficiency of Text

I posed a question to Stackoverflow a few weeks ago about a creating an efficient algorithm to search for a pattern in a large chunk of text. Right now I am using the String function indexOf to do the search. One suggestion was to use…
Elliott
  • 5,523
  • 10
  • 48
  • 87
14
votes
3 answers

When is Rabin Karp more effective than KMP or Boyer-Moore?

I'm learning about string searching algorithms and understand how they work but haven't found a good enough answer about in which cases Rabin-Karp algorithm would be more effective than KMP or Boyer-Moore. I see that it is easier to implement and…
E.K
  • 315
  • 1
  • 3
  • 6
14
votes
2 answers

Rabin Karp string matching algorithm

I've seen this Rabin Karp string matching algorithm in the forums on the website and I'm interested in trying to implement it but I was wondering If anyone could tell me why the variables ulong Q and ulong D are 100007 and 256 respectively :S? What…
c grum
  • 141
  • 1
  • 3
13
votes
3 answers

Are there any working implementations of the rolling hash function used in the Rabin-Karp string search algorithm?

I'm looking to use a rolling hash function so I can take hashes of n-grams of a very large string. For example: "stackoverflow", broken up into 5 grams would be: "stack", "tacko", "ackov", "ckove", "kover", "overf", "verfl", "erflo",…
c14ppy
  • 247
  • 3
  • 6
12
votes
2 answers

Need help in understanding Rolling Hash computation in constant time for Rabin-Karp Implementation

I've been trying to implement Rabin-Karp algorithm in Java. I have hard time computing the rolling hash value in constant time. I've found one implementation at http://algs4.cs.princeton.edu/53substring/RabinKarp.java.html. Still I could not get how…
FourOfAKind
  • 2,298
  • 5
  • 31
  • 34
10
votes
2 answers

Understanding how rolling hash works with modulus in Rabin Karp algorithm

I am having trouble understanding how the rolling hash algorithm works after the hash has been reduced to modulus value by dividing by a prime number. Consider the sequence of 5 digits in the number 123456. The first chunk is 12345. I store the…
SexyBeast
  • 7,913
  • 28
  • 108
  • 196
8
votes
4 answers

What is the best hash function for Rabin-Karp algorithm?

I'm looking for an efficient hash function for Rabin-Karp algorithm. Here is my actual code (C programming language). static bool f2(char const *const s1, size_t const n1, char const *const s2, size_t const n2) { uintmax_t hsub =…
md5
  • 23,373
  • 3
  • 44
  • 93
8
votes
3 answers

Rabin-Karp Algorithm

I am interested in implementing the Rabin-Karp algorithm to search for sub strings as stated on wiki: http://en.wikipedia.org/wiki/Rabin-Karp_string_search_algorithm. Not for homework, but for self-interest. I have implemented the Rabin-Karp…
PinkDuck
  • 113
  • 1
  • 5
6
votes
1 answer

Rabin–Karp algorithm for plagiarism implementation by using rolling hash

i am using Rabin–Karp algorithm to check plagiarism for any two source code files so firstly i simply implement its algorithm in c # here its code but its average and best case running time is O(n+m) in space O(p), but its worst-case time is…
Rdx
  • 195
  • 4
  • 11
5
votes
2 answers

Rabin-Karp String Matching is not matching

I've been working on a Rabin-Karp string matching function in C++ and I'm not getting any results out of it. I have a feeling that I'm not computing some of the values correctly, but I don't know which one(s). Prototype void rabinKarp(string…
Madison S
  • 225
  • 1
  • 10
5
votes
2 answers

Can someone explain to me the Rabin-Karp algorithm's complexity?

I'm trying to understand why the worst case running time of the Rabin-Karp algorithm is O(nm) and the average case is O(n+m). Can someone help me with that?
3
votes
1 answer

Hashes generated by Rabin Karp Rolling Hash not reflecting on the Text

Note: Lots of Possible duplicates, but nothing seems to be solving my problem. I am working on a Plagiarism detection based on MOSS. After successfully implementing a filter which strips out all the necessary details(comments,punctuations etc) I…
Nitish Upreti
  • 6,312
  • 9
  • 50
  • 92
3
votes
1 answer

implementation of rolling hash does not help in String Matching with Rabin Karp

Apologies for this probable repeated question. I am trying to use rolling hash with Karp Rabin.I have looked a different implementations of rolling hash and I am wondering where I am going wrong. Though the text has the pattern the match using hash…
bhavs
  • 2,091
  • 8
  • 36
  • 66
1
2 3 4 5 6 7 8