Articles / DSA · Part 29

Resolving Collisions: Chaining and Open Addressing (Rehashing)

Learn how hash-table collisions are resolved using Separate Chaining, Linear Probing, Quadratic Probing, and Double Hashing, and how Rehashing improves performance.

Resolving Collisions: Chaining and Open Addressing (Rehashing)

1. Introduction

In the previous article, we learned that collisions occur when two different keys produce the same hash-table index.

For example, using the hash function:

\[
h(k)=k\bmod 7
\]

Both keys 14 and 21 map to index 0:

\[
h(14)=0,\quad h(21)=0
\]

Since both cannot occupy the same single slot in a basic hash table, we need techniques to handle collisions.

The two major approaches are:

    1. Separate Chaining
  1. Open Addressing

Open Addressing includes Linear Probing, Quadratic Probing, and Double Hashing.

2. Separate Chaining

Separate Chaining handles collisions by allowing each hash-table index to store multiple elements, commonly using a linked list.

Instead of storing only one key at an index, we store a chain of keys that map to that same index.

Example

Using \(h(k)=k\bmod 7\), insert the keys:

14, 21, 28, 35

All four keys map to index 0.

Instead of overwriting one another, they are stored in a chain:

Hash Table

Index Stored Keys
0 14 → 21 → 28 → 35
1 Empty
2 Empty
3 Empty
4 Empty
5 Empty
6 Empty

All keys that produce index 0 are stored in the linked list associated with that index.

How Operations Work

  • Insertion: Calculate the hash index and insert the key into that index’s chain.
  • Search: Calculate the index, then search through its chain.
  • Deletion: Find the key in the chain and remove it.

Advantages

  • Multiple keys can share the same index.
  • The table can store more elements than its number of indexes.
  • Deletion is relatively straightforward.
  • It does not require finding another empty slot in the main array.

Disadvantages

  • Requires additional memory for linked-list pointers.
  • Searching becomes slower when chains become long.
  • Poor hash functions can create long chains.

3. Open Addressing

Open Addressing handles collisions by finding another available position within the hash table itself.

Unlike Separate Chaining, all keys are stored directly in the table array.

When a collision occurs, the algorithm follows a probing sequence to find another slot.

The three commonly used techniques are:

  • Linear Probing
  • Quadratic Probing
  • Double Hashing

4. Linear Probing

Linear Probing checks the next available index sequentially when a collision occurs.

If the calculated index is occupied, it checks the next slot, then the next, until an empty slot is found.

The formula is:

\[
h(k,i)=(h(k)+i)\bmod m
\]

Where:

  • (h(k)) is the original hash index.
  • (i) is the probe number.
  • (m) is the table size.

Example

Insert 14, 21, and 28 into a table of size 7.

Using:

\[
h(k)=k\bmod 7
\]

All three keys initially map to index 0.

Insertion of 14:

Index 0 is empty, so 14 is stored there.

Insertion of 21:

Index 0 is occupied. Check index 1, which is empty. Store 21 there.

Insertion of 28:

Index 0 is occupied. Index 1 is also occupied. Check index 2 and store 28 there.

Final Table

Index Value
0 14
1 21
2 28
3 Empty
4 Empty
5 Empty
6 Empty

This technique is simple and easy to implement.

Limitation: Primary Clustering

Linear Probing can create groups of consecutive occupied slots.

These groups are called clusters.

As clusters grow, more positions may need to be checked during insertion and searching, reducing performance.

5. Quadratic Probing

Quadratic Probing reduces primary clustering by using quadratic increments instead of checking consecutive slots.

Its formula is:

\[
h(k,i)=(h(k)+i^2)\bmod m
\]

Instead of checking:

0, 1, 2, 3, 4, …

It may check:

0, 1, 4, 2, 2, …

The exact sequence depends on the starting index and table size.

Example

Again, insert 14, 21, and 28 into a table of size 7.

All initially hash to index 0.

Insertion of 14: Store at index 0.

Insertion of 21:

  • \(i=1\): \((0+1^2)\bmod 7=1\)
  • Index 1 is empty, so store 21 there.

Insertion of 28:

  • \(i=1\): \((0+1^2)\bmod 7=1\), occupied.
  • \(i=2\): \((0+2^2)\bmod 7=4\), empty.

