Theorem 11.2
In a hash table in which collisions are resolved by chaining, a successful search
takes ‚.1 C ˛/ time on average, under the assumption of independent uniform
hashing.
Proof We assume that the element being searched for is equally likely to be any
of the n elements stored in the table. The number of elements examined during
a successful search for an element x is 1 more than the number of elements that
appear before x in x ’s list. Because new elements are placed at the front of the list,
280 Chapter 11 Hash Tables
elements before x in the list were all inserted after x was inserted. Let xi denote
the i th element inserted into the table, for i D 1; 2; : : : ; n, and let ki D xi : key.
Our analysis uses indicator random variables extensively. For each slot q in the
table and for each pair of distinct keys ki and kj , we deûne the indicator random
variable
Xij q D I fthe search is for xi , h.ki / D q, and h.kj / D q g :
That is, Xij q D 1 when keys ki and kj collide at slot q and the search is for
element xi . Because Pr fthe search is for xi g D 1=n, Pr fh.ki / D qg D 1=m,
Pr fh.kj / D qg D 1=m, and these events are all independent, we have that
Pr fXij q D 1g D 1=nm2 . Lemma 5.1 on page 130 gives E ŒXij q � D 1=nm2 .
Next, we deûne, for each element xj , the indicator random variable
Yj D I fxj appears in a list prior to the element being searched forg
D
m1X
qD0
j 1
X
i D1
Xij q ;
since at most one of the Xij q equals 1, namely when the element xi being searched
for belongs to the same list as xj (pointed to by slot q), and i < j (so that xi
appears after xj in the list).
Our ûnal random variable is Z, which counts how many elements appear in the
list prior to the element being searched for:
Z D
nX
j D1
Yj :
Because we must count the element being searched for as well as all those pre-
ceding it in its list, we wish to compute E ŒZ C 1�. Using linearity of expectation
(equation (C.24) on page 1192), we have
E ŒZ C 1� D E
"
1 C
n
X
j D1
Yj
D 1 C E
" nX
j D1
m1X
qD0
j 1
X
i D1
Xij q
D 1 C E
"m1
X
qD0
n
X
j D1
j 1
X
i D1
Xij q
D 1 C
m1X
qD0
nX
j D1
j 1
X
i D1
E ŒXij q � (by linearity of expectation)
11.2 Hash tables 281
D 1 C
m1
X
qD0
n
X
j D1
j 1
X
i D1
1
nm2
D 1 C m n.n 1/
2 1
nm2 (by equation (A.2) on page 1141)
D 1 C n 1
2m
D 1 C n
2m 1
2m
D 1 C ˛
2 ˛
2n :
Thus, the total time required for a successful search (including the time for com-
puting the hash function) is ‚.2 C ˛=2 ˛=2n/ D ‚.1 C ˛/.
What does this analysis mean? If the number of elements in the table is at
most proportional to the number of hash-table slots, we have n D O.m/ and,
consequently, ˛ D n=m D O.m/=m D O.1/. Thus, searching takes constant time
on average. Since insertion takes O.1/ worst-case time and deletion takes O.1/
worst-case time when the lists are doubly linked (assuming that the list element to
be deleted is known, and not just its key), we can support all dictionary operations
in O.1/ time on average.
The analysis in the preceding two theorems depends only on two essential prop-
erties of independent uniform hashing: uniformity (each key is equally likely to
hash to any one of the m slots), and independence (so any two distinct keys collide
with probability 1=m). expalin this to me