How to write a perfect hash function for 36 strings?

379 views Asked by At

Using the numbers 0-5 I need to write a perfect has function for 36 Student ID Strings with six characters and 3 integers (e.g. BYPLOK120).

The hash function looks something like as follows:

String [] studentID = ...

is 36 unique Strings in the format (xxx/yyy/zzz) where x and y are three letters from the students last and first names, respectively. z are 3 random numbers.

int hashTableSize = 37;`

int [] hashValue = new int[9];`

int [] weights = {1,0,5,4,3,2,5,1,2};  

Weights is 9 numbers between 0-5. THIS IS THE PART I NEED TO FIGURE OUT - there are about 2 million combinations (5^9) and less than 10 will give you a perfect hash function.

for (int i = 0; i < hashTableSize; i++)
   for (int j = 0; j < studentID[I].length; j++)
       hashVal[i] += ( studentID[i].charAt(j) * weight[j] ) % hashTableSize;

I need the hash value of each string in the array to be unique (i.e. there are no collisions when inserting)

I had duplicate hash values as I could not find the unique array of weight integers.

0

There are 0 answers