3 - Hashing Functions
3 - Hashing Functions
Hashing Function
• Easy to compute
• Uniform distribution
• Less collisions
• Collisions occur when different elements are mapped to the same cell.
• Keys k1, k2 with h(k1) = h(k2) are said to collide
What should we do now?
• Find a better hashing algorithm
• Use a bigger table
• Need a system to deal with collisions
• Closed Hashing (or Open Addressing): search for empty slots using
a second function and store item in first empty slot that is found.
• Each cell of the hash table points to a linked list of elements that are mapped to this cell.
• Simple, but requires additional memory outside of the table
0 (Alice,9578 - -) X
Alice
1
John 2
3 (Ria,8976 - -) (Joe,3182 - -)
Ria
4 (John,4743 - -) X
Joe
0 0 0 0
14 14 14 14
1 1 1 1
8 8 8
2 2 2 2
21 21
3 3 3 3
2
4 4 4 4
5 5 5 5
6 6 6 6
UCS405 "Discrete Mathematical Structures"
Quadratic Probing Example
insert(14) insert(8) insert(21) insert(2)
14%7 = 0 8%7 = 1 21%7 =0 2%7 = 2
0 0 0 0
14 14 14 14
1 1 1 1
8 8 8
2 2 2 2
2
3 3 3 3
4 4 4 4
21 21
5 5 5 5
6 6 6 6
UCS405 "Discrete Mathematical Structures"
Double Hashing
10 10 10 36 10 36 Collision 1
11 11 11 11
Using calculus it can be shown that each function in the list is smaller than the succeeding function, in the sense that the
ratio of a function and the succeeding function tends to zero as n grows without bound. Figure displays the graphs of these
functions, using a scale for the values of the functions that doubles for each successive marking on the graph. That is, the
vertical scale in this graph is logarithmic.