Special-purpose hash functions

77 Views Asked by At

I am trying to create a special purpose hash function that will have as few collisions as possible.

$99\%$ of the input will be sequential numbers, from $1$ to $N$. The size of the hash table will be $\frac{N}{10}$.

Can anyone provide the perfect hash function for this input?

2

There are 2 best solutions below

0
On BEST ANSWER

If perfect means as few collisions as possible, you can just do $f(n)=(n \pmod N)/10$ where the divide is integer division. You have to have $10$ of each number in the range $[1,N]$ mapped to each hash value, which this does.

Often we also ask that hash functions be such that one cannot reasonably predict the hash from the number to be hashed, nor invert it to find a number that will match a given hash. If you want that as well, just take any current cryptographic hash function (which will presumably return a number much larger than $\frac N{10}$), then take the output $\bmod \frac N{10}$

0
On

Can anyone provide the perfect hash function for this input?

Since your output space is smaller than the input, a perfect hash function isn't possible.

99% of the input will be sequential numbers

A hash function doesn't care about the order.

If you talk about memory and inserting values sequentially, you might want to use a different algorithm to retrieve these values.

The worst case scenario of a Hash function is still O(n). While a binary search for example is only O(log n).