collision resolution techniques
collision resolution techniques
Example:
0 600
2 50 74 14
3 99
4
5 113
(As collision occurs after inserting keys 74 and 14, they are added to the
chain)
Open addressing stores all entry records within the array itself, as opposed to
linked lists. The phrase 'open addressing' refers to the notion that the hash
value of an item does not identify its location or address. In order to insert a
new entry, the array is first checked before computing the hash index of the
hashed value, starting with the hashed index. If the space at the hashed index
is empty, the entry value is inserted there; otherwise, some probing sequences
are used until an empty slot is found.
In Closed hashing, there are three techniques that are used to resolve the
collision:
A. Linear Probing
Linear probing involves systematically checking the hash table from its very
beginning. A different site is searched if the one received is already occupied.
In linear probing, the interval between the probes is usually fixed (generally,
to a value of 1).
index = ( hash(n) % T)
(hash(n) + 1) % T
(hash(n) + 2) % T
(hash(n) + 3) % T … and so on.
Example:
1 3 3%20 3 3
2 2 2%20 2 2
3 46 46%20 6 6
4 6 6%20 6 7
5 11 11%20 11 11
6 13 13%20 13 13
7 53 53%20 13 14
8 12 12%20 12 12
9 70 70%20 10 10
INDEX
SL. NO KEY HASH INDEX
(AFTER LINEAR PROBING)
10 90 90%20 10 11
B. Quadratic Probing
The only distinction between linear and quadratic probing is the space
between succeeding probes or entry slots. When a hashed index slot for an
entry record is already taken, you must start traversing until you discover an
open slot. The spacing between slots is calculated by adding each subsequent
value of any arbitrary polynomial in the initial hashed index.
The hash(n) is the index computed using a hash function, and T is the
table size.
If slot index = ( hash(n) % T) is full, then the next slot index is calculated
by adding 1 (hash(n) + 1 x 1) % T
The sequence goes as -
index = ( hash(n) % T)
(hash(n) + 1 x 1) % T
(hash(n) + 2 x 2) % T
(hash(n) + 3 x 3) % T … and so on
Example:
1 22 22%7 1 1
2 30 30%7 2 2
5 50 50%7 1 1(1+2 x 2)
index = hash(x) % S
(hash(x) + 1*hash2(x)) % S
(hash(x) + 2*hash2(x)) % S
(hash(x) + 3*hash2(x)) % S … an so on
Example:
SL. INDEX
KEY HASH INDEX
NO (AFTER DOUBLE HASHING)
1 43 43%7 1
[h1(92) + i * (h2(92)] % 7
= [6 + 1 * (1 + 92 % 5)] % 7
2 92 92%7 6
= 9 % 7
=2
4
SL. INDEX
KEY HASH INDEX
NO (AFTER DOUBLE HASHING)
[h1(72) + i * (h2(72)] % 7
= [2 + 1 * (1 + 72 % 5)] % 7
5 72 72%7 2
= 5 % 7
=5
6 27 27%7 6