Collision Resolution: Ananda Gunawardena
Collision Resolution: Ananda Gunawardena
Collision Resolution: Ananda Gunawardena
Ananda Gunawardena
Hashing
Big Idea in Hashing
Let S={a1,a2,…am} be a set of objects that we need to
map into a table of size N.
Find a function such that H:S [1…n]
Ideally we’d like to have a 1-1 map
But it is not easy to find one
Also function must be easy to compute
Also picking a prime as the table size can help to have a
better distribution of values
Collisions
Two keys mapping to the same location in the
hash table is called “Collision”
Collisions can be reduced with a selection of a
good hash function
But it is not possible to avoid collisions altogether
Unless we can find a perfect hash function
Which is hard to do
Collision Resolving strategies
Few Collision Resolution ideas
Separate chaining
Some Open addressing techniques
Linear Probing
Quadratic Probing
Separate Chaining
Separate Chaining
Collisions can be resolved by creating a list of keys that
map to the same value
Separate Chaining
Use an array of linked lists
LinkedList[ ] Table;
Table = new LinkedList(N), where N is the table size
Define Load Factor of Table as
λ = number of keys/size of the table
(λ
λ can be more than 1)
Still need a good hash function to distribute keys
evenly
For search and updates
Linear Probing
Linear Probing
The idea:
Table remains a simple array of size N
On insert(x), compute f(x) mod N, if the cell is full, find
another by sequentially searching for the next available
slot
Go to f(x)+1, f(x)+2 etc..
On find(x), compute f(x) mod N, if the cell doesn’t
match, look elsewhere.
Linear probing function can be given by
h(x, i) = (f(x) + i) mod N (i=1,2,….)
Figure 20.4
Linear probing
hash table after
each insertion
Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley
Linear Probing Example
Consider H(key) = key Mod 6 (assume N=6)
H(11)=5, H(10)=4, H(17)=5, H(16)=4,H(23)=5
Draw the Hash table
0 0 0 0 0 0
1 1 1 1 1 1
2 2 2 2 2 2
3 3 3 3 3 3
4 4 4 4 4 4
5 5 5 5 5 5
Clustering Problem
• Clustering is a significant problem in linear probing. Why?
• Illustration of primary clustering in linear probing (b) versus no clustering
(a) and the less significant secondary clustering in quadratic probing (c).
Long lines represent occupied cells, and the load factor is 0.7.
Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley
Deleting items from a hash table
Deleting items
How about deleting items from Hash table?
Item in a hash table connects to others in the table(eg:
BST).
Deleting items will affect finding the others
“Lazy Delete” – Just mark the items as inactive rather
than removing it.
Lazy Delete
Naïve removal can leave gaps!
Insert f
0a 0a
Remove 0ea 0a
Find f
2b 2b 2b 2b
3c 3c 3c 3c
3e 3e
5d 5d 5d 5d
3f 3f 3f
8j 8j 8j 8j
8u 8u 8u 8u
10 g 10 g 10 g 10 g
8s 8s 8s 8s
8j 8j 8j 8j
8u 8u 8u 8u
10 g 10 g 10 g 10 g
8s 8s 8s 8s
Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley