double hashing with collision on first and second hash functions-java

user4060080 picture user4060080 · Nov 12, 2014 · Viewed 8.9k times · Source

For double hashing, if there is a collision with the first hash function, you'd use the second hash function, but what if there is still a collision? For example, let's say a hash table is size 15 and the hash function is (key + 3) % 15 and the second hash function is ((key % 8) / 3) + 2. Let's say "insert 59" goes to index 2 by the first hash function but there already is a key there. The second hash function would bring it to 3 but let's say there already is a value there too. Where would 59 be inserted on the hash table and why? Thanks

I specifically want to use double hashing, not chained hashing or linear probing.

Answer

user3436096 picture user3436096 · Dec 1, 2014

It is not the way we calculate the second hash function, since for every probe(unavailability of slot) you need to have a new hash function and it is not feasible.

Generic Second Hash function would be of type,

H1(x) - first hash function, H2(x) - second hash function

First time we try for following slot - H1(x),

next probe would be - H1(x)+H2(x),

next probe H1(x)+2*H2(x) ........ H1(x)+n*H2(x)