You are on page 1of 19

Collision Resolution Techniques

• finding an alternate location is called collision resolution


• Direct Chaining: An array of linked list application
○ Separate chaining
• Open Addressing: Array-based implementation
• Linear probing (linear search)

• Quadratic probing (nonlinear search)

• Double hashing (use two hash functions)


Separate Chaining
• linked representation with hash table is combined
• When two or more records hash to the same location, these records
are constituted into a singly-linked list called a chain.
Let us consider a simple hash function as “key mod 7” and
sequence of keys as 50, 700, 76, 85, 92, 73, 101.
Open Addressing
• In open addressing all keys are stored in the hash table itself. This
approach is also known as closed hashing.
• This procedure is based on probing.
• A collision is resolved by probing.
Linear Probing
• Calculate the hash key. key = data % size;
• If hashTable[key] is empty, store the value directly. hashTable[key] =
data.
• If the hash index already has some value, check for next index.
• key = (key+1) % size;
• If the next index is available hashTable[key], store the value.
Otherwise try for next index.
• Do the above process till we find the space.
1 2
3
4
Issue with Linear probing

• the table contains groups of consecutively occupied locations that are


called clustering.
• Clustering causes long probe searches and therefore decreases the
overall efficiency.
• If we choose the table size to be a prime number, then any step-size is
relatively prime to the table size. Clustering cannot be avoided by
larger step-sizes.
Quadratic Probing
• The interval between probes increases proportionally to the hash
value
• Clustering can be eliminated
• In quadratic probing, we start from the original hash location i. If a
location is occupied, we check the locations i + 1^2 , i +2^2, i + 3^2, i +
4^2...
• Wrapping from the last table location to the first table location if
necessary.
• The function for rehashing is the following:
Limitations of quadratic probing
• Clustering is caused by multiple search keys mapped to the same hash
key.
• the probing sequence for such search keys is prolonged by repeated
conflicts along the probing sequence.
Double hashing

• Double hashing reduces clustering in a better way.


• The increments for the probing sequence are computed by using a
second hash function.
• The second hash function h2 should be:

• We first probe the location h1(key). If the location is occupied, we


probe the location h1(key) + h2(key), h1(key) + 2 * h2(key), ...
Example:
Table size is 11 (0..10)
M=11, h(x) = x mod 11, h2(x) = x mod 7 +
1
Hash: 14, 17, 25, 37, 34, 16, 26 0:
Probes 1: 34
14 mod 11 = 3 1 2:
17 mod 11 = 6 1 3: 14
25 mod 11 = 3 2 25 mod 7 + 1 = 5 4: 37
37 mod 11 = 4 1 5: 16
34 mod 11 = 1 1 6: 17
16 mod 11 = 5 1 7:
26 mod 11 = 4 2 26 mod 7 + 1 = 6 8: 25
---- 9:
9 probes or 9/7 ~ 1 probe per key 10: 26

http://lti.cs.vt.edu/OpenDSA/Exercises/Hashing/HashDou
blePPRO.html

You might also like