In Cormen's book "Introduction to algorithms" I read that double hashing (in open addressing) function is in the form of:
h(k, i) = (h1(k) + i * h2(k)) mod m
where k is a key, i is a next index in the case of a collision, m is the table length and hX are hash functions.
He says that the main problem in double hashing is to utilize all indices in the table. To solve that problem we should set m to the power of 2 and h2 function should return odd values. Why (I can't see him explaining it)?
The general rule is that modulo
m, addingh_2(k)repeatedly is a cycle that repeats with periodm/GCD(m, h_2(k)). If there are no common factors betweenmandh_2(k)then it will repeat with periodmwhich means that you can reach allmindices. So you want no common factors.The "no common factors" rule is easily satisfied by making
ma power of 2 andh_2(k)odd.