Intuition The core idea (WHY this exists)
A fixed hash function can always be defeated. Once an adversary knows your function h h h , they hand you keys that all collide — every operation degrades to O ( n ) O(n) O ( n ) . The fix: don't commit to one function . Pick h h h randomly from a carefully designed family H \mathcal{H} H at runtime. Then for any fixed pair of keys, the probability they collide is tiny — and the adversary can't exploit randomness they can't see.
WHAT we buy: expected O ( 1 ) O(1) O ( 1 ) operations for every input , not just lucky ones.
Definition Universal family
A family H \mathcal{H} H of hash functions mapping a universe U U U into { 0 , 1 , … , m − 1 } \{0,1,\dots,m-1\} { 0 , 1 , … , m − 1 } is universal if for every pair of distinct keys x ≠ y x \neq y x = y :
Pr h ∈ H [ h ( x ) = h ( y ) ] ≤ 1 m \Pr_{h \in \mathcal{H}}\big[\, h(x) = h(y) \,\big] \le \frac{1}{m} Pr h ∈ H [ h ( x ) = h ( y ) ] ≤ m 1
where h h h is chosen uniformly at random from H \mathcal{H} H .
Common mistake Steel-man: "Random hashing means each key lands uniformly."
It feels right — we picture keys scattering evenly. But the universal property is not about one key's distribution; it's about pairs . We never assume the data is random. The randomness lives entirely in the choice of h h h , while the keys x , y x,y x , y are arbitrary (even adversarial). Fix: read the definition as "for fixed x , y x,y x , y , random h h h ".
Suppose we insert n n n keys into a chained hash table of m m m slots, using a randomly chosen h h h from a universal family. We want the expected number of keys colliding with a given key x x x .
α = n m \alpha = \dfrac{n}{m} α = m n . Then expected chain length seen by x x x is at most 1 + α 1 + \alpha 1 + α (the + 1 +1 + 1 for x x x itself if it's present; n − 1 m \frac{n-1}{m} m n − 1 if x x x not yet inserted).
Intuition Therefore (HOW it gives O(1))
Expected search time = O ( 1 + α ) = O(1 + \alpha) = O ( 1 + α ) . If we keep m = Θ ( n ) m = \Theta(n) m = Θ ( n ) , then α = O ( 1 ) \alpha = O(1) α = O ( 1 ) , so every operation is expected O ( 1 ) O(1) O ( 1 ) — regardless of the input keys . The guarantee is over our coin flips, not over "nice" data.
Definition The number-theoretic family
H p , m \mathcal{H}_{p,m} H p , m
Pick a prime p > p > p > (max key). For a ∈ { 1 , … , p − 1 } a \in \{1,\dots,p-1\} a ∈ { 1 , … , p − 1 } and b ∈ { 0 , … , p − 1 } b \in \{0,\dots,p-1\} b ∈ { 0 , … , p − 1 } :
h a , b ( k ) = ( ( a k + b ) m o d p ) m o d m h_{a,b}(k) = \big((a\,k + b) \bmod p\big) \bmod m h a , b ( k ) = ( ( a k + b ) mod p ) mod m
The family H p , m = { h a , b } \mathcal{H}_{p,m} = \{\, h_{a,b} \,\} H p , m = { h a , b } is universal. We pick a , b a,b a , b randomly.
Common mistake Steel-man: "The final
m o d m \bmod m mod m keeps collision probability exactly ≤ 1 / m \le 1/m ≤ 1/ m ."
It feels exact — we mapped p p p residues down to m m m buckets, so "1 / m 1/m 1/ m " looks automatic. But m m m usually does not divide p p p , so the buckets are slightly uneven: some receive ⌈ p / m ⌉ \lceil p/m\rceil ⌈ p / m ⌉ residues, others ⌊ p / m ⌋ \lfloor p/m\rfloor ⌊ p / m ⌋ . The honest bound is
Pr [ h ( x ) = h ( y ) ] ≤ ⌈ p / m ⌉ − 1 p − 1 ≤ 1 m . \Pr[h(x)=h(y)] \le \frac{\lceil p/m\rceil - 1}{p-1} \le \frac{1}{m}. Pr [ h ( x ) = h ( y )] ≤ p − 1 ⌈ p / m ⌉ − 1 ≤ m 1 .
A looser but common form is ≤ ⌈ p / m ⌉ p ≤ 1 m + 1 p \le \frac{\lceil p/m\rceil}{p} \le \frac{1}{m}+\frac{1}{p} ≤ p ⌈ p / m ⌉ ≤ m 1 + p 1 . So this family is truly universal (≤ 1 / m \le 1/m ≤ 1/ m ) when m ∣ p m \mid p m ∣ p , and almost-universal (≤ 1 / m + 1 / p \le 1/m + 1/p ≤ 1/ m + 1/ p ) in general — the extra 1 / p 1/p 1/ p is negligible since p p p is huge. Fix: quote ≤ 1 / m \le 1/m ≤ 1/ m only with the "m ∣ p m \mid p m ∣ p " caveat, otherwise carry the + 1 / p +1/p + 1/ p .
Worked example Why is it (almost) universal? (sketch of the WHY)
Fix x ≠ y x\neq y x = y . Let r = ( a x + b ) m o d p r = (ax+b)\bmod p r = ( a x + b ) mod p , s = ( a y + b ) m o d p s=(ay+b)\bmod p s = ( a y + b ) mod p . Since a ≠ 0 a\neq 0 a = 0 and p p p prime, the map ( a , b ) ↦ ( r , s ) (a,b)\mapsto(r,s) ( a , b ) ↦ ( r , s ) is a bijection onto pairs with r ≠ s r \neq s r = s (distinct keys give distinct residues mod p p p ). A collision occurs only when r ≡ s ( m o d m ) r \equiv s \pmod m r ≡ s ( mod m ) . For each fixed r r r , the number of s ≠ r s\neq r s = r with s ≡ r ( m o d m ) s\equiv r \pmod m s ≡ r ( mod m ) is at most ⌈ p / m ⌉ − 1 \lceil p/m\rceil - 1 ⌈ p / m ⌉ − 1 out of p − 1 p-1 p − 1 choices. Hence
Pr [ collision ] ≤ ⌈ p / m ⌉ − 1 p − 1 ≤ 1 m . \Pr[\text{collision}] \le \frac{\lceil p/m\rceil - 1}{p-1} \le \frac{1}{m}. Pr [ collision ] ≤ p − 1 ⌈ p / m ⌉ − 1 ≤ m 1 . ∎
Worked example Example 1 — Expected probe count
Table with m = 100 m = 100 m = 100 slots, n = 50 n = 50 n = 50 keys already in, universal h h h . Expected number of keys in the slot of a new key x x x ?
Why: x x x not yet inserted, so use n m \frac{n}{m} m n .
E [ collisions ] ≤ 50 100 = 0.5 \mathbb{E}[\text{collisions}] \le \frac{50}{100} = 0.5 E [ collisions ] ≤ 100 50 = 0.5
Expected search cost = 1 + 0.5 = 1.5 = 1 + 0.5 = 1.5 = 1 + 0.5 = 1.5 comparisons. Constant.
Worked example Example 2 — Computing
h a , b h_{a,b} h a , b
p = 17 p = 17 p = 17 , m = 6 m = 6 m = 6 , choose a = 3 , b = 4 a=3, b=4 a = 3 , b = 4 . Hash k = 10 k=10 k = 10 :
a k + b = 3 ⋅ 10 + 4 = 34 ak+b = 3\cdot 10 + 4 = 34 ak + b = 3 ⋅ 10 + 4 = 34 . Why: apply a k + b ak+b ak + b .
34 m o d 17 = 0 34 \bmod 17 = 0 34 mod 17 = 0 . Why: mod p p p first (this is the universality engine).
0 m o d 6 = 0 0 \bmod 6 = 0 0 mod 6 = 0 . Why: squeeze into table range [ 0 , m ) [0,m) [ 0 , m ) .
So h ( 10 ) = 0 h(10) = 0 h ( 10 ) = 0 .
Worked example Example 3 — Probability sanity check
Here p = 17 p=17 p = 17 , m = 6 m=6 m = 6 , and 6 ∤ 17 6 \nmid 17 6 ∤ 17 , so we expect at most ⌈ 17 / 6 ⌉ 17 = 3 17 ≈ 0.176 \frac{\lceil 17/6\rceil}{17} = \frac{3}{17}\approx 0.176 17 ⌈ 17/6 ⌉ = 17 3 ≈ 0.176 (the almost-universal bound), which is just above 1 / m = 1 / 6 ≈ 0.167 1/m = 1/6 \approx 0.167 1/ m = 1/6 ≈ 0.167 . Pick keys 10 10 10 and 15 15 15 ; over all valid ( a , b ) (a,b) ( a , b ) the empirical collision fraction is ≤ 3 / 17 \le 3/17 ≤ 3/17 . (The VERIFY block checks this numerically.)
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 h h h ) are your secret, and that's what beats him — not that the kids are "random".
"RANDOM the FUNCTION, never the DATA."
And the bound: "Pair collides at most 1-in-m." (Pr ≤ 1 / m \Pr \le 1/m Pr ≤ 1/ m — exact when m ∣ p m \mid p m ∣ p , else + 1 / p +1/p + 1/ p .)
When is a family H \mathcal{H} H called universal? For every distinct
x ≠ y x \neq y x = y ,
Pr h ∈ H [ h ( x ) = h ( y ) ] ≤ 1 / m \Pr_{h\in\mathcal{H}}[h(x)=h(y)] \le 1/m Pr h ∈ H [ h ( x ) = h ( y )] ≤ 1/ m , with
h h h 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 [ X x y ] \mathbb{E}[X_{xy}] E [ X x y ] without assuming independence, and collisions are dependent.
Expected number of keys colliding with x x x (x not inserted)? ≤ n / m = α \le n/m = \alpha ≤ n / m = α (the load factor).
Expected cost of a search with universal hashing? O ( 1 + α ) O(1 + \alpha) O ( 1 + α ) ; with
m = Θ ( n ) m=\Theta(n) m = Θ ( n ) it is
O ( 1 ) O(1) O ( 1 ) .
State the classic universal family h a , b h_{a,b} h a , b . h a , b ( k ) = ( ( a k + b ) m o d p ) m o d m h_{a,b}(k)=((ak+b)\bmod p)\bmod m h a , b ( k ) = (( ak + b ) mod p ) mod m , with prime
p > p> p > max key,
a ∈ { 1.. p − 1 } a\in\{1..p-1\} a ∈ { 1.. p − 1 } ,
b ∈ { 0.. p − 1 } b\in\{0..p-1\} b ∈ { 0.. p − 1 } .
Is h a , b h_{a,b} h a , b exactly universal? Only when
m ∣ p m \mid p m ∣ p (
Pr ≤ 1 / m \Pr \le 1/m Pr ≤ 1/ m ). In general it is
almost -universal with
Pr ≤ 1 / m + 1 / p \Pr \le 1/m + 1/p Pr ≤ 1/ m + 1/ p .
Why must a ≠ 0 a \neq 0 a = 0 in h a , b h_{a,b} h a , b ? So the map
k ↦ a k + b m o d p k\mapsto ak+b \bmod p k ↦ ak + b mod p is a bijection, ensuring distinct keys give distinct residues mod
p p p .
What does universal hashing protect against that fixed hashing cannot? An adversary who chooses worst-case keys to force all collisions.
E [ X x y ] \mathbb{E}[X_{xy}] E [ X x y ] equals what?Pr [ h ( x ) = h ( y ) ] \Pr[h(x)=h(y)] Pr [ h ( x ) = h ( y )] , since
X x y X_{xy} X x y is a 0/1 indicator.
Adversary forces collisions
Pick h randomly from family H
Pr collision at most 1 over m
E of X at most n-1 over m
Expected O of 1 per operation
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) O ( n ) ho jayega, table list jaisa ban jayega. Solution: function ko pehle se fix mat karo . Ek poora family H \mathcal{H} H banao aur runtime pe usme se ek h h h randomly uthao. Ab randomness tumhare paas hai, adversary ke paas nahi.
"Universal" ka matlab: kisi bhi do alag keys x ≠ y x \neq y x = y ke liye, random h h h choose karne par collision ki probability ≤ 1 / m \le 1/m ≤ 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 X x y X_{xy} X x y banao (collision hua to 1, warna 0). E [ X x y ] = Pr [ collision ] ≤ 1 / m \mathbb{E}[X_{xy}] = \Pr[\text{collision}] \le 1/m E [ X x y ] = Pr [ collision ] ≤ 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 ≤ n / m = α . To search ka cost O ( 1 + α ) O(1+\alpha) O ( 1 + α ) , aur m ≈ n m \approx n m ≈ n rakho to O ( 1 ) O(1) O ( 1 ) . Har input ke liye, guaranteed average — yahi power hai.
Concrete family: h a , b ( k ) = ( ( a k + b ) m o d p ) m o d m h_{a,b}(k) = ((ak+b)\bmod p)\bmod m h a , b ( k ) = (( ak + b ) mod p ) mod m , jisme p p p ek prime max key se bada, aur a , b a,b a , b random. Ek choti baat: yeh family exactly universal (≤ 1 / m \le 1/m ≤ 1/ m ) tab hoti hai jab m m m , p p p ko divide karta ho. Agar m ∤ p m \nmid p m ∤ p (jo aam baat hai), to thoda sa loose ho jata hai — bound ≤ 1 / m + 1 / p \le 1/m + 1/p ≤ 1/ m + 1/ p banta hai, par p p p bahut bada hota hai isliye 1 / p 1/p 1/ p ignorable hai. Exam me yeh caveat likh doge to full marks.