3.3.8 · D2Hashing

Visual walkthrough — Universal hashing — probabilistic guarantee

2,194 words10 min readBack to topic

We start with zero notation. We will earn each piece.


Step 1 — The table is a row of buckets

Figure — Universal hashing — probabilistic guarantee

Look at the figure. Three keys (, , ) each get an arrow into a box. Two of them landed in the same box — that shared box is a collision. Collisions are the enemy: a box with several keys forces us to walk down a little list (a chain) to find ours.

Small means fast lookup. So the whole page is: find the typical size of .


Step 2 — Randomness lives in , not in the keys

The fix, drawn below: we roll dice to choose from a whole bag of hash functions (a family ), and we roll after the bully has committed to the keys — and secretly.

Figure — Universal hashing — probabilistic guarantee

This is why we can beat any input: the bully cannot aim at randomness he has not seen. See Adversarial inputs and randomized algorithms.


Step 3 — The universal promise, as a coin-flip per pair

Figure — Universal hashing — probabilistic guarantee

Step 4 — Turn "count" into 0/1 switches

Figure — Universal hashing — probabilistic guarantee

The figure shows a row of light bulbs, one per key. Lit bulbs (value ) are the collisions; counting lit bulbs = the value of . Nothing clever yet — we just renamed the count. The payoff comes next.


Step 5 — Each switch's average is its collision probability

Figure — Universal hashing — probabilistic guarantee

Step 6 — Add the averages (linearity of expectation)

Figure — Universal hashing — probabilistic guarantee

Step 7 — Name the load, read off

Combine with Step 6:

The is itself (we always touch our own box). See Load factor and rehashing.


Step 8 — The degenerate cases (never leave the reader stranded)

Figure — Universal hashing — probabilistic guarantee

The one-picture summary

Figure — Universal hashing — probabilistic guarantee

One flow, left to right: random → per-pair collision → switch with average add of them via linearity → → cost .

Recall Feynman: the whole walk in plain words

We have a wall of numbered boxes and we want to drop our key into one and later find it fast. Trouble: a bully picks keys on purpose to jam one box. Our escape is to roll dice and pick our sorting-rule from a bag — and we roll secretly, so the bully can't aim. The bag is "fair" (universal) if any two keys the bully names only crash into the same box about one time in . Now, to count how many keys crash into our box, we put a light bulb on every other key: lit if it crashes into us, dark otherwise. The number of lit bulbs is our slowdown. The average brightness of one bulb is just its crash chance, . And here's the magic: the average of the whole wall of bulbs is just the sum of each bulb's average — this is true even though all the bulbs are wired to the same dice. Add up bulbs at each and you get at most . Keep the wall about as wide as the pile of keys and that number is a constant — so every lookup is fast, no matter what the bully does.


Connections

Concept Map

secret

bag is universal

is average of

add n-1 of them

gives

plus one for self

keep m like n

Roll dice pick h from bag

Adversary cannot aim

Pair collides at most 1 over m

Switch Xxy average at most 1 over m

Linearity of expectation

E of X at most n-1 over m

Search cost O of 1 plus alpha

Constant time every input