CS 332: Algorithms: Universal Hashing
CS 332: Algorithms: Universal Hashing
CS 332: Algorithms: Universal Hashing
Universal Hashing
Reminder: Homework 4
Programming assignment:
Implement Skip Lists
Evaluate performance
Extra credit: compare performance to randomly
built BST
Due Mar 9 (Fri before Spring Break)
Monday after break counts as 1 late day
Review: Chaining
Chaining puts elements that hash to the same
k6
k2
k5
k8
k1
k4
k5
k2
k1
k4
K
(actual
k7
keys)
k3
k3
k8
k6
k7
table appropriately
Fractional part of kA
Upshot:
Choose m = 2P
Choose A not too close to 0 or 1
Knuth: Good choice for A = (5 - 1)/2
Universal Hashing
Let
(x y)
Universal Hashing
Theorem 12.3:
Choose h from a universal family of hash functions
Hash n keys into a table of m slots, n m
Then the expected number of collisions involving a particular key
x is less than 1
Proof:
For each pair of keys y, z, let cyx = 1 if y and z collide, 0 otherwise
E[cyz] = 1/m (by definition)
Let Cx be total number of collisions involving key x
n 1
E[C x ] E[cxy ]
Since n m, we
1
yT have E[Cx] < m
yx
analysis of algorithms
So far, weve only looked at one design
technique (What is it?)
analysis of algorithms
So far, weve only looked at one design
technique: divide and conquer
Next up: augmenting data structures
Or, One good thief is worth ten good scholars
of order statistics?
including x itself:
M
8
C
5
P
2
A
1
F
3
D
1
Q
1
H
1
Selection On OS Trees
M
8
C
5
P
2
A
1
F
3
D
1
Q
1
H
1
OS-Select
OS-Select(x, i)
{
r = x->left->size + 1;
if (i == r)
return x;
else if (i < r)
return OS-Select(x->left, i);
else
return OS-Select(x->right, i-r);
}