3.3.6 · D2Hashing

Visual walkthrough — Load factor — when to resize, rehashing cost

2,071 words9 min readBack to topic

This is the deep-dive companion to the parent note. If a word here feels new, we define it before we use it. Prerequisite ideas live at Hashing, Dynamic Arrays, and Amortized Analysis.


Step 1 — What a hash table is, as a picture

WHAT. A hash table is a row of numbered boxes. We call each box a bucket. The number of buckets is (think " = mailboxes"). The number of keys we have actually stored is (think " = number of things"). A key is just a thing we want to remember — a name, a number, a word.

WHY start here. Every symbol on this page is one of , , or built from them. If we don't picture the boxes first, the algebra later is meaningless.

PICTURE. Below, the row of white boxes is the table. boxes exist; of them hold an amber key. The rest are empty.

Figure — Load factor — when to resize, rehashing cost

Step 2 — The load factor : how full the row is

WHAT. We define one number that measures "how crowded": The symbol (Greek letter "alpha") is just a name for the ratio . We earn it here: from now on always means "filled boxes divided by all boxes."

WHY this ratio and not, say, alone? Because crowdedness is relative. Three keys in 8 boxes () is roomy; three keys in 4 boxes () is tight. Only the ratio captures the feeling of "getting full," which is exactly what will trigger our resize.

PICTURE. Same 8 boxes as before but shaded by how full the row is. The fill bar underneath reads .

Figure — Load factor — when to resize, rehashing cost

Step 3 — The trigger: when crosses the threshold, we resize

WHAT. We pick a ceiling for crowdedness, called the max load factor (for this walkthrough , the simplest case; real tables use ). After each insert we check: is ? If yes, we build a bigger table.

WHY a threshold at all? In Separate Chaining the expected work per lookup is , and in Open Addressing it is about — both grow with . Keeping under a fixed ceiling keeps those costs constant. The threshold is the promise that keeps lookups fast.

PICTURE. A gauge: below we are in the green "insert normally" zone; the instant we cross it, the needle points to the amber "REBUILD" zone.

Figure — Load factor — when to resize, rehashing cost

Step 4 — Rehashing: why we can't just copy the boxes

WHAT. When we make a bigger table of size , a key does not stay in the same box number. Its box is computed as (see Hash Functions) — the raw hash number wrapped around to fit in boxes. Change to and the wrap changes, so the box changes. Moving every key into its new box is called rehashing.

WHY it costs . There are keys, and each one must be recomputed and re-placed. That's pieces of work — linear in the number of stored keys. One insert that triggers this suddenly does units of work instead of . That is the "scary" step we must tame.

PICTURE. Left: old table of , key with sits in box . Right: new table , same key now lands in box . The amber arrow shows it moved.

Figure — Load factor — when to resize, rehashing cost

Step 5 — The worry, drawn as a spike

WHAT. Let's plot the cost of each individual insert as we insert one key at a time. Most inserts cost (drop a key in an empty box). But the inserts that trigger a rehash spike up to the current .

WHY draw it. The picture makes the puzzle vivid: if some inserts cost , how can we ever call this table " per insert"? The answer is Step 6.

PICTURE. A bar chart of per-insert cost. Flat little bars of height , punctuated by tall amber spikes at inserts — exactly the powers of two, where a rehash fires.

Figure — Load factor — when to resize, rehashing cost
Recall Why do the spikes land on powers of two?

A table of size overflows (with ) right when the -th key arrives. Starting at , overflows happen going , i.e. on inserts — one more than each power of two.


Step 6 — Add up ALL the spikes (the key trick)

WHAT. Instead of asking "how bad is one spike?", we add up the total moving work across all inserts. When we double from size , the rehashes move , then , then , then , … keys — up to . Summing:

WHY sum instead of max? This is the heart of Amortized Analysis: a cost that is huge once in a while can be cheap on average. To see the average we need the total, then divide by the number of inserts.

PICTURE. Stack the spike heights as nested bars — a staircase where each step is double the one before. The tallest step () is bigger than all the shorter steps put together. That single visual fact is the whole proof.

Figure — Load factor — when to resize, rehashing cost

Step 7 — Collapse the sum with the geometric series

WHAT. The stacked bars are a geometric series: each term is the last, and the ratio is fixed. The closed form is where is the number of doublings and . Each symbol: is the keys moved on the -th rehash; the sum runs over every rehash; is the grand total.

WHY a geometric series is the right tool. We chose to grow by a constant multiplier (). Multiplying by a fixed ratio each time is the exact definition of a geometric progression — so its sum is exactly the geometric closed form . No other summation formula applies to "double each time"; this is the tool for "repeated multiplication."

PICTURE. The staircase from Step 6 slotted inside a single box of height : the whole series just fits, with the top-left corner () missing.

Figure — Load factor — when to resize, rehashing cost

Step 8 — The degenerate case: additive growth is a catastrophe

WHAT. Suppose instead of doubling we grow by a fixed amount (say ) each overflow. Then we rehash every inserts, moving keys. Summing:

WHY show this. A derivation isn't finished until we show what happens when the key choice (the ) is broken. Here the terms grow linearly (), not geometrically — so their sum is quadratic (), and dividing by gives per insert. That contrast is the entire reason doubling wins.

PICTURE. Two staircases side by side: doubling (bars race up, sum stays under a lid) versus additive (bars climb one-by-one, their total balloons past the frame).

Figure — Load factor — when to resize, rehashing cost

The one-picture summary

Everything above in a single frame: rare-but-tall rehash spikes, whose entire stacked area fits under a lid of height , so the average height per insert is a flat constant line.

Figure — Load factor — when to resize, rehashing cost
Recall Feynman: tell the whole walkthrough to a friend

Picture a shelf of boxes (that's the table). is just "how full" — filled boxes over total boxes. We promise never to let it get too crowded, because crowded means slow lookups. So when it fills past our line, we build a shelf twice as big and carry every item over — a big chore that costs one unit per item. That chore looks terrifying: some single insert suddenly does units of work. But here's the trick — add up all the chores over the whole life of the table: . Because each chore is double the last, the biggest one alone outweighs all the smaller ones combined, so the grand total is under — no matter how big gets. Spread that across inserts and each insert pays about extra units: a constant. Now try growing by a fixed instead: the chores become , climbing one step at a time, and their total balloons to — a disaster. The single magic ingredient is the word "double": constant multiplication is what makes the sum geometric, and geometric is what makes the average flat.


Flashcards

Why does one rehash cost ?
All keys must be recomputed as and re-placed, since the new size changes every box index.
In the doubling sum , why is the total under ?
Each term doubles, so the last term exceeds the sum of all previous terms; the whole series equals .
Amortized cost per insert with doubling?
— total work divided by inserts.
Why is additive growth () amortized ?
Move-work terms grow linearly, so their sum is ; divided by that's .
What single property makes doubling's sum geometric?
A constant multiplicative ratio (each new size is a fixed factor times the old), which is the definition of a geometric series.