Skip to content

Collisions: separate chaining

Resolve collisions with a list in each bucket, relate the load factor n/m to the expected cost of a search, and keep that cost constant by resizing and rehashing.

It opens on

Five keys go into 5 buckets with key % 5: 12, 25, 31, 40 and 7. How many comparisons does a search for 7 make, 1 or 2?

Why it matters

Separate chaining is the simplest working hash table and the model behind Java's HashMap; the load factor argument, a successful search about 1 + α/2 and a failed one about α, and doubling to keep α bounded are what exams and interviews ask when they ask why hash tables are O(1).

Good to know first

  • Hash functions and hash tables: bucket = key % m and collisions
  • Dynamic arrays and amortized cost: doubling makes n appends O(n) in all
  • Lists: indexing and mutation: append and in

Step by step

  1. A list in each bucket

    Insert keys into a chained table, draw the chains, and count the comparisons of successful and failed searches.

    With m = 5, 12 % 5 = 2, 25 % 5 = 0, 31 % 5 = 1, 40 % 5 = 0 and 7 % 5 = 2. So 40 joins 25 in bucket 0, and 7 joins 12 in bucket 2.

    A common mix-up

    A collision means the second key is lost or overwrites the first.

    In fact: With chaining both keys stay, one after the other in the same bucket's list.

  2. The load factor and the cost

    Define the load factor α = n/m, and give the expected comparisons of failed and successful searches with even spreading.

    The run measured 1.48 at α = 1, 1.22 at 0.5 and 2.02 at 2, close to 1.25, 1.5 and 2 from the formula. Failed searches came out at 0.49, 1.03 and 1.99, close to α.

    measured averages, both

    A common mix-up

    A hash table is O(1) whatever its size.

    In fact: Only while α = n/m stays bounded; a fixed m makes search grow linearly with n.

  3. Resizing

    Resize a chained table when α passes 1 by doubling m and rehashing every key, and explain why inserts stay O(1) amortized.

    With m = 10, 25 moves from bucket 0 to 5, and 40 stays at 0. 12 stays at 2, 7 moves to 7, and 31 stays at 1.

    A common mix-up

    Resizing can copy each chain to the same bucket number in the bigger table.

    In fact: a key's bucket is key % m, so it changes with m and every key must be rehashed.

Try it

Suppose the table never grew. Insert 15 and 20 into the table above. Which chain do they join, and what does a search for 20 cost?Answer

Bucket 0, making the chain 25, 40, 15, 20; a search for 20 takes 4 comparisons

Hint: Both are multiples of 5

A chained table has 300 keys in 100 buckets. About how many comparisons does a failed search make, and a successful one?Answer

About 3 and about 2.5

Hint: α = 3

Continue by inserting 43 into the 10-bucket table. Where does it go, and is a resize needed?Answer

Bucket 3, n = 7 and α = 0.7, so no resize

Hint: The limit is 1

Which entries does a search for 7 compare it with before it stops?Answer

12 and then 7, the chain of bucket 2, so 2 comparisons.

At α = 1, what should a successful search cost by the formula?Answer

About 1 + 1/2 = 1.5 comparisons.

Collisions: separate chaining

30 minutes every month. No card.