Hash Tables: Collision Resolution. 2 Overview Hash Tables Collisions Linear Probing Problems with...
-
Upload
leona-york -
Category
Documents
-
view
213 -
download
0
Transcript of Hash Tables: Collision Resolution. 2 Overview Hash Tables Collisions Linear Probing Problems with...
![Page 1: Hash Tables: Collision Resolution. 2 Overview Hash Tables Collisions Linear Probing Problems with Linear Probing Chaining.](https://reader035.fdocuments.us/reader035/viewer/2022070400/56649f155503460f94c2a77f/html5/thumbnails/1.jpg)
Hash Tables: Collision ResolutionHash Tables: Collision Resolution
![Page 2: Hash Tables: Collision Resolution. 2 Overview Hash Tables Collisions Linear Probing Problems with Linear Probing Chaining.](https://reader035.fdocuments.us/reader035/viewer/2022070400/56649f155503460f94c2a77f/html5/thumbnails/2.jpg)
2
OverviewOverview
• Hash Tables
• Collisions
• Linear Probing
• Problems with Linear Probing
• Chaining
![Page 3: Hash Tables: Collision Resolution. 2 Overview Hash Tables Collisions Linear Probing Problems with Linear Probing Chaining.](https://reader035.fdocuments.us/reader035/viewer/2022070400/56649f155503460f94c2a77f/html5/thumbnails/3.jpg)
3
HashingHashing
key hash function
0
1
2
3
TABLESIZE - 1
:
:
hash table
pos
![Page 4: Hash Tables: Collision Resolution. 2 Overview Hash Tables Collisions Linear Probing Problems with Linear Probing Chaining.](https://reader035.fdocuments.us/reader035/viewer/2022070400/56649f155503460f94c2a77f/html5/thumbnails/4.jpg)
4
“Kruse”
0
1
2
3
6
4
5
hash table
Example:
5
Kruse
hash function
![Page 5: Hash Tables: Collision Resolution. 2 Overview Hash Tables Collisions Linear Probing Problems with Linear Probing Chaining.](https://reader035.fdocuments.us/reader035/viewer/2022070400/56649f155503460f94c2a77f/html5/thumbnails/5.jpg)
5
HashingHashing
• Each item has a unique key.
• Use a large array called a Hash Table.
• Use a Hash Function.
![Page 6: Hash Tables: Collision Resolution. 2 Overview Hash Tables Collisions Linear Probing Problems with Linear Probing Chaining.](https://reader035.fdocuments.us/reader035/viewer/2022070400/56649f155503460f94c2a77f/html5/thumbnails/6.jpg)
6
OperationsOperations
• Initialize– all locations in Hash Table are empty.
• Insert
• Search
• Delete
![Page 7: Hash Tables: Collision Resolution. 2 Overview Hash Tables Collisions Linear Probing Problems with Linear Probing Chaining.](https://reader035.fdocuments.us/reader035/viewer/2022070400/56649f155503460f94c2a77f/html5/thumbnails/7.jpg)
7
Hash FunctionHash Function
• Maps keys to positions in the Hash Table.
• Be easy to calculate.
• Use all of the key.
• Spread the keys uniformly.
![Page 8: Hash Tables: Collision Resolution. 2 Overview Hash Tables Collisions Linear Probing Problems with Linear Probing Chaining.](https://reader035.fdocuments.us/reader035/viewer/2022070400/56649f155503460f94c2a77f/html5/thumbnails/8.jpg)
8
Example: Hash Function #3
“collisions”
value = (s[i] + 3*value) % 7;
Hash Key Value
Aho 0Kruse 5Standish 1Horowitz 5Langsam 5Sedgewick 2Knuth 1
![Page 9: Hash Tables: Collision Resolution. 2 Overview Hash Tables Collisions Linear Probing Problems with Linear Probing Chaining.](https://reader035.fdocuments.us/reader035/viewer/2022070400/56649f155503460f94c2a77f/html5/thumbnails/9.jpg)
9
CollisionCollision
• When two keys are mapped to the same position.
• Very likely.
![Page 10: Hash Tables: Collision Resolution. 2 Overview Hash Tables Collisions Linear Probing Problems with Linear Probing Chaining.](https://reader035.fdocuments.us/reader035/viewer/2022070400/56649f155503460f94c2a77f/html5/thumbnails/10.jpg)
10
BirthdaysBirthdays
10
20
30
40
50
60
70
0.1169
0.4114
0.7063
0.8912
0.9704
0.9941
0.9992
Number of People Probability
![Page 11: Hash Tables: Collision Resolution. 2 Overview Hash Tables Collisions Linear Probing Problems with Linear Probing Chaining.](https://reader035.fdocuments.us/reader035/viewer/2022070400/56649f155503460f94c2a77f/html5/thumbnails/11.jpg)
11
Collision ResolutionCollision Resolution
• Linear Probing.
• Chaining
![Page 12: Hash Tables: Collision Resolution. 2 Overview Hash Tables Collisions Linear Probing Problems with Linear Probing Chaining.](https://reader035.fdocuments.us/reader035/viewer/2022070400/56649f155503460f94c2a77f/html5/thumbnails/12.jpg)
12
Linear ProbingLinear Probing
• Linear search in the array from the position where collision occurred.
![Page 13: Hash Tables: Collision Resolution. 2 Overview Hash Tables Collisions Linear Probing Problems with Linear Probing Chaining.](https://reader035.fdocuments.us/reader035/viewer/2022070400/56649f155503460f94c2a77f/html5/thumbnails/13.jpg)
13
InsertInsert
• Apply hash function to get a position.
• Try to insert key at this position.
• Deal with collision.
![Page 14: Hash Tables: Collision Resolution. 2 Overview Hash Tables Collisions Linear Probing Problems with Linear Probing Chaining.](https://reader035.fdocuments.us/reader035/viewer/2022070400/56649f155503460f94c2a77f/html5/thumbnails/14.jpg)
14
Aho Hash Function
0
1
2
3
6
4
5
hash table
Aho, Kruse, Standish, Horowiz, Langsam, Sedgewick, Knuth
0
Example: Insert with Linear Probing
Aho
![Page 15: Hash Tables: Collision Resolution. 2 Overview Hash Tables Collisions Linear Probing Problems with Linear Probing Chaining.](https://reader035.fdocuments.us/reader035/viewer/2022070400/56649f155503460f94c2a77f/html5/thumbnails/15.jpg)
15
Kruse
0
1
2
3
6
4
5
hash table
Aho, Kruse, Standish, Horowiz, Langsam, Sedgewick, Knuth
5
Example: Insert with Linear Probing
Aho
Kruse
Hash Function
![Page 16: Hash Tables: Collision Resolution. 2 Overview Hash Tables Collisions Linear Probing Problems with Linear Probing Chaining.](https://reader035.fdocuments.us/reader035/viewer/2022070400/56649f155503460f94c2a77f/html5/thumbnails/16.jpg)
16
Standish
0
1
2
3
6
4
5
hash table
Aho, Kruse, Standish, Horowiz, Langsam, Sedgewick, Knuth
1
Example: Insert with Linear Probing
Aho
Kruse
Standish
Hash Function
![Page 17: Hash Tables: Collision Resolution. 2 Overview Hash Tables Collisions Linear Probing Problems with Linear Probing Chaining.](https://reader035.fdocuments.us/reader035/viewer/2022070400/56649f155503460f94c2a77f/html5/thumbnails/17.jpg)
17
Horowitz
0
1
2
3
6
4
5
hash table
Aho, Kruse, Standish, Horowiz, Langsam, Sedgewick, Knuth
5
Example: Insert with Linear Probing
Aho
Kruse
Standish
Horowitz
Hash Function
![Page 18: Hash Tables: Collision Resolution. 2 Overview Hash Tables Collisions Linear Probing Problems with Linear Probing Chaining.](https://reader035.fdocuments.us/reader035/viewer/2022070400/56649f155503460f94c2a77f/html5/thumbnails/18.jpg)
18
Langsam
0
1
2
3
6
4
5
hash table
Aho, Kruse, Standish, Horowiz, Langsam, Sedgewick, Knuth
5
Example: Insert with Linear Probing
Aho
Kruse
Standish
Horowitz
Hash Function
Langsam
![Page 19: Hash Tables: Collision Resolution. 2 Overview Hash Tables Collisions Linear Probing Problems with Linear Probing Chaining.](https://reader035.fdocuments.us/reader035/viewer/2022070400/56649f155503460f94c2a77f/html5/thumbnails/19.jpg)
19
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++ }}
![Page 20: Hash Tables: Collision Resolution. 2 Overview Hash Tables Collisions Linear Probing Problems with Linear Probing Chaining.](https://reader035.fdocuments.us/reader035/viewer/2022070400/56649f155503460f94c2a77f/html5/thumbnails/20.jpg)
20
SearchSearch
• Apply hash function to get a position.
• Look in that position.
• Deal with collision.
![Page 21: Hash Tables: Collision Resolution. 2 Overview Hash Tables Collisions Linear Probing Problems with Linear Probing Chaining.](https://reader035.fdocuments.us/reader035/viewer/2022070400/56649f155503460f94c2a77f/html5/thumbnails/21.jpg)
21
Kruse
Langsam
0
1
2
3
6
4
5
hash table
Aho, Kruse, Standish, Horowiz, Langsam, Sedgewick, Knuth
5
Example: Search with Linear Probing
Aho
Standish
Horowitz
Hash Function
Langsam
found.
![Page 22: Hash Tables: Collision Resolution. 2 Overview Hash Tables Collisions Linear Probing Problems with Linear Probing Chaining.](https://reader035.fdocuments.us/reader035/viewer/2022070400/56649f155503460f94c2a77f/html5/thumbnails/22.jpg)
22
Kruse
Knuth
0
1
2
3
6
4
5
hash table
1
Example: Search with Linear Probing
Aho
Standish
Horowitz
Hash Function
Langsam
not found.
Aho, Kruse, Standish, Horowiz, Langsam, Sedgewick, Knuth
![Page 23: Hash Tables: Collision Resolution. 2 Overview Hash Tables Collisions Linear Probing Problems with Linear Probing Chaining.](https://reader035.fdocuments.us/reader035/viewer/2022070400/56649f155503460f94c2a77f/html5/thumbnails/23.jpg)
23
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++ }}
![Page 24: Hash Tables: Collision Resolution. 2 Overview Hash Tables Collisions Linear Probing Problems with Linear Probing Chaining.](https://reader035.fdocuments.us/reader035/viewer/2022070400/56649f155503460f94c2a77f/html5/thumbnails/24.jpg)
24
DeleteDelete
• 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.
![Page 25: Hash Tables: Collision Resolution. 2 Overview Hash Tables Collisions Linear Probing Problems with Linear Probing Chaining.](https://reader035.fdocuments.us/reader035/viewer/2022070400/56649f155503460f94c2a77f/html5/thumbnails/25.jpg)
25
Linear Probing: ProblemsLinear Probing: Problems
• Speed.
• Tendency for clustering to occur as the table becomes half full.
• Deletion of records is very difficult.
![Page 26: Hash Tables: Collision Resolution. 2 Overview Hash Tables Collisions Linear Probing Problems with Linear Probing Chaining.](https://reader035.fdocuments.us/reader035/viewer/2022070400/56649f155503460f94c2a77f/html5/thumbnails/26.jpg)
26
ChainingChaining
• Uses a Linked List at each position in the Hash Table.
![Page 27: Hash Tables: Collision Resolution. 2 Overview Hash Tables Collisions Linear Probing Problems with Linear Probing Chaining.](https://reader035.fdocuments.us/reader035/viewer/2022070400/56649f155503460f94c2a77f/html5/thumbnails/27.jpg)
27
hash table
0
1
2
3
:
:
![Page 28: Hash Tables: Collision Resolution. 2 Overview Hash Tables Collisions Linear Probing Problems with Linear Probing Chaining.](https://reader035.fdocuments.us/reader035/viewer/2022070400/56649f155503460f94c2a77f/html5/thumbnails/28.jpg)
28
0
1
2
3
6
4
5
Aho, Kruse, Standish, Horowiz, Langsam, Sedgwick, Knuth
Example: Chaining
0, 5, 1, 5, 5, 2, 1
3
1
2
Kruse Horowitz
Knuth
1
Standish
Aho
Sedgewick
Langsam
0
0
0
![Page 29: Hash Tables: Collision Resolution. 2 Overview Hash Tables Collisions Linear Probing Problems with Linear Probing Chaining.](https://reader035.fdocuments.us/reader035/viewer/2022070400/56649f155503460f94c2a77f/html5/thumbnails/29.jpg)
29
Hashtable with ChainingHashtable with Chaining
• At each position in the array you have a list:
List hashTable[MAXTABLE];
0
1
2
1
2
1
:
![Page 30: Hash Tables: Collision Resolution. 2 Overview Hash Tables Collisions Linear Probing Problems with Linear Probing Chaining.](https://reader035.fdocuments.us/reader035/viewer/2022070400/56649f155503460f94c2a77f/html5/thumbnails/30.jpg)
30
Insert with ChainingInsert with Chaining
• Apply hash function to get a position in the array.
• Insert key into the Linked List at this position in the array.
![Page 31: Hash Tables: Collision Resolution. 2 Overview Hash Tables Collisions Linear Probing Problems with Linear Probing Chaining.](https://reader035.fdocuments.us/reader035/viewer/2022070400/56649f155503460f94c2a77f/html5/thumbnails/31.jpg)
31
module InsertChaining(item){ posHash = hash(key of item)
insert (hashTable[posHash], item); }
0
1
2
1
2 Knuth
1
Standish
Aho
Sedgewick
:
![Page 32: Hash Tables: Collision Resolution. 2 Overview Hash Tables Collisions Linear Probing Problems with Linear Probing Chaining.](https://reader035.fdocuments.us/reader035/viewer/2022070400/56649f155503460f94c2a77f/html5/thumbnails/32.jpg)
32
Search with ChainingSearch with Chaining
• Apply hash function to get a position in the array.
• Search the Linked List at this position in the array.
![Page 33: Hash Tables: Collision Resolution. 2 Overview Hash Tables Collisions Linear Probing Problems with Linear Probing Chaining.](https://reader035.fdocuments.us/reader035/viewer/2022070400/56649f155503460f94c2a77f/html5/thumbnails/33.jpg)
33
/* module returns NULL if not found, or the address of the * node if found */
module SearchChaining(item){ posHash = hash(key of item) Node* found;
found = searchList (hashTable[posHash], item);
return found;
}
0
1
2
1
2 Knuth
1
Standish
Aho
Sedgewick
:
![Page 34: Hash Tables: Collision Resolution. 2 Overview Hash Tables Collisions Linear Probing Problems with Linear Probing Chaining.](https://reader035.fdocuments.us/reader035/viewer/2022070400/56649f155503460f94c2a77f/html5/thumbnails/34.jpg)
34
Delete with ChainingDelete 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.
![Page 35: Hash Tables: Collision Resolution. 2 Overview Hash Tables Collisions Linear Probing Problems with Linear Probing Chaining.](https://reader035.fdocuments.us/reader035/viewer/2022070400/56649f155503460f94c2a77f/html5/thumbnails/35.jpg)
35
/* module uses the Linked list delete function to delete an item *inside that list, it does nothing if that item isn’t there. */
module DeleteChaining(item){ posHash = hash(key of item) deleteList (hashTable[posHash], item);}
0
1
2
1
2 Knuth
1
Standish
Aho
Sedgewick
:
![Page 36: Hash Tables: Collision Resolution. 2 Overview Hash Tables Collisions Linear Probing Problems with Linear Probing Chaining.](https://reader035.fdocuments.us/reader035/viewer/2022070400/56649f155503460f94c2a77f/html5/thumbnails/36.jpg)
36
Disadvantages of ChainingDisadvantages of Chaining
• Uses more space.
• More complex to implement.– A linked list at every element in the array
![Page 37: Hash Tables: Collision Resolution. 2 Overview Hash Tables Collisions Linear Probing Problems with Linear Probing Chaining.](https://reader035.fdocuments.us/reader035/viewer/2022070400/56649f155503460f94c2a77f/html5/thumbnails/37.jpg)
37
Advantages of ChainingAdvantages of Chaining
• Insertions and Deletions are easy and quick.
• Allows more records to be stored.
• Naturally resizable, allows a varying number of records to be stored.