47.05.01 · theoretical-cs / randomized-algorithms

Universal Hashing, Perfect Hashing, and the FKS Construction

shipped3 tiersLean: none

Anchor (Master): CLRS 2022 Introduction to Algorithms 4e (MIT Press) §11.3-11.5; Fredman, Komlos & Szemeredi 1984 (FKS construction with worst-case lookup); Dietzfelbinger et al. 1994 (dynamic perfect hashing)

Intuition Beginner

A hash table maps keys to array positions using a hash function. A good hash function spreads keys evenly so that few keys land in the same slot (a collision). A bad hash function concentrates keys, causing many collisions and slow lookups.

The problem is: how do you guarantee a good hash function? If an adversary knows your hash function, they can choose keys that all collide. Universal hashing solves this by choosing the hash function at random from a carefully designed family. No matter what keys the adversary picks, a randomly chosen function from a universal family has low collision probability.

Think of it like seating guests at a dinner party. If you always seat people by last name, a family reunion would cluster everyone at one table. But if you assign seats by a random formula (say, "birthday mod number of tables"), then no group can guarantee they all sit together. Universal hashing is that random formula, with a mathematical guarantee that collisions are rare.

Perfect hashing goes further: for a fixed, known set of keys, it constructs a hash table with zero collisions and constant-time lookup. The FKS construction (named after Fredman, Komlos, and Szemeredi) achieves this in space for keys.

Visual Beginner

Universal hashing: choosing a random function from a family:

Hash family H = {h_1, h_2, h_3, ...}
Keys: {Alice, Bob, Carol, Dave}

         h_1    h_2    h_3    h_4
Alice:    0      2      0      1
Bob:      1      0      2      3
Carol:    0      2      1      1      ← h_1: Alice & Carol collide
Dave:     2      1      0      0      ← h_3: Carol & Dave don't

Pick at random from . For any two distinct keys, the probability that they collide is at most where is the table size.

Property Universal hash Fixed hash
Collision probability for any pair Depends on input
Adversary resistance No input causes many collisions Adversary can force collisions
Choice Random from family Deterministic
Expected chain length (load factor) Unbounded in worst case

Worked example Beginner

A universal hash family for keys that are integers in (where is a prime larger than any key) and a hash table of size :

where and are chosen uniformly at random.

Let and . Pick . Compute . Compute . No collision.

Pick different parameters: . . . Still no collision.

The collision probability for any pair of distinct keys is at most , regardless of which keys are chosen.

Check your understanding Beginner

Formal definition Intermediate+

Definition (universal hash family). Let be a universe of keys and a positive integer. A family of hash functions from to is universal if for every pair of distinct keys with :

Equivalently, if is chosen uniformly at random from :

The family is strongly universal (or pairwise independent) if for all and all :

Definition (-universal). A family is -universal if for any distinct keys and any values :

Theorem (collision bound for universal hashing). If is chosen from a universal family and keys are stored in a table of size , then for any key , the expected number of other keys colliding with is at most (the load factor ).

Proof. Let be the set of stored keys. For a query key , the number of collisions is . By linearity of expectation: .

Counterexamples to common slips

  • "Universal hashing prevents all collisions." No. It bounds the expected number of collisions; individual pairs can still collide. Perfect hashing (FKS) is needed for zero collisions.

  • "A universal hash family must be large." The family has only functions, which is polynomial in the key range. Universality does not require an exponentially large family.

  • "Strongly universal implies universal." It does: strong universality gives , which satisfies the universal bound with equality.

Key theorem with proof Intermediate+

Theorem (FKS construction). Given a static set of keys, the FKS scheme constructs a hash table using space with worst-case lookup time. The construction succeeds with high probability.

Proof. Top level: choose a universal hash function mapping keys to slots. Let be the number of keys landing in slot . By universality, for each (if the load is balanced). The total space for second-level tables is . By linearity of expectation:

The total collisions are . So .

By Markov's inequality, . So a random gives total second-level space at most with probability at least . Repeat until this holds.

Second level: for each slot with keys, choose a universal hash function mapping to slots. By the birthday paradox, the collision probability for any pair is , so the expected number of collisions is . Markov gives: . Repeat until collision-free.

Lookup: compute , then . Two hash computations, .

Bridge. Universal hashing is the foundational reason hash tables achieve expected performance without assumptions about the input distribution: random choice from a structured family ensures that no adversary can force pathological behaviour. This builds toward the randomised algorithms of 47.05.02 where randomisation provides similar adversarial robustness for sorting and searching, and it appears again in 47.05.03 where Miller-Rabin primality testing uses random witnesses with the same collision-probability argument. The central insight is that the pairwise independence property of universal hash families gives strong concentration bounds on the number of collisions, and putting these together with the two-level FKS construction yields worst-case lookups in space — the static equivalent of an ideal hash table.

Exercises Intermediate+

Lean formalization Intermediate+