Store 28 at index 4.

Final Table

Index Value
0 14
1 21
2 Empty
3 Empty
4 28
5 Empty
6 Empty

Quadratic Probing avoids checking consecutive slots, reducing primary clustering.

However, keys with the same initial hash index follow the same probing sequence, which can cause secondary clustering

Also, depending on the table size and formula, quadratic probing may not visit every slot. The table size and probing formula must be selected carefully.

6. Double Hashing

Double Hashing uses two hash functions.

The first function calculates the initial index, while the second determines the step size used when a collision occurs.

The formula is:

\[
h(k,i)=(h_1(k)+i\cdot h_2(k))\bmod m
\]

Where:

  • \(h_1(k)\) determines the initial index.
  • \(h_2(k)\) determines the step size.
  • \(i\) is the probe number.

Example

Let the table size be 7.

Use:

\[
h_1(k)=k\bmod 7
\]

\[
h_2(k)=1+(k\bmod 5)
\]

Insert keys 14, 21, and 28.

All three have the same initial index:

\[
h_1(14)=h_1(21)=h_1(28)=0
\]

Insertion of 14:

Index 0 is empty. Store 14.

Insertion of 21:

  • Initial index = 0, occupied.
  • Step size = \(1+(21\bmod 5)=2\).
  • Next index = \((0+2)\bmod 7=2\).

Store 21 at index 2.

Insertion of 28:

  • Initial index = 0, occupied.
  • Step size = \(1+(28\bmod 5)=4\).
  • Next index = \((0+4)\bmod 7=4\).

Store 28 at index 4.

Final Table

Index Value
0 14
1 Empty
2 21
3 Empty
4 28
5 Empty
6 Empty

Double Hashing generally distributes keys more effectively than Linear or Quadratic Probing because different keys can have different step sizes.

For reliable coverage of the table, the second hash function should produce a step size relatively prime to the table size.

7. Rehashing

As more keys are inserted, a hash table may become crowded, causing more collisions and slower operations.

Rehashing is the process of creating a larger hash table and inserting the existing keys again using the new table size or hash function.

Example

Suppose the original table size is 7 and uses:

\[
h(k)=k\bmod 7
\]

Keys 14, 21, and 28 all map to index 0, creating collisions.

We increase the table size to 13 and reinsert the keys using:

\[
h(k)=k\bmod 13
\]

Now:

Key Old Index (mod 7) New Index (mod 13)
14 0 1
21 0 8
28 0 2

The keys are now distributed across different indexes, reducing collisions.

Important: Rehashing does not simply copy keys to their old indexes. Every key must be hashed again because the table size or hash function has changed.

Rehashing takes \(O(n)\) time for \(n\) stored keys, but it happens occasionally. This helps maintain efficient average performance over many insertions.

8. Comparison of Collision Resolution Techniques

Technique How it works Main limitation
Separate Chaining Stores colliding keys in a linked list or chain Extra memory and potentially long chains
Linear Probing Checks the next consecutive slot Primary clustering
Quadratic Probing Uses quadratic jumps Secondary clustering; may not visit every slot
Double Hashing Uses a second hash function for step size Requires carefully chosen hash functions

9. Applications

Collision resolution techniques are important in:

  • Hash maps and dictionaries.
  • Database indexes.
  • Caching systems.
  • Compiler symbol tables.
  • Fast lookup systems.
  • Memory-efficient data storage.

Different implementations choose different collision-handling techniques depending on their performance, memory, and workload requirements.

Collisions are a natural part of hashing, but they can be handled efficiently.

Separate Chaining stores multiple keys at the same hash index using a chain. Open Addressing finds alternative positions inside the table using Linear Probing, Quadratic Probing, or Double Hashing.

Rehashing helps maintain performance by increasing the table size and redistributing existing keys.

Understanding these techniques completes our study of hashing and prepares us to use hash-based data structures effectively in real applications.

Keep a copy

Read offline as a printout or PDF.

farhanpaal.com

Keep reading

DSA · Part 1

What is DSA?

Learn DSA from the basics with this beginner-friendly guide to data structures and algorithms. Understand arrays, linked lists, stacks, queues, trees, graphs,…

8 min read

Ask Farhan

Ask anything about Farhan