Copyright @ 2009 Ananda Gunawardena
Lecture 16
More on Hashing
Collision Resolution
Introduction
In this lesson we will discuss several collision resolution strategies. The key thing in
hashing is to find an easy to compute hash function. However, collisions cannot be
avoided. Here we discuss three strategies of dealing with collisions, linear probing,
quadratic probing and separate chaining.
Linear Probing
Suppose that a key hashes into a position that is already occupied. The simplest strategy
is to look for the next available position to place the item. Suppose we have a set of hash
codes consisting of {89, 18, 49, 58, 9} and we need to place them into a table of size 10.
The following table demonstrates this process.
Table Courtesy of Weiss Data Structures Book
The first collision occurs when 49 hashes to the same location with index 9. Since 89
occupies the A[9], we need to place 49 to the next available position. Considering the
array as circular, the next available position is 0. That is (9+1) mod 10. So we place 49 in
A[0]. Several more collisions occur in this simple example and in each case we keep
looking to find the next available location in the array to place the element. Now if we
need to find the element, say for example, 49, we first compute the hash code (9), and
look in A[9]. Since we do not find it there, we look in A[(9+1) % 10] = A[0], we find it
there and we are done. So what if we are looking for 79? First we compute hashcode of
This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentCopyright @ 2009 Ananda Gunawardena
79 = 9. We probe in A[9], A[(9+1)%10]=A[0], A[(9+2)%10]=A[1], A[(9+3)%10]=A[2],
A[(9+4)%10]=A[3] etc. Since A[3] = null, we do know that 79 could not exists in the set.
Lazy Deletion
This is the end of the preview. Sign up
to
access the rest of the document.
 Fall '08
 bajkzek
 hash function, Separate Chaining, Ananda Gunawardena

Click to edit the document details