Skip to content
Part IA Lent Term

Load Factor and Resizing

The Load Factor

The load factor α=n/m\alpha = n/m measures how full a hash table is, where nn is the number of stored pairs and mm is the table size (number of slots).

As α\alpha increases:

  • For chaining: average chain length grows, so search and insert expected time grows as O(1+α)O(1 + \alpha).
  • For open addressing: the probability of finding an occupied slot increases sharply, and the expected number of probes grows as 11α\frac{1}{1-\alpha} (unsuccessful search) or 1αln11α\frac{1}{\alpha}\ln\frac{1}{1-\alpha} (successful search).

Why Resizing Is Necessary

A hash table with fixed capacity mm will eventually become overloaded if nn grows. Without resizing:

  • Chaining: chain lengths grow linearly with nn, degrading to O(n)O(n) per operation.
  • Open addressing: the table fills completely, and insertions fail (or probe sequences become impractically long before that).

Resizing is the operation of creating a new, larger table and rehashing all existing keys into it.

Resizing Strategy

When the load factor exceeds a threshold, double the table size (or increase to the next suitable prime):

  1. Allocate a new table of size m2mm' \approx 2m.
  2. For each key in the old table, compute h(k)modmh'(k) \bmod m' and insert into the new table.
  3. Replace the old table with the new one.

Thresholds: typically 0.750.75 for chaining (some implementations use 1.01.0) and 0.50.5 for open addressing (the expected probe count at α=0.5\alpha = 0.5 is only 2 for unsuccessful search).

Amortised Analysis of Resizing

A single resize costs Θ(n)\Theta(n) because every key must be rehashed. If this happened on every insert, the insert cost would be Θ(n)\Theta(n), defeating the purpose.

However, resizing happens infrequently. If the table doubles each time:

  • After mm inserts (from size m/2m/2 to mm), one resize costs Θ(m)\Theta(m).
  • The m/2m/2 inserts since the last resize each get charged an amortised Θ(1)\Theta(1) for the resize cost, plus their own Θ(1)\Theta(1) insert cost.

This is identical to the amortised analysis of dynamic arrays: the total cost of nn inserts starting from an empty table is Θ(n)\Theta(n), so the amortised cost per insert is Θ(1)\Theta(1).

Downsizing

If many deletions reduce nn significantly (e.g. α<0.25\alpha < 0.25), the table can be downsized to reclaim memory. The same amortised argument applies: periodic downsizing adds Θ(1)\Theta(1) amortised cost per deletion.

Avoid aggressive downsizing (e.g. at α=0.5\alpha = 0.5), which could cause thrashing: a sequence of insert-delete-insert oscillating around the threshold, triggering repeated O(n)O(n) resizes.

Example: Resizing in Practice

Start with m=4m = 4, threshold =0.75= 0.75.

Insert 1: n=1, α=0.25, table=[1,_,_,_]
Insert 2: n=2, α=0.50, table=[1,2,_,_]
Insert 3: n=3, α=0.75, table=[1,2,3,_]
Insert 4: n=4, α=1.00 > 0.75 → RESIZE to m=8
           Rehash all 4 existing keys into new table.
           Total cost: Θ(4) for resize + Θ(1) for insert 4.

After resize, α=4/8=0.50\alpha = 4/8 = 0.50, well within the threshold.

Performance of Open Addressing with Resizing

With open addressing, tombstones also consume slots. When the fraction of tombstones becomes large, searches probe through many dead entries. A rehash into a same-size (or larger) table clears tombstones and restores performance. Maintain nkn_k (live keys) and nmn_m (tombstones); trigger rehash when nk+nmn_k + n_m exceeds threshold.

Summary

QuantityMeaning
α=n/m\alpha = n/mLoad factor
Chaining thresholdTypically 0.750.751.01.0
Open addressing thresholdTypically 0.50.5
Resize costΘ(n)\Theta(n) (rehash all keys)
Amortised insert costΘ(1)\Theta(1) (same analysis as dynamic arrays)
Resize triggerα\alpha exceeds threshold (or too many tombstones)
DownsizingReclaim memory when α\alpha becomes very small