Professional Documents
Culture Documents
13 Hashing
13 Hashing
13 Hashing
Hash Tables
Tables –– 11
h(k4)
k1 k4
K
(actual k2 collisio h(k2)=h(k5)
keys) k5
k3 n
h(k3)
m–1
Open Addressing:
» All elements stored in hash table itself.
» When collisions occur, use a systematic
(consistent) procedure to store elements
in free slots of the table.
0
U
(universe of keys) h(k1)=h(k4)
X
k1
k4
K
(actual k2 k6 X
k5 h(k2)=h(k5)=h(k6)
keys)
k8 k7
k3
X h(k3)=h(k7)
h(k8)
m–1
0
U
(universe of keys) k1 k4
k1
k4
K
(actual k2 k6
k5 k5 k2 k6
keys)
k8 k7
k3
k7 k3
k8
m–1
Proof:
Any key not already in the table is equally likely to hash
to any of the m slots.
To search unsuccessfully for any key k, need to search to
the end of the list T[h(k)], whose expected length is α.
Adding the time to compute the hash function, the total
time required is Θ(1+α).
Proof (contd):
Let xi be the ith element inserted into the table, and let ki = key[xi].
Define indicator random variables Xij = I{h(ki) = h(kj)}, for all i, j.
Simple uniform hashing Pr{h(ki) = h(kj)} = 1/m
E[Xij] = 1/m.
Expected number of elements examined in a successful search is:
1 n n
E 1 X ij
n i 1 j i 1
1 2 n( n 1)
1 n
nm 2
Expected total time for a successful search
n 1
1 = Time to compute hash function + Time
2m
1
to search
2 2n
= O(2+/2 – /2n) = O(1+ ).
shtables - 16 Comp 122, Fall 2003 Lin / Devi
Expected Cost – Interpretation
If n = O(m), then =n/m = O(m)/m = O(1).
Searching takes constant time on average.
Insertion is O(1) in the worst case.
Deletion takes O(1) worst-case time when lists are doubly
linked.
Hence, all dictionary operations take O(1) time on
average with hash tables with chaining.
r1 · r0
extract p bits
h(k)