3.6. Collision Resolution¶
3.6.1. Collision Resolution¶
We now turn to the most commonly used form of hashing: closed hashing with no bucketing, and a collision resolution policy that can potentially use any slot in the hash table.
During insertion, the goal of collision resolution is to find a free slot in the hash table when the home position for the record is already occupied. We can view any collision resolution method as generating a sequence of hash table slots that can potentially hold the record. The first slot in the sequence will be the home position for the key. If the home position is occupied, then the collision resolution policy goes to the next slot in the sequence. If this is occupied as well, then another slot must be found, and so on. This sequence of slots is known as the probe sequence, and it is generated by some probe function that we will call p. Insertion works as follows.
# Insert record E with key value K into hash table HT
def hashInsert(E, K):
pos = home = h(K) # Init probe sequence
i = 1
while HT[pos].key != EMPTYKEY: # e.g. EMPTYKEY = None
if K == HT[pos].key # Duplicates not allowed
return
pos = (home + p(K, i)) % M # Probe
i += 1
HT[pos].elem = E
HT[pos].key = K
Method hashInsert
first checks to see if the home slot for the
key is empty.
If the home slot is occupied, then we use the probe function
\(\textbf{p}(k, i)\) to locate a free slot in the table.
Function p has two parameters, the key \(k\) and a
count \(i\) of where in the probe sequence we wish to be.
That is, to get the first position in the probe sequence after the
home slot for key \(K\), we call \(\textbf{p}(K, 1)\).
For the next slot in the probe sequence, call \(\textbf{p}(K, 2)\).
Note that the probe function returns an offset from the original home
position, rather than a slot in the hash table.
Thus, the for
loop in hashInsert
is computing positions
in the table at each iteration by adding the value returned from the
probe function to the home position.
The \(i\) th call to p returns the \(i\) th offset to be used.
Searching in a hash table follows the same probe sequence that was followed when inserting records. In this way, a record not in its home position can be recovered. An implementation for the search procedure is as follows.
# search record with key value K in the hashtable HT
def hashSearch(K):
home = pos = h(K) # Home and inital positions for key
i = 1
while key != HT[pos].key and EMPTYKEY != HT[pos].key:
pos = (home + p(K, i)) % M # Next on probe sequence
i += 1
return HT[pos].elem # return EMPTYELEM if pos is not occupied in HT
Both the insert and the search routines assume that at least one slot on the probe sequence of every key will be empty. Otherwise they will continue in an infinite loop on unsuccessful searches. Thus, the hash system should keep a count of the number of records stored, and refuse to insert into a table that has only one free slot.
The simplest approach to collision resolution is simply to move down the table from the home slot until a free slot is found. This is known as linear probing. The probe function for simple linear probing is \(\textbf{p}(K, i) = i\). That is, the \(i\) th offset on the probe sequence is just \(i\), meaning that the \(i\) th step is simply to move down \(i\) slots in the table. Once the bottom of the table is reached, the probe sequence wraps around to the beginning of the table (since the last step is to mod the result to the table size). Linear probing has the virtue that all slots in the table will be candidates for inserting a new record before the probe sequence returns to the home position.
Can you see any reason why this might not be the best approach to collision resolution?
3.6.2. The Problem with Linear Probing¶
While linear probing is probably the first idea that comes to mind when considering collision resolution policies, it is not the only one possible. Probe function p allows us many options for how to do collision resolution. In fact, linear probing is one of the worst collision resolution methods. The main problem is illustrated by the next slideshow.
Again, the ideal behavior for a collision resolution mechanism is that each empty slot in the table will have equal probability of receiving the next record inserted (assuming that every slot in the table has equal probability of being hashed to initially). This tendency of linear probing to cluster items together is known as primary clustering. Small clusters tend to merge into big clusters, making the problem worse. The objection to primary clustering is that it leads to long probe sequences.