V
V
Victor2015-03-20 02:50:15
Java
Victor, 2015-03-20 02:50:15

Uniformly distributed hash function. How to use all bits of the hash code?

Good day.
In Sedgwick's book Algorithms in Java, I came across an interesting paragraph. It is about creating a uniformly distributed modular hash function.
So there is an array size M, the maximum value of int:

int hash(Key key) {
// То что в скобках превращает 32-битное число в неотрицательное 31-битное
return (key.hashCode() & 0xfffffff) % mod;
}

Why modular hashing is understandable, the module is taken from the hash so that you can address the index in the array. But there is one big BUT that is not clear to me.
Why does the size of the array have to be a prime number to, as the author pointed out, " use all the bits of the hash code "?
How can prime numbers help?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Mrrl, 2015-03-20
@davinctor

A simple module was invented in those days (and for those situations) when just the address of an object was taken as a hash code. Since the address is often divisible by 4 (or 8, or 16*k+4, as in Borland-C on 8086), an even array length would result in a very unequal distribution.
The second (no less important) reason is that internal collision resolution is often used: in this case, each "basket" contains 0 or 1 element of the set (no lists!). If the cell M, where you want to put the element, is already occupied, then calculate some step K=f(M), and check the cells (M+K)%L, (M+2*K)%L, ..., until find a free one. For this sequence to go through the entire array, K and L must be coprime. The function f has to be evaluated frequently, so the mutual simplicity is guaranteed by taking L as a prime number.

B
bobrovskyserg, 2015-03-20
@bobrovskyserg

Because a prime number is coprime to any other (except multiples).
If, for example, the size of the array were even, then the hashes of all even numbers would be even.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question