Title: Hashing and Hash Tables
1Hashing and Hash Tables
- Binhai Zhu
- Computer Science Department, Montana State
University -
2Motivation
- What are the dictionary operations?
-
3Motivation
- What are the dictionary operations?
- (1) Insert
- (2) Delete
- (3) Search (most of the time, we will be
focusing on search)
4Objective
- Searching takes T(n) time in the worst case (when
the data is unorganized). - Even using binary search it takes T(log n) time
when the data are sorted. - Our Objective?
-
5Objective
- Searching takes T(n) time in the worst case (when
the data is unorganized). - Even using binary search it takes T(log n)
time when the data are sorted. - Our Objective?
- O(1) time on average using hashing, under a
reasonable assumption.
6Definitions
- A hash table is a generalization of an array
(direct addressing is allowed), so lets first
talk about direct-address table. - Universe of keys U0,1,2,,m-1, no two elements
have the same key. - To represent a dynamic set, we use an array, or
direct address table T0..m-1, in which each
position (slot) corresponds to the key in the
universe.
7Definitions
- To represent a dynamic set, we use an array, or
direct address table T0..m-1, in which each
position (slot) corresponds to a key in the
universe.
T
satellite data
0
/
key
1
/
2
2
U (universe of keys)
3
3
/
4
K (actual keys)
5
5
6
/
7
/
8
8
9
/
8- With a direct address table T0..m-1, how do we
search an element x with key k? -
T
satellite data
0
/
key
1
/
2
2
U (universe of keys)
3
3
/
4
K (actual keys)
5
5
6
/
7
/
8
8
9
/
9- With a direct address table T0..m-1, how do we
search an element x with key k? - Direct-Address-Search(T,k) return Tk
T
satellite data
0
/
key
1
/
2
2
U (universe of keys)
3
3
/
4
K (actual keys)
5
5
6
/
7
/
8
8
9
/
10- With a direct address table T0..m-1, how do we
search/insert/delete an element x with key k? - Direct-Address-Search(T,k) return Tk
T
satellite data
0
/
key
1
/
2
2
U (universe of keys)
3
3
/
4
K (actual keys)
5
5
6
/
7
/
8
8
9
/
11- With a direct address table T0..m-1, how do we
search/insert/delete an element x with key k? - Direct-Address-Search(T,k) return Tk
- Direct-Address-Insert(T,x) Tkeyx ? x
- Direct-Address-Delete(T,x) Tkeyx ? Nil
T
satellite data
0
/
key
1
/
2
2
U (universe of keys)
3
3
/
4
K (actual keys)
5
5
6
/
7
/
8
8
9
/
12- With a direct address table T0..m-1, how do we
search/insert/delete an element x with key k? - Direct-Address-Search(T,k) return Tk
- Direct-Address-Insert(T,x) Tkeyx ? x
O(1) time! - Direct-Address-Delete(T,x) Tkeyx ? Nil
T
satellite data
0
/
key
1
/
2
2
U (universe of keys)
3
3
/
4
K (actual keys)
5
5
6
/
7
/
8
8
9
/
13- With a direct address table T0..m-1, how do we
search/insert/delete an element x with key k? - Direct-Address-Search(T,k) return Tk
- Direct-Address-Insert(T,x) Tkeyx ? x
Problem? - Direct-Address-Delete(T,x) Tkeyx ? Nil
T
satellite data
0
/
key
1
/
2
2
U (universe of keys)
3
3
/
4
K (actual keys)
5
5
6
/
7
/
8
8
9
/
14Hash Table
- With direct addressing, an element with key k is
inserted in slot h(k). h is called a hash
function. - h maps the universe U of keys into the slots of a
hash table T0..m-1. - h U ? 0,1,,m-1
-
T
0
/
1
/
2
U (universe of keys)
3
/
4
K (actual keys)
5
6
/
7
8
/
9
/
15Hash Table
- With direct addressing, an element with key k is
inserted in slot h(k). h is called a hash
function. - h maps the universe U of keys into the slots of a
hash table T0..m-1. - h U ? 0,1,,m-1
-
T
0
/
1
/
/
2
U (universe of keys)
3
/
4
K (actual keys)
5
If h(5)h(8)
6
/
X
Collision!
7
8
/
9
/
16Collision
- Two keys hash to the same slot --- collision.
- While collision is hard to avoid, if we design
the hash function carefully we can at least
decrease the chance for collision (and in some
cases may avoid collision). -
T
0
/
1
/
/
2
U (universe of keys)
3
/
4
K (actual keys)
5
If h(5)h(8)
6
/
X
Collision!
7
8
/
9
/
17Collision Resolution by Chaining
- Two keys hash to the same slot --- collision.
- While collision is hard to avoid, if we design
the hash function carefully we can at least
decrease the chance for collision (and in some
cases may avoid collision). -
T
0
/
1
/
/
2
U (universe of keys)
3
/
4
K (actual keys)
5
If h(5)h(8)
6
/
7
8
/
9
/
18Collision Resolution by Chaining
- Chained-Hash-Insert(T,x) insert x at the head
of list Th(keyx) - Chained-Hash-Search(T,k)
T
0
/
1
/
/
2
U (universe of keys)
3
/
4
K (actual keys)
5
If h(5)h(8)
6
/
7
8
/
9
/
19Collision Resolution by Chaining
- Chained-Hash-Insert(T,x) insert x at the head
of list Th(keyx) - Chained-Hash-Search(T,k) search for an element
with key k in list Th(k)
T
0
/
1
/
/
2
U (universe of keys)
3
/
4
K (actual keys)
5
If h(5)h(8)
6
/
7
8
/
9
/
20Collision Resolution by Chaining
- Chained-Hash-Insert(T,x) insert x at the head
of list Th(keyx) - Chained-Hash-Search(T,k) search for an element
with key k in list Th(k) - Chained-Hash-Delete(T,x)
T
0
/
1
/
/
2
U (universe of keys)
3
/
4
K (actual keys)
5
If h(5)h(8)
6
/
7
8
/
9
/
21Collision Resolution by Chaining
- Chained-Hash-Insert(T,x) insert x at the head
of list Th(keyx) - Chained-Hash-Search(T,k) search for an element
with key k in list Th(k) - Chained-Hash-Delete(T,x) delete x from the list
Th(keyx) - Time?
T
0
/
1
/
/
2
U (universe of keys)
3
/
4
K (actual keys)
5
If h(5)h(8)
6
/
7
8
/
9
/
22Collision Resolution by Chaining
- Example Let h(k) k mod 11, insert
5,28,19,15,20,33,12,17,39,11 into T0..10.
T
33
11
0
1
12
/
2
3
/
15
4
5
5
28
17
39
6
7
/
19
8
9
20
10
/
23Hash function
- A hash function which causes no collision is
called perfect hash function. - A good hash function is one which satisfies
simple uniform hashing --- each key is equally
likely to hash to any of the m slots. (It is
difficult to check this condition though.) - Now lets see some example for hash functions.
Assume that all the keys can be represented as
natural numbers. -
24Famous Examples of Hash Functions
- Division h(k) k mod m, m should be a prime
number, better close to a power of 2. - Multiplication h(k) m(kA mod 1),
- A(v5
1)/20.61803... -
25Famous Examples of Hash Functions
- Division h(k) k mod m, m should be a prime
number, better close to a power of 2. - Multiplication h(k) m(kA mod 1),
- A(v5 1)/20.61803
- Example. K 123456, m10000.
- h(k) 10000(123456 x
0.61803 mod 1)
26Famous Examples of Hash Functions
- Division h(k) k mod m, m should be a prime
number, better close to a power of 2. - Multiplication h(k) m(kA mod 1),
- A(v5
1)/20.61803... - Example. K 123456, m10000.
- h(k) 10000(123456 x
0.61803 mod 1) - 10000(76300.0041151 mod 1)
-
27Famous Examples of Hash Functions
- Division h(k) k mod m, m should be a prime
number, better close to a power of 2. - Multiplication h(k) m(kA mod 1),
- A(v5 1)/20.61803
- Example. K 123456, m10000.
- h(k) 10000(123456 x
0.61803 mod 1) - 10000(76300.0041151 mod 1)
- 10000 x 0.0041151)
-
28Famous Examples of Hash Functions
- Division h(k) k mod m, m should be a prime
number, better close to a power of 2. - Multiplication h(k) m(kA mod 1),
- A(v5 1)/20.61803.
- Example. K 123456, m10000.
- h(k) 10000(123456 x
0.61803 mod 1) - 10000(76300.0041151 mod 1)
- 10000 x 0.0041151)
- 41.151
-
29Famous Examples of Hash Functions
- Division h(k) k mod m, m should be a prime
number, better close to a power of 2. - Multiplication h(k) m(kA mod 1),
- A(v5 1)/20.61803.
- Example. K 123456, m10000.
- h(k) 10000(123456 x
0.61803 mod 1) - 10000(76300.0041151 mod 1)
- 10000 x 0.0041151)
- 41.151
- 41
-
30Famous Examples of Hash Functions
- Division h(k) k mod m, m should be a prime
number, better close to a power of 2. - Multiplication h(k) m(kA mod 1),
- A(v5 1)/20.61803.
- Folding The key is divided into several parts.
These parts are combined or folded together and
are transformed in a certain way to create the
target address. - Example 1. Shift folding 123-456-789 (SSN)
-
123456789 1368 - 1368 mod 1000 368.
-
31Famous Examples of Hash Functions
- Division h(k) k mod m, m should be a prime
number, better close to a power of 2. - Multiplication h(k) m(kA mod 1),
- A(v5 1)/20.61803.
- Folding The key is divided into several parts.
These parts are combined or folded together and
are transformed in a certain way to create the
target address. - Example 1. Shift folding 123-456-789 (SSN)
-
123456789 1368 - 1368 mod 1000 368.
- Example 2. Boundary folding 123-456-789
(SSN) -
123654789 1566 - 1566 mod 1000
566. -
32Famous Examples of Hash Functions
- Division h(k) k mod m, m should be a prime
number, better close to a power of 2. - Multiplication h(k) m(kA mod 1),
- A(v5 1)/20.61803.
- Folding The key is divided into several parts.
These parts are combined or folded together and
are transformed in a certain way to create the
target address. - Mid-square function key is squared and the
middle part of the result is taken as the
address. - Example. k3121, 31212 9740641, so h(k)
-
-
33Famous Examples of Hash Functions
- Division h(k) k mod m, m should be a prime
number, better close to a power of 2. - Multiplication h(k) m(kA mod 1),
- A(v5 1)/20.61803.
- Folding The key is divided into several parts.
These parts are combined or folded together and
are transformed in a certain way to create the
target address. - Mid-square function key is squared and the
middle part of the result is taken as the
address. - Example. k3121, 31212 9740641, so h(k)
406. - You can also encode the square into binary
representation and take the middle part. -
-
34Famous Examples of Hash Functions
- Division h(k) k mod m, m should be a prime
number, better close to a power of 2. - Multiplication h(k) m(kA mod 1),
- A(v5 1)/20.61803.
- Folding The key is divided into several parts.
These parts are combined or folded together and
are transformed in a certain way to create the
target address. - Mid-square function key is squared and the
middle part of the result is taken as the
address. - Extraction Only a part of the key is used to
compute the address. - Example 123456789, first 4 digits 1234,
last 4 digits 6789 - first 2
digits of 1234 ? last digits of 6789 - we have
1289 -
-
35Famous Examples of Hash Functions
- Division h(k) k mod m, m should be a prime
number, better close to a power of 2. - Multiplication h(k) m(kA mod 1),
- A(v5 1)/20.61803.
- Folding The key is divided into several parts.
These parts are combined or folded together and
are transformed in a certain way to create the
target address. - Mid-square function key is squared and the
middle part of the result is taken as the
address. - Extraction Only a part of the key is used to
compute the address. - Radix Transformation k is transformed into
another number base - Example 34510 4239 , then 423 mod 100
23. -
-
36Famous Examples of Hash Functions
- Division h(k) k mod m, m should be a prime
number, better close to a power of 2. - Multiplication h(k) m(kA mod 1),
- A(v5 1)/20.61803.
- Folding The key is divided into several parts.
These parts are combined or folded together and
are transformed in a certain way to create the
target address. - Mid-square function key is squared and the
middle part of the result is taken as the
address. - Extraction Only a part of the key is used to
compute the address. - Radix Transformation k is transformed into
another number base - Example 34510 4239 , then 423 mod 100
23. - 26410 3239, then 323 mod
100 23. -
-
37Famous Examples of Hash Functions
- Division h(k) k mod m, m should be a prime
number, better close to a power of 2. - Multiplication h(k) m(kA mod 1),
- A(v5 1)/20.61803.
- Folding The key is divided into several parts.
These parts are combined or folded together and
are transformed in a certain way to create the
target address. - Mid-square function key is squared and the
middle part of the result is taken as the
address. - Extraction Only a part of the key is used to
compute the address. - Radix Transformation k is transformed into
another number base - Example 34510 4239 , then 423 mod 100
23. - 26410 3239, then 323 mod
100 23. - Collision is hard to avoid in
the worst case! -
-
38Famous Examples of Hash Functions
- Division h(k) k mod m, m should be a prime
number, better close to a power of 2. - Multiplication h(k) m(kA mod 1),
- A(v5 1)/20.61803.
- Folding The key is divided into several parts.
These parts are combined or folded together and
are transformed in a certain way to create the
target address. - Mid-square function key is squared and the
middle part of the result is taken as the
address. - Extraction Only a part of the key is used to
compute the address. - Radix Transformation k is transformed into
another number base -
-
39Open Addressing
- In some applications, it is hard to dynamically
allocate additional space for handling the
chaining. - So it is natural to come up with a different way
to handle collision in which all elements are
stored in the hash table itself. Then, instead of
following pointers, we simply compute the
sequences of slots to be examined. -
- Lets use insertion as an example.
-
-
40Open Addressing
- In some applications, it is hard to dynamically
allocate additional space for handling the
chaining. - So it is natural to come up with a different way
to handle collision in which all elements are
stored in the hash table itself. Then, instead of
following pointers, we simply compute the
sequences of slots to be examined. -
- Lets use insertion as an example.
- To perform insertion using open addressing, we
successively examine or probe the hash table
until we find an empty slot to put the element. - Moreover, the sequence of positions probed
depends on the key being inserted i.e., - h U x 0,1,,m-1 ? 0,1,,m-1
-
41Open Addressing
- To perform insertion using open addressing, we
successively examine or probe the hash table
until we find an empty slot to put the element. - Moreover, the sequence of positions probed
depends on the key being inserted i.e., - h U x 0,1,,m-1 ? 0,1,,m-1
- Apparently, for every key k, the probe
sequence - lth(k,0), h(k,1),,h(k,m-1)gt is a permutation
of lt0,1,,m-1gt - so that every position in the hash table is
eventually considered as a slot for a new key as
the table fills up. - Now, for simplicity, assume kx, and there
is no deletion. -
42Open Addressing
- Hash-Insert(T,k)
- 1. i ? 0
- 2. repeat j ? h(k,i)
- if Tj Nil
- then Tj ? k
- return j
- else i ? i 1
- 7. until im
- 8. error hash table overflow
-
43Open Addressing
- Hash-Insert(T,k)
- 1. i ? 0
- 2. repeat j ? h(k,i)
- if Tj Nil
- then Tj ? k
- return j
- else i ? i 1
- 7. until im
- 8. error hash table overflow
-
T
0
1
2
3
4
5
6
7
8
Example. Insert keys 10,22,31,4,15,28,17,88,59
into T. h(k,i)h(k)i mod m, h(k)k mod m.
9
10
44Open Addressing
- Hash-Insert(T,k)
- 1. i ? 0
- 2. repeat j ? h(k,i)
- if Tj Nil
- then Tj ? k
- return j
- else i ? i 1
- 7. until im
- 8. error hash table overflow
-
T
0
h(10,0)(100) mod 11 10
1
2
3
4
5
6
7
8
Example. Insert keys 10,22,31,4,15,28,17,88,59
into T. h(k,i)h(k)i mod m, h(k)k mod m.
9
10
10
45Open Addressing
- Hash-Insert(T,k)
- 1. i ? 0
- 2. repeat j ? h(k,i)
- if Tj Nil
- then Tj ? k
- return j
- else i ? i 1
- 7. until im
- 8. error hash table overflow
-
T
0
22
h(10,0)(100) mod 11 10 h(22,0)
0 h(31,0)9 h(4,0)4 h(15,0)(40) mod 11
4
1
2
3
4
4
5
6
7
8
Example. Insert keys 10,22,31,4,15,28,17,88,59
into T. h(k,i)h(k)i mod m, h(k)k mod m.
9
31
10
10
46Open Addressing
- Hash-Insert(T,k)
- 1. i ? 0
- 2. repeat j ? h(k,i)
- if Tj Nil
- then Tj ? k
- return j
- else i ? i 1
- 7. until im
- 8. error hash table overflow
-
T
0
22
h(10,0)(100) mod 11 10 h(22,0)
0 h(31,0)9 h(4,0)4 h(15,0)(40) mod 11
4 h(15,1)(41) mod 11 5
1
2
3
4
4
5
15
6
7
8
Example. Insert keys 10,22,31,4,15,28,17,88,59
into T. h(k,i)h(k)i mod m, h(k)k mod m.
9
31
10
10
47Open Addressing
- Hash-Insert(T,k)
- 1. i ? 0
- 2. repeat j ? h(k,i)
- if Tj Nil
- then Tj ? k
- return j
- else i ? i 1
- 7. until im
- 8. error hash table overflow
-
T
0
22
1
88
2
3
4
4
5
15
28
6
17
7
8
59
- Example. Insert keys 10,22,31,4,15,28,17,88,59
into T. - h(k,i)h(k)i mod m,
- h(k)k mod m.
9
31
10
10
48Open Addressing
- Hash-Search(T,k)
- 1. i ? 0
- 2. repeat j ? h(k,i)
- if Tj k
- then return j
- i ? i 1
- 6. until TjNil or im
- 7. return Nil
-
T
0
22
1
88
i 0 j ? h(15,0)4 Tj ! 15 i 1 j ?
h(15,1)5 Tj 15 return 5
2
3
4
4
5
15
28
6
17
7
8
59
Example. Search 15 in T. h(k,i)h(k)i mod m,
h(k)k mod m.
9
31
10
10
49Open Addressing
- How about deletion?
- You can simply use Hash-Search
- to find the key first. Then what?
- 1. i ? 0
- 2. repeat j ? h(k,i)
- if Tj ! Nil and Tjk
- then Tj ? Nil? exit
- i ? i 1
- 6. until TjNil or im
-
T
0
22
1
88
2
3
4
4
5
15
28
6
17
7
8
59
Example. Delete 4,15 in T. h(k,i)h(k)i mod
m, h(k)k mod m.
9
31
10
10
50Open Addressing
- How about deletion?
- You can simply use Hash-Search
- to find the key first. Then what?
- 1. i ? 0
- 2. repeat j ? h(k,i)
- if Tj ! Nil and Tj k
- then Tj ? Nil?, exit
- i ? i 1
- 6. until TjNil or im
-
T
0
22
1
88
Delete 15 i 0 j ? h(15,0)4 Tj Nil exit
2
3
4
Nil
5
15
28
6
17
7
8
59
Example. Delete 4,15 in T. h(k,i)h(k)i mod
m, h(k)k mod m.
9
31
10
10
51Open Addressing
- How about deletion?
- You can simply use Hash-Search
- to find the key first.
- 1. i ? 0
- 2. repeat j ? h(k,i)
- if Tj ! Nil and Tj k
- then Tj ? deleted, exit
- i ? i 1
- 6. until TjNil or im
-
T
0
22
1
88
Delete 15 i 0 j ? h(15,0)4 Tj deleted i
1 j ? h(15,1)5 Tj15
2
3
4
deleted
5
15
28
6
17
7
8
59
Example. Delete 4,15 in T. h(k,i)h(k)i mod
m, h(k)k mod m.
9
31
10
10
52Open Addressing
- How about deletion?
- You can simply use Hash-Search
- to find the key first.
- 1. i ? 0
- 2. repeat j ? h(k,i)
- if Tj ! Nil and Tj k
- then Tj ? deleted, exit
- i ? i 1
- 6. until TjNil or im
-
T
0
22
1
88
Delete 15 i 0 j ? h(15,0)4 Tj deleted i
1 j ? h(15,1)5 Tj15 15 is deleted!
2
3
4
deleted
deleted
5
28
6
17
7
8
59
Example. Delete 4,15 in T. h(k,i)h(k)i mod
m, h(k)k mod m.
9
31
10
10
53Linear probing
- That is sth we have just seen.
- h is an ordinary hash function i.e.,
- h U ? 0,1,2,,m-1
- h(k,i) h(k) i mod m.
- Initial slot probed is exactly Th(k).
-
54Quadratic probing
- h is an ordinary hash function i.e.,
- h U ? 0,1,2,,m-1
- h(k,i) h(k) C1i C2i2 mod m, C1, C2 are
two non-zero constants. - Initial slot probed is also Th(k), but when
igt0 it is intuitively better than linear probing.
-
55Quadratic probing
- Example.
- Insert keys 10,22,31,4,15,28,17,88,59 into T.
- h(k,i)h(k)C1i C2i2 mod m,
- h(k)k mod m,
- C11, C23.
T
0
1
2
3
4
5
6
7
8
9
10
56Quadratic probing
- Example.
- Insert keys 10,22,31,4,15,28,17,88,59 into T.
- h(k,i)h(k)C1i C2i2 mod m,
- h(k)k mod m,
- C11, C23.
T
0
22
1
h(10,0)10 h(22,0)0 h(31,0)9 h(4,0)4 h(15,0)4
2
3
4
4
5
6
7
8
9
31
10
10
57Quadratic probing
- Example.
- Insert keys 10,22,31,4,15,28,17,88,59 into T.
- h(k,i)h(k)C1i C2i2 mod m,
- h(k)k mod m,
- C11, C23.
T
0
22
1
h(10,0)10 h(22,0)0 h(31,0)9 h(4,0)4 h(15,0)4
h(15,1)413 mod 11 8 h(28,0)6
2
3
4
4
5
6
28
7
8
15
9
31
10
10
58Quadratic probing
- Example.
- Insert keys 10,22,31,4,15,28,17,88,59 into T.
- h(k,i)h(k)C1i C2i2 mod m,
- h(k)k mod m,
- C11, C23.
T
0
22
1
h(17,0)6 h(17,1)10 h(17,2)623x22 mod 11
9 h(17,3)633x32 mod 11 3
2
3
17
4
4
5
6
28
7
8
15
9
31
10
10
59Quadratic probing
- Example.
- Insert keys 10,22,31,4,15,28,17,88,59 into T.
- h(k,i)h(k)C1i C2i2 mod m,
- h(k)k mod m,
- C11, C23.
T
0
22
1
88
2
3
17
4
4
5
6
28
59
7
8
15
9
31
10
10
60Double probing
- h(k,i) h1(k) ih2(k) mod m,
- h1, h2 are two auxiliary hash functions.
- Initial slot probed is also Th1(k).
- If m 2j, then h2 should better be an odd
function. - Example. h1(k)k mod m, h2(k)1k mod m,
mm-2.
61Double probing
- Example.
- Insert keys 10,22,31,4,15,28,17,88,59 into T.
- h(k,i)h1(k)ih2(k) mod m,
- h1(k)k mod m,
- h2(k)1 k mod (m-1).
T
0
1
2
3
4
5
6
7
8
9
10
62Double probing
- Example.
- Insert keys 10,22,31,4,15,28,17,88,59 into T.
- h(k,i)h1(k)ih2(k) mod m,
- h1(k)k mod m,
- h2(k)1 k mod (m-1).
T
0
22
h(10,0)10 h(22,0)0 h(31,0)9 h(4,0)4 h(15,0)4
h(15,1)41x6 mod 11 10 h(15,2)42x6 mod
11 5
1
2
3
4
4
5
6
7
8
9
31
10
10
63Double probing
- Example.
- Insert keys 10,22,31,4,15,28,17,88,59 into T.
- h(k,i)h1(k)ih2(k) mod m,
- h1(k)k mod m,
- h2(k)1 k mod (m-1).
T
0
22
h(28,0)6 h(17,0)6 h(17,1)61x8 mod 11
3 h(88,2)02x9 mod 11 7 h(59,2)42x10
mod 11 2
1
2
3
17
4
4
5
15
6
28
7
8
9
31
10
10
64Double probing
- Example.
- Insert keys 10,22,31,4,15,28,17,88,59 into T.
- h(k,i)h1(k)ih2(k) mod m,
- h1(k)k mod m,
- h2(k)1 k mod (m-1).
T
0
22
h(28,0)6 h(17,0)6 h(17,1)61x8 mod 11
3 h(88,2)02x9 mod 11 7 h(59,2)42x10
mod 11 2
1
59
2
3
17
4
4
5
15
6
28
88
7
8
9
31
10
10
65Analysis of hashing (in general tough)
- In a hash table with size m, we want to store n
elements with collision resolved by chaining. Let
a n/m. - Theorem. An unsuccessful search takes expected
time O(1a), under the assumption of simple
uniform hashing.
66Analysis of hashing (in general tough)
- Theorem 11.1. An unsuccessful search takes
expected time O(1a), under the assumption of
simple uniform hashing. - Sketch of proof.
- Under the assumption of simple uniform hashing,
any new key - k is equally likely to hash to any of the m
slots. The expected time to search unsuccessfully
for the key k is the expected time to search to
the end of list Th(k), which has expected size
a. Therefore, including the cost for computing
h(k), the cost for this unsuccessful search is
O(1a). ?