Professional Documents
Culture Documents
Hash Tables
Hsu, Lih-Hsing
Computer Theory Lab.
Chapter 11 P.2
Computer Theory Lab.
Chapter 11 P.3
Computer Theory Lab.
Chapter 11 P.4
Computer Theory Lab.
DIRECTED_ADDRESS_SEARCH(T,k)
return T [k ]
DIRECTED_ADDRESS_INSERT(T,x)
T [ key [ x ]] x
DIRECTED-ADDRESS_DELETE(T,x)
T [ key [ x ]] nil
Chapter 11 P.5
Computer Theory Lab.
Chapter 11 P.7
Computer Theory Lab.
ame slot
Chapter 11 P.8
Computer Theory Lab.
open addressing
Chapter 11 P.9
Computer Theory Lab.
Collision resolution by
chaining:
In chaining, we put all the
elements that hash to the same
slot in a linked list.
Chapter 11 P.10
Computer Theory Lab.
Chapter 11 P.11
Computer Theory Lab.
CHAINED_HASH_INSERT(T,x )
Insert x at the head of the list T[h[key[x]]]
CHAINED_HASH_SEARCH(T,k )
Search for the element with key k in the
list T[h[k]]
Chapter 11 P.12
Computer Theory Lab.
CHAINED_HASH_DELETE(T,x )
delete x from the list T[h[key[x]]]
Complexity:
INSERT O(1)
DELETE O(1) if the list
are doubly linked.
Chapter 11 P.13
Computer Theory Lab.
Chapter 11 P.14
Computer Theory Lab.
Chapter 11 P.15
Computer Theory Lab.
Theorem 11.1.
If a hash table in which collision are
resolved by chaining, an unsuccessful
search takes expected time (1+),
under the assumption of simple uniform
hashing.
Chapter 11 P.16
Computer Theory Lab.
Proof.
n
The average length of the list is .
m
The expected number of elements
examined in an unsuccessful search is
.
The total time required (including the
time for computing h(k) is O(1+ ).
Chapter 11 P.17
Computer Theory Lab.
Theorem 11.2
If a hash table in which collision are
resolved by chaining, a successful
search takes time , (1+) on the
average, under the assumption of
simple uniform hashing.
Chapter 11 P.18
Computer Theory Lab.
Proof.
Assume the key being searched is
equally likely to be any of the n keys
stored in the table.
Assume that CHAINED_HASH_INSERT
procedure insert a new element at the
end of the list instead of the front.
Chapter 11 P.19
Computer Theory Lab.
1 n n 1 n n
E 1 X ij n 1 E[ X ij ]
n i 1 j i 1 i 1 j i 1
1 n n 1
1
n i 1
j i 1 m
1 n
1
nm i 1
(n i )
1 n n
1 n i
nm i 1 i 1
1 n(n 1)
1 n 2
nm 2
Total time required for
1 .
2 2n search
a successful
( 2 ) (1 ).
2 2n
Chapter 11 P.20
Computer Theory Lab.
Chapter 11 P.21
Computer Theory Lab.
Example:
Assume 0 k 1 .
Set h( k ) km .
Chapter 11 P.22
Computer Theory Lab.
Chapter 11 P.23
Computer Theory Lab.
h( k ) k mod m
Suggestion: Choose m to be prime
and not too close to exactly power of 2.
Chapter 11 P.24
Computer Theory Lab.
h( k ) m( kA mod 1 )
where kA mod 1 kA kA
Chapter 11 P.25
Computer Theory Lab.
Suggestion: 5 1
p
choose m 2 , A 2
Chapter 11 P.26
Computer Theory Lab.
Example:
14
k 123456, p 14, m 2 26384
5 1
A 0.61803...
2
h(k ) 10000 (123456 A mod1)
10000 0.0041151
41.151 41
Chapter 11 P.27
Computer Theory Lab.
Chapter 11 P.28
Computer Theory Lab.
Chapter 11 P.29
Computer Theory Lab.
Chapter 11 P.30
Computer Theory Lab.
Theorem 11.3
Suppose that a hash function h is choose fro
m a universal collection of hash function and i
s used to hash n keys into a table T of size m,
using chaining to resolve collisions. If key k is
not in the table, then the expected length E[n
h(k)] of the list that key k hashes to is at most
. If key k is in the table, then the expected l
ength E[nh(k)] of the lest containing key k is at
most 1 + .
Chapter 11 P.31
Computer Theory Lab.
Proof.
For each pair k and l of distinct keys, de
fine the indicator random variable
Xkl = I{h(k) = h(l)}.
Pr{h(k)=h(l)} ≤ 1/m
E[Xkl] ≤ 1/m
Chapter 11 P.32
Computer Theory Lab.
We define Yk X kl .
lT
l k
Thus we have
E[Yk ] E[ X kl ]
lT
l k
E[X kl ]
lT
l k
1
lT m
l k
Chapter 11 P.33
Computer Theory Lab.
Chapter 11 P.34
Computer Theory Lab.
Corollary 11.4
Using universal hashing and collision
resolution by chaining in a table with m
slots, it takes expected time (n) to
handle any sequence if n INSERT,
SEARCH and DELETE operations
containing O(m) INSERT operations.
Chapter 11 P.35
Computer Theory Lab.
Proof.
Since the number of insertions is O(m),
we have n = O(m) and so = O(1). The
INSERT and DELETE operations take
constant time and, by Theorem 11.3,
the expected time for each SEARCH
operation is O(1). BY linearity of
expectation, therefore, the expected
time for the entire sequence of
operations is O(n)
Chapter 11 P.36
Computer Theory Lab.
Chapter 11 P.37
Computer Theory Lab.
Chapter 11 P.38
Computer Theory Lab.
Chapter 11 P.39
Computer Theory Lab.
Theorem 11.5
The class Hp,m defined above is a
universal hash functions.
Chapter 11 P.40
Computer Theory Lab.
Proof.
Consider two distinct keys k, l from Zp, so k ≠ l
. For a given hash function ha,b we let
r = (ak + b) mod p,
s = (al + b) mod p.
r≠s
a = ((r – s)((k – l)-1 mod p)) mod p,
b = (r – ak) mod p,
For any given pair of input k and l, if we pick
(a, b) uniformly at random form Zp* Zp, the r
esulting pair (r, s) is equally likely to be any p
air of distinct values modulo p.
Chapter 11 P.41
Computer Theory Lab.
Chapter 11 P.43
Computer Theory Lab.
HASH_INSERT(T,k)
1 i0
2 repeat j h(k, i)
3 if T[j]= NIL
4 then T[j] k
5 return j
6 else i i + 1
7 until i = m
8 error “hash table overflow”
Chapter 11 P.44
Computer Theory Lab.
HASH_SEARCH(T,k)
1 i0
2 repeat j h(k, i)
3 if T[j] = k
4 then return j
5 ii+1
6 until T[j] = NIL or i = m
7 return NIL
Chapter 11 P.45
Computer Theory Lab.
Linear probing:
h( k , i ) ( h' ( k ) i ) mod m
It suffers the primary clustering
problem.
Chapter 11 P.46
Computer Theory Lab.
Quadratic probing:
Chapter 11 P.47
Computer Theory Lab.
Double hashing:
Chapter 11 P.48
Computer Theory Lab.
h1 (k ) k mod13
h2 (k ) 1 (k mod11)
Insert 14
Chapter 11 P.49
Computer Theory Lab.
Example:
h1 (k ) k mod m
h2 (k ) 1 (k mod m )
Chapter 11 P.50
Computer Theory Lab.
Chapter 11 P.51
Computer Theory Lab.
Chapter 11 P.52
Computer Theory Lab.
Theorem 11.6
Given an open-address hash-table
with load factor = n/m < 1, the
expected number of probes in an
unsuccessful search is at most
1/(1-) assuming uniform hashing.
Chapter 11 P.53
Computer Theory Lab.
Proof.
Define pi =Pr{exactly i probes
access occupied slots}
for 0 i n . And pi 0 if i n.
The expected
number of probes is
1 ip.i
i 0
Define qi Pr{at least i probes
access occupied slots}.
Chapter 11 P.54
Computer Theory Lab.
ipi qi
i 0 i 1
E[ X ] i Pr{ X i}
i 0
i (Pr{ X i} Pr{ X i 1})
i 0 i 0
Pr{ X i}
Chapter 11 i 1 P.55
Computer Theory Lab.
n n n 1
q1 q2 ( )( )
m m m 1
n n 1 n .i 1 n i i
qi ( )( )...( )( )
m m 1 m i 1 m
if 1 i n
Chapter 11 P.56
Computer Theory Lab.
qi 0 for i > n.
1
1 ipi 1 qi 1 ...
2
i 0 i 0 1
Chapter 11 P.57
Computer Theory Lab.
Example:
1
0 .5 2
1
1
0 .9 10
1
Chapter 11 P.58
Computer Theory Lab.
Corollary 11.7
Inserting an element into an open-
address hash table with load factor
requires at most 1/(1 - ) probes
on average, assuming uniform
hashing.
Chapter 11 P.59
Computer Theory Lab.
Proof.
An element is inserted only if there
is room in the table, and thus
1 a key requires an
. Inserting
unsuccessful search followed by
placement of the key in the first
empty slot found. Thus, the
expected number of probes is
. 1
Chapter 11 1 P.60
Computer Theory Lab.
Theorem 11.8
Given an open-address hash table
with load factor 1 , the
expected number of successful
1 1 1
search is at most ln1
assuming uniform hashing and
assuming that each key in the table
is equally likely to be searched for.
Chapter 11 P.61
Computer Theory Lab.
Proof.
A search for k follows the same probe
sequence as followed when k was
inserted.
If k is the (i+1)st key inserted in the
hash table, the expected number of
probes made in a search for k is at
1 m
most .
i mi
1
m
Chapter 11 P.62
Computer Theory Lab.
Chapter 11 P.63
Computer Theory Lab.
1 n 1 m m n 1 1 1
( H m H m 1 )
n i 0 m i n i 0 m i
1
(ln m 1 ln(m n))
(Since ln i H i ln i 1)
1 m 1 1 1 1
ln ln
m n 1
Chapter 11 P.64
Computer Theory Lab.
Example:
1 1 1
0 .5 ln 3.387
1
1 1 1
0 .9 ln 3.670
1
Chapter 11 P.65