Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

perfect hash function

Tags:

I'm attempting to hash the values

10, 100, 32, 45, 58, 126, 3, 29, 200, 400, 0 

I need a function that will map them to an array that has a size of 13 without causing any collisions.

I've spent several hours thinking this over and googling and can't figure this out. I haven't come close to a viable solution.

How would I go about finding a hash function of this sort? I've played with gperf, but I don't really understand it and I couldn't get the results I was looking for.

like image 496
gregghz Avatar asked Nov 09 '10 06:11

gregghz


People also ask

Is there a perfect hash function?

A perfect hash function can be constructed that maps each of the keys to a distinct integer, with no collisions. These functions only work with the specific set of keys for which they were constructed. Passing an unknown key will result a false match or even crash. A minimal perfect hash function goes one step further.

What is a perfect hash in a hash table?

A perfect hash function is one that maps N keys to the range [1,R] without having any collisions. A minimal perfect hash function has a range of [1,N]. We say that the hash is minimal because it outputs the minimum range possible. The hash is perfect because we do not have to resolve any collisions.

How do you implement a perfect hash function?

Perfect hashing is implemented using two hash tables, one at each level. Each of the table uses universal hashing. The first level is the same a hashing with chaining such that n elements is hashed into m slots in the hash table. This is done using a has function selected from a universal family of hash functions.

What is a perfect hash function in the context of hashing?

So, in simple terms we can say that a hash function is used to transform a given key into a specific slot index. Its main job is to map each and every possible key into a unique slot index. If every key is mapped into a unique slot index, then the hash function is known as a perfect hash function.


2 Answers

if you know the exact keys then it is trivial to produce a perfect hash function -

int hash (int n) {   switch (n) {     case 10:   return 0;     case 100:  return 1;     case 32:   return 2;     // ...     default:   return -1;   } } 
like image 72
tobyodavies Avatar answered Oct 14 '22 13:10

tobyodavies


Found One

I tried a few things and found one semi-manually:

(n ^ 28) % 13 

The semi-manual part was the following ruby script that I used to test candidate functions with a range of parameters:

t = [10, 100, 32, 45, 58, 126, 3, 29, 200, 400, 0] (1..200).each do |i|   t2 = t.map { |e| (e ^ i) % 13 }   puts i if t2.uniq.length == t.length end 
like image 40
DigitalRoss Avatar answered Oct 14 '22 15:10

DigitalRoss