Beruflich Dokumente
Kultur Dokumente
8B
Hash tables
Hashing
Data records are stored in a hash table.
The position of a data record in the hash
table is determined by its key.
A hash function maps keys to positions in the
hash table.
If a hash function maps two keys to the same
position in the hash table, then a collision
occurs.
Example
10
Goals of Hashing
Linear Probing
14 70
83 29
10
10
Special consideration
55
10
14 70 36 83 29 72 48 10
Efficiency
using Linear Probing
O(____)
O(____)
Chained Hashing
Chaining
0
55
null
null
null
36 48
8
null
9 10
null
null
72 29
null
null
14 70
83
null
null
null
10
11
"Act"
"Cat"
"Ads"
"Act"
65650
"Cat"
67510
"Ads"
67602
12
Birthday Paradox:
A Hashing Function
Let k be a birthday.
Hash each birthday into a table of size 365 (one cell for each
day of the year).
13
Load Factor
The load factor of a hash table with n
elements is given by the following formula:
= n / table.length
Thus, 0 < < 1 for linear probing.
( can be greater than 1 for other collision
resolution methods)
For linear probing, as approaches 1, the
number of collisions increases
14
Reducing Collisions
The probability of a collision increases as the
load factor increases.
We cannot just double the size of the table
and copy the elements from the original table
to the new table.
Why?
15
Rehashing
Algorithm:
16
17
18
Example
19
Example (cont'd)
20
10
Example (cont'd)
21
Example (cont'd)
22
11