Hash 2
Hash 2
Overview
• Hash Tables
• Collisions
• Linear Probing
• Problems with Linear Probing
• Chaining
2
Hashing
hash table
0
1
key hash
pos 2
function
3
:
:
TABLESIZE - 1
3
Example:
hash table
0
1
“Kruse” hash 2
5
function
3
5 Kruse
6
4
Hashing
5
Operations
• Initialize
– all locations in Hash Table are empty.
• Insert
• Search
• Delete
6
Hash Function
• Maps keys to positions in the Hash Table.
• Be easy to calculate.
• Use all of the key.
• Spread the keys uniformly.
7
Example: Hash Function #3
value = (s[i] + 3*value) % 7;
Hash
Key Value
Aho 0
Kruse 5
Standish 1 “collisions”
Horowitz 5
Langsam 5
Sedgewick 2
Knuth 1
8
Collision
• When two keys are mapped to the same
position.
• Very likely.
9
Birthdays
Number of People Probability
10 0.1169
20 0.4114
30 0.7063
40 0.8912
50 0.9704
60 0.9941
70 0.9992
10
Collision Resolution
• Linear Probing.
• Chaining
11
Linear Probing
• Linear search in the array from the position
where collision occurred.
12
Insert
• Apply hash function to get a position.
• Try to insert key at this position.
• Deal with collision.
13
Example: Insert with Linear Probing
Aho, Kruse, Standish, Horowiz, Langsam, Sedgewick, Knuth
hash table
0 Aho
1
Aho Hash 2
0
Function
3
6
14
Example: Insert with Linear Probing
Aho, Kruse, Standish, Horowiz, Langsam, Sedgewick, Knuth
hash table
0 Aho
1
Kruse Hash 2
5
Function
3
5 Kruse
6
15
Example: Insert with Linear Probing
Aho, Kruse, Standish, Horowiz, Langsam, Sedgewick, Knuth
hash table
0 Aho
1 Standish
Standish Hash 2
1
Function
3
5 Kruse
6
16
Example: Insert with Linear Probing
Aho, Kruse, Standish, Horowiz, Langsam, Sedgewick, Knuth
hash table
0 Aho
1 Standish
Horowitz Hash 2
5
Function
3
5 Kruse
6 Horowitz
17
Example: Insert with Linear Probing
Aho, Kruse, Standish, Horowiz, Langsam, Sedgewick, Knuth
hash table
0 Aho
1 Standish
5 Kruse
6 Horowitz
18
module linearProbe(item)
{
position = hash(key of item)
count = 0
loop {
if (count == hashTableSize) then {
output “Table is full”
exit loop
}
if (hashTable[position] is empty) then {
hashTable[position] = item
exit loop
}
position = (position + 1) % hashTableSize
count++
}
}
19
Search
• Apply hash function to get a position.
• Look in that position.
• Deal with collision.
20
Example: Search with Linear Probing
Aho, Kruse, Standish, Horowiz, Langsam, Sedgewick, Knuth
1 Standish
5 Kruse
6 Horowitz
21
Example: Search with Linear Probing
Aho, Kruse, Standish, Horowiz, Langsam, Sedgewick, Knuth
hash table
0 Aho
1 Standish
5 Kruse
not found. 6 Horowitz
22
module search(target)
{
count = 0
position = hash(key of target)
loop {
if (count == hashTableSize) then {
output “Target is not in Hash Table”
return -1.
}
else if (hashTable[position] is empty) then {
output “Item is not in Hash Table”
return -1.
}
else if (hashTable[position].key == target) then {
return position.
}
position = (position + 1) % hashTableSize
count++
}
} 23
Delete
• Use the search function to find the item
• If found check that items after that also
don’t hash to the item’s position
• If items after do hash to that position, move
them back in the hash table and delete the
item.
24
Linear Probing: Problems
• Speed.
• Tendency for clustering to occur as the
table becomes half full.
• Deletion of records is very difficult.
25
Chaining
• Uses a Linked List at each position in the
Hash Table.
26
hash table
0
:
:
27
Example: Chaining
Aho, Kruse, Standish, Horowiz, Langsam, Sedgwick, Knuth
0, 5, 1, 5, 5, 2, 1
1 Aho
0
2 Standish Knuth
1
2 1 Sedgewick
3 0
4 0
3 Kruse Horowitz Langsam
5
6 0
28
Hashtable with Chaining
• At each position in the array you have a list:
List hashTable[MAXTABLE];
1
0
2
1
2 1
:
29
Insert with Chaining
• Apply hash function to get a position in the
array.
• Insert key into the Linked List at this
position in the array.
30
module InsertChaining(item)
{
posHash = hash(key of item)
1 Aho
0
2 Standish Knuth
1
2 1 Sedgewick
: 31
Search with Chaining
• Apply hash function to get a position in the
array.
• Search the Linked List at this position in the
array.
32
/* module returns NULL if not found, or the address of the
* node if found */
module SearchChaining(item)
{
posHash = hash(key of item)
Node* found;
return found;
}
1 Aho
0
2 Standish Knuth
1
2 1 Sedgewick
: 33
Delete with Chaining
• Apply hash function to get a position in the
array.
• Delete the node in the Linked List at this
position in the array.
34
/* module uses the Linked list delete function to delete an item
module DeleteChaining(item)
{
posHash = hash(key of item)
1 Aho
0
2 Standish Knuth
1
2 1 Sedgewick
: 35
Disadvantages of Chaining
36
Advantages of Chaining
37