Questions tagged [rolling-hash]
17 questions
15
votes
2 answers
Why is the base used to compute hashes in Rabin–Karp always primes?
The Rabin–Karp string matching algorithm requires a hash function which can be computed quickly. A common choice is
$$ h(x_0\ldots x_n) = \sum_{i=0}^n b^i x_i, $$
where $b$ is prime (all computations are module $2^w$, where $w$ is the width of a…
Saurabh Jain
- 291
- 2
- 7
10
votes
1 answer
Does the Rabin-Karp really need me to care about applying a mod Q operation on the rolling hashes?
I have been reading about the Rabin Karp algorithm and I kept wondering what's the big deal with keeping our rolling hashes values bounded by a value Q?
I'd thought that as our integer number representation on the typical computer is 2-complement,…
devoured elysium
- 600
- 4
- 14
7
votes
1 answer
Find longest common substring using a rolling hash
The longest common substring (LCS) of two input strings $s,t$ is a common substring (in both of them) of maximum length. We can relax the constraints to generalize the problem: find a common substring of length $k$. We can then use binary search to…
mrk
- 3,748
- 23
- 35
4
votes
1 answer
Finding hash of a substring $[i, j]$ in $O(1)$ using $O(|S|)$ pre computation
Given a string $S$ of length $n$ characters, is it possible to calculate the hash of its substring $[i, j]$ (from index $i$ to $j$, both inclusive) in $O(1)$ using some form of precomputation? Can we use a modification of the rolling hash?
The…
Kyuubi
- 273
- 3
- 9
3
votes
3 answers
By what criteria is the base value selected in Rabin Karp algorithm?
In the Rabin Karp algorithm the rolling hash is calculated as follows:
H1= c1*a^k-1 + c2*a^k-2+c3*a^k-3+…+ck*a^0
where a is a constant. On what basis is this a selected? In Cormen they have used a value 10 and at some other places it is 26. By…
Navjot Singh
- 1,215
- 1
- 9
- 26
3
votes
1 answer
pattern search with k-mismatch
I have used Rabin Karp Rolling Hash for searching a pattern $P$ in a text $T$. Now I am allowing $k$-mismatches, but not able to do a faster implementation.
I tried modifying RK algorithm by splitting the pattern into few blocks, but that does not…
CPP_NEW
- 31
- 3
2
votes
3 answers
How does Rabin fingerprint finds breakpoints (chunk boundary)?
I was reading up on chunking and found Rabin fingerprinting is also used to break files into chunks. After reading about this algorithm, I understood how rolling hash is computed using Rabin fingerprint. This is explained almost everywhere. What I…
user270386
- 121
- 1
- 3
2
votes
1 answer
Repeated fingerprinting after array updates
I've got a microprocessor and want to quickly identify the settings of my application (stored in some eeprom regions) via a fingerprint instead of having to dump the entire memory every time.
So I was going to compute a 32-bit hash from the few…
Bergi
- 610
- 3
- 12
2
votes
1 answer
Understanding Rabin-Karp's rolling hash computation
Possibly related to this. Let $T$ be the text and $n$ be the length of the pattern. I understand that if substrings of $T$ are interpreted as base-$d$ numbers where $d$ is the alphabet's size, then for all suitable $k$:
$$T[k+1\text{..}k+n] =…
giofrida
- 195
- 6
2
votes
2 answers
Rolling hash hacking
The hash value of a string $s$ is given by
$$
h(s) = \sum^{|s|}_{i = 1} s_i \cdot p^{|s| - i} \mod m; \text{ $m$ is prime, $m < 10^{12}$}.
$$
The string $s$, $p$, $m$ is given,
$|s| \le 14$, alphabet size $= 62$ (letters and digits).
I need…
zdikov
- 21
- 1
1
vote
0 answers
Rolling Hash calculation with Horner's method
I understood how Horner's method reduces the complexity(number of operations) while evaluating a polynomial.
I have a character array derived from a string
String s = "hi how are you"
char[] array = {'h' , 'i', ' ', 'h', 'o', 'w', ' ', 'a',…
Arun Rahul
- 171
- 2
- 3
1
vote
2 answers
modular arithmetic in rolling hash algorithm
My question was around the rolling hash function in RobinKarp algorithm. It is intuitive for me to understand how to get from xi to xi+1 without the mod, however with the mod i am not sure how we still get the right answer. Is there any mathematical…
noi.m
- 135
- 3
1
vote
1 answer
Understanding Polynomial Rolling Hash Function by Modular Arithmetic
I was learning the Polynomial Hash function in python, the one used in Rabin Karp Algorithm
This is the implementation I was taught:
def GivenHash(S,p=113,x=10):
hash = 0
for i in range(len(S)-1,-1,-1):
hash = (hash*x + S[i]) % p
return…
reverseambition
- 13
- 3
1
vote
2 answers
How to use a worst case scenario in Rolling Hash: Rabin Karp Algorithm when the given string only contains the occurrences of "a and b?"
Issue: I am having a hard time figuring out how to use the worst case for Rolling Hash, especially if the occurrences are only "a and b" for the string. Not only that but it is a bit of a hassle to figure out the process if the pattern that we tried…
Ralph Henry
- 13
- 2
1
vote
1 answer
finding all cyclic substrings of a string
I have been stuck on this problem for a while now; any help would be appreciated.
Given a string S, find the number of distinct substrings which are composed of 2 or more consecutive strings. For example, "abbccbbadad" has 3 because "bb" is composed…
pblpbl
- 53
- 3