views:

50

answers:

1

With 1 good hash function, how can I create a static hash table for N distinct keys that is collision free? I know the keys in advance, so the hash table doesn't have to be resized (hence it is static).

Related question: will it always be possible to find a hash table with no collisions?

+1  A: 

With a static set, why would you need hashing at all? An indexed array should do just fine and you don't have to make it bigger than N.. For example, if you can have an incrementing ID for the objects and use that as the hash key, you get no collisions and effectively the structure you've created is an array and the lookup is at least as fast as with a hashtable.

Mikko Wilkman
I know the set of keys stored in the hash table ahead of time, but I don't know what keys the user will supply at runtime. The user may enter an invalid key that would cause the lookup to fail.
Matt Fichman
Ok then I guess you'd anyway need some mapping from the key to the array index so it might not work anyway. I'd just make sure the hashcode algorightm of the key is good and keep the table big enough to minimize collisions. Is it really critical to be 100% sure there's no collisions?
Mikko Wilkman