Double Hashing for Conflict Resolution in Hash Tables

286 views Hash Table Internals

Linear and Quadratic probing do a great job at handling collisions in a Hash Table, but they both suffer from Clustered Collision, which degrades performance. So, can we do better?

Double hashing is a technique that minimizes the problem of clustered collisions by using a secondary hash function to find the next available slot.

Double Hashing

Double hashing is an Open Addressing technique to address collisions in a hash table; hence, instead of using an auxiliary data structure to hold the collided keys, it leverages the already available free slots.

The probing function for Double Hashing is defined as

p(k, i) = (h1(k) + i * h2(k)) mod m

Thus, with every attempt we make to find an empty slot, we can leap by any distance in any direction, making all slots equally likely. A sample sequence for a particular key k1 could be

  • primary slot: h1(k1), if that is occupied then
  • attempt 1: h1(k1) + h2(k1), if that is occupied then
  • attempt 2: h1(k1) + 2 * h2(k1), if that is occupied then
  • attempt 3: h1(k1) + 3 * h2(k1), and so on

Linear probing and quadratic traversals take a predictable leap to hunt for an empty slot, while double hashing probing leaps depend on the key and hence reduce the chances of clustering. So, different keys will have different leaps.

Choosing a second hash function

The second hash function is super-critical, as it is aimed at resolving collisions effectively while ensuring minimal clustering. The second hash function should

  1. never return 0
  2. cycle through the entire table (with no particular order)
  3. be fast to compute and almost feel like a random number generator

Advantages of Double Hashing

  1. Uniform spread upon collision
  2. follows no specific offset pattern, the key purely depends on
  3. least prone to the clustering problem

Disadvantage of Double Hashing

Double hashing is not cache-friendly, as it requires us to hop across the hash table to hunt an empty slot. We may be at one extreme of the table and then move to the other one.

Arpit Bhayani

Arpit's Newsletter

CS newsletter for the curious engineers

❤️ by 17000+ readers

If you like what you read subscribe you can always subscribe to my newsletter and get the post delivered straight to your inbox. I write essays on various engineering topics and share it through my weekly newsletter.

Other essays that you might like

Implementing Hash Maps with Hash Tables

425 views 15 likes 2022-08-03

Maps and Dictionaries are amazing, but how are they implemented? In this 11th video of this Hash Table Internals series...

Implementing Hash Sets with Hash Tables

259 views 6 likes 2022-08-01

Sets are amazing, but how are they implemented? In this 10th video of this Hash Table Internals series, we take an in-d...

Implementing Resize of a Hash Table

257 views 9 likes 2022-07-29

So, the Hash Table needs to be resized in order to maintain consistent performance, but how exactly? In this 9th Video ...

Why are Hash Tables always doubled?

770 views 19 likes 2022-07-27

Why are the underlying arrays of the hash tables always a power of 2? When we trigger a resize why are Hash Tables alway...

Be a better engineer

A set of courses designed to make you a better engineer and excel at your career; no-fluff, pure engineering.

System Design Masterclass

A masterclass that helps you become great at designing scalable, fault-tolerant, and highly available systems.

Enrolled by 700+ learners

Details →

Designing Microservices

A free course to help you understand Microservices and their high-level patterns in depth.

Enrolled by 17+ learners

Details →

GitHub Outage Dissections

A free course to help you learn core engineering from outages that happened at GitHub.

Enrolled by 67+ learners

Details →

Hash Table Internals

A free course to help you learn core engineering from outages that happened at GitHub.

Enrolled by 25+ learners

Details →

BitTorrent Internals

A free course to help you understand the algorithms and strategies that power P2P networks and BitTorrent.

Enrolled by 42+ learners

Details →

Topics I talk about

Being a passionate engineer, I love to talk about a wide range of topics, but these are my personal favourites.

Arpit's Newsletter read by 17000+ engineers

🔥 Thrice a week, in your inbox, an essay about system design, distributed systems, microservices, programming languages internals, or a deep dive on some super-clever algorithm, or just a few tips on building highly scalable distributed systems.

  • v12.4.4
  • © Arpit Bhayani, 2022

Powered by this tech stack.