3.3.8Hashing

Universal hashing — probabilistic guarantee

1,826 words8 min readdifficulty · medium1 backlinks

What "universal" actually means


Deriving the performance guarantee from scratch

Suppose we insert nn keys into a chained hash table of mm slots, using a randomly chosen hh from a universal family. We want the expected number of keys colliding with a given key xx.

Figure — Universal hashing — probabilistic guarantee

A concrete universal family (so it's not abstract)


Worked examples


Recall Feynman: explain to a 12-year-old

Imagine a bully who always picks the locker that's already full so you can't fit your stuff. If your locker-picking rule is fixed, he always wins. So instead, before class you secretly roll dice to decide the rule. Now the bully doesn't know which lockers things go to, so on average everyone gets their own locker and nobody waits long. The dice (random choice of hh) are your secret, and that's what beats him — not that the kids are "random".


Flashcards

When is a family H\mathcal{H} called universal?
For every distinct xyx \neq y, PrhH[h(x)=h(y)]1/m\Pr_{h\in\mathcal{H}}[h(x)=h(y)] \le 1/m, with hh chosen uniformly at random.
Where does the randomness live in universal hashing?
In the choice of the hash function from the family — NOT in the keys, which may be adversarial.
Why use linearity of expectation in the analysis?
It lets us sum E[Xxy]\mathbb{E}[X_{xy}] without assuming independence, and collisions are dependent.
Expected number of keys colliding with xx (x not inserted)?
n/m=α\le n/m = \alpha (the load factor).
Expected cost of a search with universal hashing?
O(1+α)O(1 + \alpha); with m=Θ(n)m=\Theta(n) it is O(1)O(1).
State the classic universal family ha,bh_{a,b}.
ha,b(k)=((ak+b)modp)modmh_{a,b}(k)=((ak+b)\bmod p)\bmod m, with prime p>p> max key, a{1..p1}a\in\{1..p-1\}, b{0..p1}b\in\{0..p-1\}.
Is ha,bh_{a,b} exactly universal?
Only when mpm \mid p (Pr1/m\Pr \le 1/m). In general it is almost-universal with Pr1/m+1/p\Pr \le 1/m + 1/p.
Why must a0a \neq 0 in ha,bh_{a,b}?
So the map kak+bmodpk\mapsto ak+b \bmod p is a bijection, ensuring distinct keys give distinct residues mod pp.
What does universal hashing protect against that fixed hashing cannot?
An adversary who chooses worst-case keys to force all collisions.
E[Xxy]\mathbb{E}[X_{xy}] equals what?
Pr[h(x)=h(y)]\Pr[h(x)=h(y)], since XxyX_{xy} is a 0/1 indicator.

Connections

Concept Map

defeated by adversary

degrades to

avoids

must satisfy

for distinct x y

equals expectation

summed via

gives

with load factor

chain length

keep m equals theta n

Fixed hash function

Adversary forces collisions

O of n operations

Pick h randomly from family H

Universal property

Pr collision at most 1 over m

Indicator E of Xxy

Linearity of expectation

E of X at most n-1 over m

alpha equals n over m

Expected 1 plus alpha

Expected O of 1 per operation

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Dekho, idea simple hai: agar tum ek fixed hash function use karoge, to koi smart banda (adversary) tumhare function ko dekh kar aise keys de dega jo sab ek hi slot me ja gire — phir har operation O(n)O(n) ho jayega, table list jaisa ban jayega. Solution: function ko pehle se fix mat karo. Ek poora family H\mathcal{H} banao aur runtime pe usme se ek hh randomly uthao. Ab randomness tumhare paas hai, adversary ke paas nahi.

"Universal" ka matlab: kisi bhi do alag keys xyx \neq y ke liye, random hh choose karne par collision ki probability 1/m\le 1/m hoti hai. Yaad rakho — randomness keys me nahi, function ke selection me hai. Keys chahe adversary de, fark nahi padta.

Guarantee derive karna easy hai: har dusri key ke liye ek indicator XxyX_{xy} banao (collision hua to 1, warna 0). E[Xxy]=Pr[collision]1/m\mathbb{E}[X_{xy}] = \Pr[\text{collision}] \le 1/m. Phir linearity of expectation lagao (independence ki zaroorat hi nahi) — sabko jodo, mil jata hai expected collisions n/m=α\le n/m = \alpha. To search ka cost O(1+α)O(1+\alpha), aur mnm \approx n rakho to O(1)O(1). Har input ke liye, guaranteed average — yahi power hai.

Concrete family: ha,b(k)=((ak+b)modp)modmh_{a,b}(k) = ((ak+b)\bmod p)\bmod m, jisme pp ek prime max key se bada, aur a,ba,b random. Ek choti baat: yeh family exactly universal (1/m\le 1/m) tab hoti hai jab mm, pp ko divide karta ho. Agar mpm \nmid p (jo aam baat hai), to thoda sa loose ho jata hai — bound 1/m+1/p\le 1/m + 1/p banta hai, par pp bahut bada hota hai isliye 1/p1/p ignorable hai. Exam me yeh caveat likh doge to full marks.

Go deeper — visual, from zero

Test yourself — Hashing

Connections