Mathlib has basic hash table infrastructure but no universal hash family definition, no collision probability bounds, and no FKS construction. The formal definition of universality as and the construction of concrete universal families (the family over prime fields) are absent. A Codex.Algorithms.Hashing module defining universal hash families with collision probability proofs and the FKS space bound would be the foundational target; this unit ships without it.

Advanced results Master

Theorem 1 (cuckoo hashing). For dynamic sets, cuckoo hashing (Pagh and Rodler, 2004) achieves worst-case lookup using two hash functions and two tables. Each key is stored in either table 1 at position or table 2 at position . Lookup checks both locations. Insertion may require relocating existing keys, and fails (requiring rehash) if a cycle forms. For load factor , the expected insertion time is and the failure probability is exponentially small.

Theorem 2 (power of two choices). If each key is hashed to random locations and placed in the least loaded, the maximum load drops from (for ) to [CLRS §11.3]. This "power of two choices" phenomenon has far-reaching consequences in load balancing and parallel computing.

Theorem 3 (dynamic perfect hashing). Dietzfelbinger et al. (1994) extended FKS to dynamic sets, supporting insertions and deletions while maintaining worst-case lookup and space. The amortized insertion and deletion cost is with high probability. The key idea is to rebuild the second-level tables incrementally when a slot grows beyond its allocated size.

Synthesis. Universal hashing is the foundational reason hash tables achieve robust expected performance: the pairwise independence property bounds collision probability regardless of the input distribution, and the FKS construction extends this to worst-case performance for static sets. The central insight is that random choice from a structured algebraic family (linear functions over finite fields) gives the same probabilistic guarantees as a truly random function, and putting these together with the two-level construction of FKS yields a data structure whose space and time are simultaneously optimal. This builds toward the randomised algorithms of 47.05.02 where random choices from structured families provide adversarial robustness in sorting and searching, and appears again in 47.03.01 where the BPP class captures the same principle at the complexity-theoretic level: random choices from polynomial-size families suffice for efficient computation.

Full proof set Master

Proposition 1 (collision bound). If is chosen from a universal family mapping to and keys are stored, then for any key , the expected number of collisions with is at most .

Proof. .

Proposition 2 (FKS space bound). In the FKS construction with top-level table size , the expected total second-level space is at most .

Proof. .

Proposition 3 (second-level success probability). For a slot with keys and second-level table size , a randomly chosen universal hash function is collision-free with probability at least .

Proof. The expected number of collisions is . By Markov's inequality, , so .

Connections Master

  • 37.01.01 — Probability spaces and expectation provide the mathematical foundation for bounding collision probabilities; linearity of expectation is the main tool.

  • 46.01.02 — Mutual information and entropy quantify the randomness in hash function output; the entropy of the hash function choice determines the collision probability.

  • 47.05.02 — Randomised quicksort and treaps use random choices from structured families for adversarial robustness, paralleling universal hashing.

  • 47.03.01 — BPP captures the complexity-theoretic version of the same principle: random choice from polynomial-size families enables efficient computation with bounded error.

Historical & philosophical context Master

Universal hashing was introduced by Larry Carter and Mark Wegman in their 1979 paper "Universal Classes of Hash Functions" [CLRS §11.3]. Their key insight was that a randomly chosen hash function from a carefully designed algebraic family provides the same probabilistic guarantees as a truly random function, while being efficiently computable. The construction is their most famous example.

The FKS construction was introduced by Michael Fredman, Janos Komlos, and Endre Szemeredi in 1984, proving that static hash tables can achieve worst-case lookup in space — resolving a question that had been open since the invention of hash tables. The result was surprising because it showed that perfect hashing does not require quadratic space.

Cuckoo hashing was introduced by Rasmus Pagh and Flemming Rodler in 2004, extending the FKS ideas to dynamic sets with worst-case lookups. The name comes from the cuckoo bird's habit of pushing eggs out of other birds' nests, mirroring the algorithm's eviction strategy.

Bibliography Master

@book{clrs2022,
  author    = {Cormen, T. H. and Leiserson, C. E. and Rivest, R. L. and Stein, C.},
  title     = {Introduction to Algorithms},
  edition   = {4th},
  publisher = {MIT Press},
  year      = {2022},
}
@article{carter1979universal,
  author  = {Carter, J. Lawrence and Wegman, Mark N.},
  title   = {Universal Classes of Hash Functions},
  journal = {Journal of Computer and System Sciences},
  volume  = {18},
  number  = {2},
  pages   = {143--154},
  year    = {1979},
}
@article{fredman1984storing,
  author  = {Fredman, Michael L. and Koml{\'o}s, J{\'a}nos and Szemer{\'e}di, Endre},
  title   = {Storing a Sparse Table with $O(1)$ Worst Case Access Time},
  journal = {Journal of the ACM},
  volume  = {31},
  number  = {3},
  pages   = {538--544},
  year    = {1984},
}
@article{pagh2004cuckoo,
  author  = {Pagh, Rasmus and Rodler, Flemming Friche},
  title   = {Cuckoo Hashing},
  journal = {Journal of Algorithms},
  volume  = {51},
  number  = {2},
  pages   = {122--144},
  year    = {2004},
}