Search code examples
rabin-karp

Karp-Rabin algorithm


The below image is from : 6.006-Introduction to algorithms,

enter image description here

While doing the course 6.006-Introduction to algorithms, provided by MIT OCW, I came across the Rabin-Karp algorithm.

Can anyone help me understand as to why the first rs()==rt() required? If it’s used, then shouldn’t we also check first by brute force whether the strings are equal and then move ahead? Why is it that we are not considering equality of strings when hashing is done from t[0] and then trying to find other string matches?

In the image, rs() is for hash value, and rs.skip[arg] is to remove the first character of that string assuming it is ‘arg’


Solution

  • Can anyone help me understand as to why the first rs()==rt() required?

    I assume you mean the one right before the range-loop. If the strings have the same length, then the range-loop will not run (empty range). The check is necessary to cover that case.

    If it’s used, then shouldn’t we also check first by brute force whether the strings are equal and then move ahead?

    Not sure what you mean here. The posted code leaves blank (with ...) after matching hashes are found. Let's not forget that at that point, we must compare strings to confirm we really found a match. And, it's up to the (not shown) implementation to continue searching until the end or not.

    Why is it that we are not considering equality of strings when hashing is done from t[0] and then trying to find other string matches?

    I really don't get this part. Note the first two loops are to populate the rolling hashes for the input strings. Then comes a check if we have a match at this point, and then the loop updating the rolling hashes pair wise, and then comparing them. The entire t is checked, from start to end.