Hash Functions

21
1 11.3 Hash Functions

Transcript of Hash Functions

Page 1: Hash Functions

1

11.3 Hash Functions

Page 2: Hash Functions

2

11.3 Hash Functions

Page 3: Hash Functions

3

Division Method

Page 4: Hash Functions

4

Map a key k into one of mslots by taking the

remainder of k divided by m

Hash by Division

Page 5: Hash Functions

5

h(k) = key mod m

Hash by Division

Page 6: Hash Functions

6

Hash by Division

Page 7: Hash Functions

7

But

Hash by Division

Page 8: Hash Functions

8

Table size m should not be the power of 2p

Why?

Hash by Division

Page 9: Hash Functions

9

A prime not too close to an exact power of 2 is often a

good choice for m

Hash by Division

Page 10: Hash Functions

10

Number of elements to store is 2000

character strings, where a character has 8 bits

Hash by Division

Page 11: Hash Functions

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

Page 12: Hash Functions

12

Each key is integers so hash function would be

h(k) = key % 701

Hash by Division

Page 13: Hash Functions

13

Multiplication Method

Page 14: Hash Functions

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

Page 15: Hash Functions

15

h(k) = floor(m(kA mod 1))Where 0<A<1

Hash by Multiplication

Page 16: Hash Functions

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

Page 17: Hash Functions

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

Page 18: Hash Functions

18

Universal Hashing

Page 19: Hash Functions

19

The any hash functions doesn't got solution for the worst case

performance

Universal Hashing

Page 20: Hash Functions

20

Universal Hashing : The hash function randomly in a way that independent of the keys that are

actually going to be stored.

Universal Hashing

Page 21: Hash Functions

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