As mentioned before there is the problem of collisions, that is, when two distinct keys hash to the same address.
The first point to be made about this problem is that it destroys some of the simplicity of hashing.
Initially it may have been thought that the key need not be stored with the data at the hash address.
Unfortunately this is not so.
No matter what method we use to resolve collisions we still need to store the key with the data so that at search time when a key is hashed we can distinguish its data from the data associated with keys which have hashed to the same address.
There are a number of strategies for dealing with collisions.
Essentially they fall into two classes, those which use pointers to link together collided keys and those which do not.
Let us first look at the ones which do not use pointers.
These have a mechanism for searching the store, starting at the address where the collision occurred, for an empty storage location if a record needs to be inserted, or, for a matching key value at retrieval time.
The simplest of these advances from the hash address each time moving along a fixed number of locations, say s, until an empty location or the matching key value is found.
The collision strategy thus traces out a well defined sequence of locations.
This method of dealing with collisions is called the linear method.
The tendency with this method is to store collided records as closely to the initial hash address as possible.
This leads to an undesirable effect called primary clustering. In this context all this means is that the records tend to concentrate in groups or bunch-up.
It destroys the uniform nature of the hashing function.
To be more precise, it is desirable that hash addresses are equally likely, however, the first empty location at the end of a collision sequence increases in likelihood in proportion to the number of records in the collision sequence.
To see this one needs only to realise that a key hashed to any location in the sequence will have its record stored at the end of the sequence.
Therefore big groups of records tend to grow even bigger.
This phenomenon is aggravated by a small step size s when seeking an empty location. Sometimes s = 1 is used in which case the collision strategy is known as the open addressing technique.
Primary clustering is also worse when the hash table (available storage) is relatively full.
Variations in the linear method which avoid primary clustering involve making the step size a variable.
One way is to set s equal to ai + bi^2 on the ith step. Another is to invoke a random number generator which calculates the step size afresh each time. These last two collision handling methods are called the quadratic and random method respectively. Although they avoid primary clustering they are nevertheless subject to secondary clustering, which is caused by keys hashing to the same address and following the same sequence in search |