J.E.D.I
KEY1 KEY2
KEY3
12
792 Collision is redefined in this approach. It happens when a bucket overflows – that is,
when an insertion is attempted on a full bucket. Hence, there is a significant reduction in the number of collisions. However, this method wastes some space and is not free from
overflowing further, in which case an overflow policy must be invoked. In the above example, there are three slots in each address. Being static in size, problem arises when
more than three values hash to a single address.
10.4.3 Open Addressing Probing
In open addressing, when the address produced by a hash function hk has no more space for insertion, an empty slot other than hk in the hash table is located. This
process is called probing. In this empty slot, the new record that has collided with the old one, known as the colliding key, could be safely placed. In this method, we
introduce the permutation of the table addresses, say
β ,
β
1
, ... β
m-1
, This permutation is
called probe sequence.
In this section, we will cover two techniques: linear probing and double hashing.
10.4.3.1 Linear Probing
Linear probing is one of the simplest techniques in handling collisions in which the file is scanned or probed sequentially as a circular file and the colliding key is stored in the
nearest available space to the address. This is used in systems where a “first come, first served” basis is followed. An example is an airline reservation system where seats for
chance passengers are given away when passengers for reserved seats don’t arrive. Whenever a collision occurs at a certain address, the succeeding addresses are probed,
that is, searched sequentially until an empty one is found. The key then uses this address. Array should be considered circular, so that when the last location is reached,
the search proceeds to the first location of the array.
In this method, it is possible that the entire file could be searched, starting at position i+1, and the colliding keys are distributed throughout the file. If a key hashes to position
i, which is occupied, positions i+1, …,n are searched for an empty location.
The slots in a hash table may contain only one key or could also be a bucket. In this example, buckets of capacity 2 are used to store the following keys:
Key Value k Hash Value hk
Key Value k Hash Value hk
125 8
234 845
431 2
444 2
947 11
256 9
981 6
345 7
792 12
745 4
459 4
Data Structures 191
J.E.D.I
Key Value k Hash Value hk
Key Value k Hash Value hk
902 5
725 10
569 10
652 2
254 7
421 5
382 5
458 3
resulting to the following hash table: KEY1
KEY2 845
234
1 2
444 431
3
652 458
4
745 459
5
902 382
6
981 421
7
345 254
8
125
9
256
10
569 725
11
947
12
792 In this technique, key 642 hashed to address 2 but it is already full. Probing for the next
available space led to address 3 where key is safely stored. Later in the insertion process, the key 458 hash to address 3 and it is stored on the second slot in the address.
With key 421 that hashed to a full address 5, the next available space is at address 6, where the key is stored.
This approach resolves the problem of overflowing in bucket addressing. Also, probing for available space makes the storage of overflowed keys near its home address in most
cases. However, this method suffers from displacement problem where the keys that rightfully owns an address may be displaced by other keys that just probed to the said
address. Also, probing an full hash table would entail a time complexity of On.
10.4.3.2 Double Hashing