Minimal Perfect hashing!

Hi!
I need an advice for building a perfect hash.I want to hash week days list

sunday
monday
tuesday
wednesday
thursday
friday
saturday

by using perfect hash.According to this example , letter table should be

Letter Value
f -8
m -4
s -9
t -7
w -6
y 3


so in hash table , list will look like

Word Hash
---- -----
sunday 0
monday 5
tuesday 3
wednesday 6
thursday 4
friday 1
saturday 2


How can i implement this as a c++ code?

Last edited on
I don't get it. How do you arrive at the hashes from the letter values?
Hi,

i hope the following solution can help you. I simply worked with the modulo operator.

modulo(%) = m = 7;

-8%m = 1 = Friday;
-4%m=4=Thursday;
-9%m=2=Saturday;
-7%m=0=Sunday;
-6%m=6=Wednesday;
3%m=3=tuesday;

and monday need the key 5 or -5;

My Hash function is

1
2
3
4
int hash( string word )
{
        return(   keyValue( word[0] ) + keyValue( word[ word.length() -1 ] )+  word.length() ) ;
}


keyValue is a function that returns value which is assigned for letter from letter table.

Letter Table
---------------
Letter Value
f -8
m -4
s -9
t -7
w -6
y 3

For example : If we want to hash "saturday" hash function will return
keyValue( 's') + keyValue( 'y' ) + 8
which is equal to 2.
Anyway, I did this part.But i use static letter table.The part that i couldn't solve is to create letter table dynamically.
This algorithm should work not only for week days , but also work for any other word list just like C reserve words( keywords ).

For more detail :
http://www.eptacom.net/pubblicazioni/pub_eng/mphash.html



Topic archived. No new replies allowed.