Hash Functions
-
Upload
nifras-ismail -
Category
Education
-
view
283 -
download
0
Transcript of Hash Functions
1
11.3 Hash Functions
2
11.3 Hash Functions
3
Division Method
4
Map a key k into one of mslots by taking the
remainder of k divided by m
Hash by Division
5
h(k) = key mod m
Hash by Division
6
Hash by Division
7
But
Hash by Division
8
Table size m should not be the power of 2p
Why?
Hash by Division
9
A prime not too close to an exact power of 2 is often a
good choice for m
Hash by Division
10
Number of elements to store is 2000
character strings, where a character has 8 bits
Hash by Division
11
We don’t mind examining an average of 3 elements in an unsuccessful search,
andso we allocate a hash table of size m=701.
We could choose m=701 becauseit is a prime near 2000/3
Butnot near any power of 2.
Hash by Division
12
Each key is integers so hash function would be
h(k) = key % 701
Hash by Division
13
Multiplication Method
14
This can be done in two steps.Multiply the key k by Constant A, where 0<A<1
Get the fraction part of kA kA mod 1
Multiply by the size of hash table mm(kA mod 1)
Then take the floor of the above valueFloor( m(kA mod 1) )
Hash by Multiplication
15
h(k) = floor(m(kA mod 1))Where 0<A<1
Hash by Multiplication
16
Advantage of Multiplication method is value of m is not a critical one we can choose
anything
Typically we choose m as 2p
Hash by Multiplication
17
Advantage of Multiplication method is value of m is not a critical one we can choose
anything
Typically we choose m as 2p
Hash by Multiplication
18
Universal Hashing
19
The any hash functions doesn't got solution for the worst case
performance
Universal Hashing
20
Universal Hashing : The hash function randomly in a way that independent of the keys that are
actually going to be stored.
Universal Hashing
21
As in the case of quick-sort, randomization guarantees that
no single input will always evoke worst-case behavior. Because
we randomly select the hash function, the algorithm can be have differently on each
execution, even for the same input, guaranteeing good average-case performance for
any input.
Universal Hashing