3.3.8 · D1Hashing

Foundations — Universal hashing — probabilistic guarantee

2,221 words10 min readBack to topic

This page assumes nothing. Every symbol the parent note throws at you — , , , , , , , , the indicator — is built here from the ground up, in the order that lets each one lean on the one before it. Read top to bottom; by the end the parent note reads like plain English.


0. What is a hash table, in pictures?

Before any symbol, the object.

Figure — Universal hashing — probabilistic guarantee

Look at the figure. On the left is a pile of keys (arbitrary things — words, IDs, numbers). In the middle is the rule (a machine). On the right is the table: a row of numbered boxes. The rule reads a key and points at exactly one box.

Two keys landing in the same box is called a collision (the coral pair in the figure). Collisions are not errors — the box just holds a little list (a chain) — but too many in one box means slow searching. Everything in this topic is about keeping collisions rare. (For how a full box is handled, see Hashing — chaining vs open addressing.)


1. The symbol — the universe of keys

Picture a giant warehouse containing every possible key: all account numbers from 0 to 999999999, or every possible word. The keys you actually insert are a tiny handful pulled from this warehouse.

Why the topic needs it: the guarantee must hold for any keys the enemy might pick, and "any" means "any from ". We name the warehouse so we can say "for every in " precisely.


2. The symbol — the number of slots

Why the topic needs it: the collision probability we chase is — the more boxes, the less crowded, the rarer collisions. is the knob.


3. The symbol and the arrow notation

We write for "the box that rule sends key to". If , keys and collide.

Figure — Universal hashing — probabilistic guarantee

The figure shows two different rules and over the same keys. Notice: under the two coral keys collide; under they land apart. Different rule, different collisions — this is the seed of the whole idea. If we can choose which rule, we can dodge bad collisions.


4. The idea of a family

The symbol means is a member of ("belongs to"). So = " is one of the rules in the family".

Why the topic needs it: the entire trick is drawing a rule at random from . You cannot draw at random from a single rule — you need a bag to draw from. is that bag.


5. Probability — what the brackets mean

Our random experiment is: draw a card from the deck , uniformly at random.

So reads: "pick a random rule from the bag; what fraction of the bag's rules make and collide?" The little "" under just says which thing is being randomised — the rule, not the keys.

Figure — Universal hashing — probabilistic guarantee

The figure is the mental model of this probability: a bag of coloured tokens (each token = one rule). Green tokens = "this rule keeps apart", red tokens = "this rule collides them". The probability of collision is just (red tokens) ÷ (all tokens). Universal will mean: for every pair , the red fraction is at most .


6. The counting tool: , and expectation

Concrete picture: roll a fair die, let be the top face. Sometimes , sometimes ; the average over many rolls is . That is . You never see on one roll — it's the centre of gravity of the outcomes.

Why the topic needs it: we can't promise "you will never have collisions". We promise something weaker but honest — "on average, very few". That average is .


7. The 0/1 gadget: the indicator

The subscript just labels which pair this switch watches. There is one such switch for every pair.


8. The operation (needed for the concrete family)

Picture a clock with marks. Counting steps around it, is where the hand stops. E.g. : go 34 steps around a 17-mark clock — two full loops (34 = 17+17) land you back at . So .

Figure — Universal hashing — probabilistic guarantee

The figure shows the clock idea: numbers wrapping around a circle of marks. Why the topic needs : it is the squeezer that forces any big number into the valid range — the last step of .


9. The load factor

Why the topic needs it: the whole punchline is "expected work ". Keep boxes proportional to keys () and stays a small constant, so every operation is expected . Growing the table to keep small is the job of Load factor and rehashing.


Prerequisite map

Universe U of all keys

Hash function h maps key to box

m boxes numbered 0 to m-1

Family H of many rules

Draw h uniformly at random

Probability Pr over the draw

Indicator Xxy is 0 or 1

Expectation E equals the average

Sum via linearity of expectation

Remainder a mod b

Concrete family h a b

Prime p bigger than max key

Universal property Pr collision at most 1 over m

n stored keys

Load factor alpha equals n over m

Expected cost O of 1 plus alpha


Equipment checklist

Cover the right side and test yourself; each should feel obvious now.

What does stand for?
The universe — the set of all possible keys, not just the ones stored.
What is , and how are the boxes numbered?
The number of slots; numbered to (that's still boxes).
Read the notation aloud.
" takes a key from the universe and returns a box number."
What is a collision?
When two distinct keys satisfy — they land in the same box.
What is and what does mean?
A family (bag) of hash functions; means is one rule drawn from that bag.
"Uniformly at random" means what?
Every rule in the family is equally likely to be drawn.
In , what is random and what is fixed?
The rule is random; the keys are fixed (possibly adversarial).
What is in plain words?
The long-run average value of the random quantity .
For an indicator , why does ?
A switch averages to the probability it is : .
Compute .
— thirty-four is exactly two loops of seventeen.
What does mean?
" divides evenly" (leaves no remainder).
What is the load factor ?
— average keys per box.

Connections