Start with what goes wrong. Take h(k) = k mod 8, a perfectly reasonable hash function, and hand it to someone who wants the table to perform badly. They insert 8, 16, 24, 32, …. Every key lands in slot 0, every lookup walks the whole chain, and the table's average-case analysis was about a distribution of keys that this input is not from. Hashing with Chaining shows that instance running; this lesson is what to do about it.
Definition · A universal familyLet U be the set of possible keys and m the number of slots. A finite family H of functions U → {0, …, m − 1} is universal if for every pair of distinct keys x ≠ y in U, P(h(x) = h(y)) ≤ 1/m when h is drawn uniformly at random from H. The probability is over the choice of h only; x and y are fixed first, and may be chosen by an adversary who knows H.
The order of quantifiers is the content of the definition. For every pair, the average over functions is small. Not: for every function, most pairs are fine — that would be a much weaker statement and it is what a fixed hash function offers. The adversary is allowed to pick the pair after seeing the family, and is not allowed to see the coin that picks h.
Definition · The multiply-shift familyFix a prime p larger than every key. For a in 1..p − 1 and b in 0..p − 1, let hₐ,ᵦ(k) = ((a·k + b) mod p) mod m. The family is all p(p − 1) of these functions, and drawing h uniformly means drawing a and b uniformly.
Theorem · The multiply-shift family is universalFor distinct keys x and y below p, a uniformly chosen hₐ,ᵦ has P(hₐ,ᵦ(x) = hₐ,ᵦ(y)) ≤ 1/m.
Work modulo p, which is a field because p is prime. Write r = (ax + b) mod p and s = (ay + b) mod p. As (a, b) ranges over the p(p − 1) allowed pairs, (r, s) ranges over all p(p − 1) pairs with r ≠ s, each exactly once: given r ≠ s, solving r − s = a(x − y) for a is possible and unique because x − y is invertible modulo p, and b is then determined.
So the question becomes: for how many pairs r ≠ s is r mod m = s mod m? For each of the p choices of r, the values s congruent to it modulo m and different from it number at most ⌈p/m⌉ − 1 ≤ (p − 1)/m.
That gives at most p(p − 1)/m colliding pairs out of p(p − 1), so the probability is at most 1/m.
□
The lab does not take that proof on trust: it enumerates. At p = 11 and m = 4 the family has 10 × 11 = 110 functions, and for the pair 3 and 7 exactly 20 of them collide. That is 2/11 ≈ 0.1818, comfortably under the bound 1/4. Change the pair and the count changes; over every pair of distinct keys below 11, the worst is still 2/11, so this family does better than universality requires on this instance.
Example · Three instances, counted in fullp = 11, m = 4, pair 3 and 7: 20 collisions in 110 functions, 2/11 = 0.1818, bound 1/4. p = 7, m = 3, pair 1 and 4: 10 in 42, 5/21 = 0.2381, bound 1/3. p = 13, m = 5, pair 2 and 9: 22 in 156, 11/78 = 0.1410, bound 1/5. Every one of those is a complete enumeration of the family, not a sample of it.
What is bought, stated carefully
The guarantee is about a pair fixed before the coin is flipped, and it holds for every such pair. So a table built on a universal family has an expected chain length of at most 1 + (n − 1)/m for every input, including inputs constructed to be bad. The expectation is over the choice of h, and it is the same guarantee that “Randomised Quicksort” gets from a random pivot: the input is arbitrary, the coins are the algorithm's, and the average is over the coins.
What is not bought is any promise about the particular h you drew. Some member of the family is terrible for your keys, and you may have drawn it. The guarantee says that is unlikely, in the precise sense that the average over the family is small; if the table turns out slow you re-draw h and rebuild, and that is a real operation with a real cost. A guarantee in expectation is not a guarantee per instance, which is the same distinction “The Cost Is a Distribution” drew for a comparison count.
The fixed function, and the adversary
Set the comparison the other way. Fix h(k) = k mod 4 and let the adversary choose. They pick 1 and 5, which are congruent modulo 4, and those two keys collide with probability 1 — certainty, not 1/4. The panel prints that row beside the counted rate, and the contrast is the entire argument for the family: 2/11 against 1, on keys the adversary chose in both cases.
There is a cost, and it is worth naming. Every lookup now computes a multiplication, an addition and two remainders instead of one remainder, and the table must store a, b and p. Whether that is worth paying depends on whether anyone can choose your keys, which is a question about the system rather than about the algorithm — and it is the reason a compiler's symbol table and a public web service make different choices here.