0

we have a hash table with size 16, using double hashing method.

h1(x) = k mod 16

h2(x) = 2*(k mod 8)

I know that h2 hash function is bad, probably because mod 8 and times 2, but I don't know how to explain it. is there any explanation like "h2 hash function should mod prime or it will cause ____ problem "

1 Answers1

1

It is bad because it increases the number of collisions.

The (mod 8) means that you are only looking for 8 pigeonholes in your 16-pigeonhole table.

Multiplying it by 2 just spreads those 8 pigeonholes out so that you don’t have to search too many slots past the hashed index to find an empty hole...

You should always compute modulo the size of your table.

h(x) ::= x (mod N)   // where N is the table size

The purpose of making the table size a prime number just has to do with how powers of two are very common in computer science. If your data is random, then the size of the table doesn’t matter.

— As long as it is big enough for your expected load factor. A 16-element table is very small. You shouldn’t expect to store more than 6-12 random values in your table without a high-probability of collisions.

A very good linked thread is What is a good Hash Function?, which is totally worth a read just for the links to further reading alone.

Dúthomhas
  • 8,200
  • 2
  • 17
  • 39