3.3.1 · D2Hashing

Visual walkthrough — Hash function — properties - deterministic, uniform, fast

1,789 words8 min readBack to topic

This is the picture-by-picture derivation behind the parent topic. If a word here is unfamiliar, it gets a figure before it gets a formula.


Step 1 — What a hash function physically does

WHAT. Imagine a long shelf of numbered boxes: box , box , up to box . A key is any thing you want to store — a word, a number, a name. A hash function is just a rule that reads the key and points at exactly one box.

WHY. Before we can talk about "even spreading", we need a picture of the machine doing the placing. Everything later is about how full each box gets.

PICTURE. In the figure below, three keys ("cat", "dog", "fox") enter the machine and each comes out pointing at one box. The machine is a black box for now — we only care that each key gets one box.

Figure — Hash function — properties -  deterministic, uniform, fast

Step 2 — "Uniform" means every box is equally likely

WHAT. We now name the good behaviour. A hash is uniform when a key is just as likely to land in box as box as any other box. No box is a favourite.

WHY. The whole speed argument collapses if some box is a magnet that swallows most keys. Uniformity is the assumption that makes "the average box" actually representative.

PICTURE. The bar chart below compares a bad hash (one tall bar — a magnet box) against a uniform hash (all bars roughly level). We will only be able to prove fast lookups for the level picture.

Figure — Hash function — properties -  deterministic, uniform, fast

Step 3 — Focus on ONE box and ask "how many keys landed here?"

WHAT. Pick a single box, say box . We want to count how many of our keys fell into it.

WHY. A lookup that lands in box must walk through everything sitting in box . So the cost of a lookup is decided by how crowded a single box is — not by the whole table. That is why we zoom in on one box.

PICTURE. Below, box is highlighted. Each of the incoming keys either falls in box (arrow into the box) or misses it (arrow elsewhere). We are about to count only the "in" arrows.

Figure — Hash function — properties -  deterministic, uniform, fast

Step 4 — Turn the counting into a switch (the indicator)

WHAT. For each key (numbered to ) we invent a tiny switch :

WHY. Counting "how many landed in box " is awkward. But if each key carries a switch that is when it's in the box and otherwise, then adding up all the switches gives the count automatically. This trick — turning a yes/no question into a number — is why the algebra becomes easy.

PICTURE. Each incoming key gets a little tag: green tag (it's in box ), grey tag (it missed). The total inside box is just the sum of the green tags.

Figure — Hash function — properties -  deterministic, uniform, fast

So each switch has average value:

  • = the average value of key 's switch.
  • = comes straight from Step 2's uniformity — that's why we needed uniform.

Step 5 — Add the switches: linearity of expectation

WHAT. The number of keys in box is the sum of all switches, and its average is:

WHY. There is a beautiful rule called linearity of expectation: the average of a sum is the sum of the averages — always, even when the switches influence each other. That is what lets us slide the inside the sum. We add a total of times, giving .

PICTURE. The figure stacks copies of the value into one bar of height — the average crowd size of a box.

Figure — Hash function — properties -  deterministic, uniform, fast

Step 6 — From crowd size to lookup cost

WHAT. A lookup does two things: (1) run once to find the box, then (2) walk through the keys inside that box comparing them. Step 5 says the box holds about keys on average. So:

WHY each piece.

  • The = computing — a single fast calculation (this is the fast property from the parent).
  • The = walking the average chain of keys (this is the separate-chaining list inside the box).

PICTURE. A lookup arrow jumps instantly to box (cost ), then hops down a short chain of links (cost ).

Figure — Hash function — properties -  deterministic, uniform, fast

Step 7 — The edge cases (where the story could break)

WHAT & WHY. A derivation you can trust must survive its extremes. Below every limiting case is checked against the same formula .

PICTURE. Three shelves side by side: empty, balanced, and the disaster case.

Figure — Hash function — properties -  deterministic, uniform, fast

The one-picture summary

Figure — Hash function — properties -  deterministic, uniform, fast

This single figure compresses the whole walkthrough: keys enter a uniform machine, spread across boxes so each box averages keys, making a lookup jump () + short walk () .

Recall Feynman retelling — explain it to a friend

Picture a wall of mailboxes. You throw letters at it with a rule that scatters them evenly — no box is a magnet. If you have as many letters as boxes, each box ends up with about one letter. So when you go to fetch a letter, you jump straight to its box (instant) and flick through the tiny pile there (also instant). That's why hashing feels like magic: even spreading keeps every pile short. But two things can ruin it — cramming way too many letters in (piles grow, so you make more boxes and re-throw), or a lazy rule that dumps everything in box 7 (one giant pile, back to slow searching). The maths is just: average pile size , and fetch time jump pile .


Connections