Seyed Masoud Hosseini · Overview · Study log · Weekly summaries · Ideas · Search · Transcript · RSS feed

Design & Analysis of Algorithms · Lecture 11 of 34 · 1:21:51

Lecture 8: Randomization: Universal & Perfect Hashing

8. Randomization: Universal & Perfect Hashing on YouTube

Study guide

What this lecture covers

This lecture answers a question left open by the introductory treatment of hash tables: how do you get constant-time dictionary operations without assuming the input keys are random? It follows the randomized-algorithms unit on skip lists, and moves from a predecessor/successor structure to the dictionary problem, where you can only ask whether an exact key is present. The lecture builds up hashing with chaining from an analysis you may already know, then replaces its unrealistic assumption (simple uniform hashing) with a randomized guarantee that holds for any input.

After watching, you should be able to explain why choosing a random hash function, rather than assuming random keys, removes the need for average-case analysis, prove that a universal hash family gives constant expected-time operations, construct a concrete universal hash family using modular dot products, and describe how perfect hashing achieves zero collisions and constant worst-case search time for static key sets.

Key ideas

  • Dictionary problem: maintain a set of items by key, supporting insert, delete and exact search (no neighbor information), unlike the predecessor/successor search solved by AVL trees or skip lists.
  • Simple uniform hashing: the textbook assumption that any two distinct keys collide with probability 1/m; it is an assumption about the keys being random, which the lecture treats as cheating because real inputs can be adversarial.
  • Universal hash family: a set of hash functions H such that for any two distinct keys, the probability a randomly chosen h from H sends them to the same slot is at most 1/m; randomness comes from the choice of function, not the keys.
  • Expected chain length theorem: if h is drawn uniformly from a universal family, the expected number of keys sharing a slot with any given key is at most 1 + alpha, where alpha = n/m is the load factor.
  • Indicator random variables and linearity of expectation: the proof technique used throughout, writing a count (like collisions) as a sum of 0/1 variables so expectations can be summed term by term.
  • Dot-product hash family: viewing a key in base m as a vector of digits and computing h_a(k) = (a . k) mod m for a random vector a, using a prime table size so digits have modular inverses; this family is provably universal.
  • Perfect (FKS) hashing: a two-level scheme for static key sets (Fredman, Komlos and Szemeredi, 1984) that gets zero collisions and constant worst-case search time, using linear space, by hashing each first-level slot's colliding keys into a second-level table of size proportional to the square of how many keys land there.
  • Birthday paradox / union bound: sizing a secondary table at l_j^2 makes the probability of any collision among its keys at most one half, so retrying a failed hash choice converges in an expected constant (and with high probability logarithmic) number of tries.

Walkthrough

Reviewing the dictionary problem and hashing with chaining (0:00)

The lecture opens by distinguishing the dictionary abstract data type (insert, delete, exact search) from hash tables, the data structure that implements it. It recaps hashing with chaining: a table of m slots, each a linked list, with a hash function mapping the universe of keys into slots. Under simple uniform hashing, this gives expected 1 + alpha time per operation, where alpha = n/m. The lecture flags that this assumption is really about the keys being random, contrasted with the earlier quicksort analysis, where randomizing the pivot choice avoided any assumption about the input at all. The goal for today is the same move for hashing: keep the analysis worst-case over keys, and push all the randomness into the algorithm.

Universal hashing and its guarantee (15:12)

The lecture introduces universal hash families: a set H of hash functions from which one is drawn uniformly at random, with the property that any two distinct keys collide with probability at most 1/m, no matter what the keys are. It then proves the main theorem: with h drawn from a universal family, the expected chain length for any key is at most 1 + alpha. The proof defines indicator variables for pairwise collisions, applies linearity of expectation, bounds each term by 1/m using universality, and adds back the key colliding with itself to recover the 1 + alpha bound.

Building a universal hash family (27:29)

After noting that "all functions from the universe to slots" is universal but useless (too much time and space to store), the lecture constructs an efficient family. With a prime table size m and universe size a power of m, a key is viewed as a vector of base-m digits. The hash function h_a(k) is the dot product of k with a random vector a, taken modulo m; the family is all such functions over choices of a. Choosing a hash function means choosing one random key-sized vector, which is constant time and space in the word-RAM model. A simpler-to-compute alternative family, h_{a,b}(k) = ((a*k + b) mod p) mod m, is mentioned but left to the textbook.

Proving the dot-product family is universal (42:06)

For two distinct keys, their base-m digit vectors must differ in some digit d. The lecture rewrites the collision probability as a condition on a linear combination of the a_i, isolates the a_d term, and uses the fact that m is prime (so multiplicative inverses exist) to solve for the exact value a_d would need to take for a collision. Since a_d is chosen independently and uniformly at random from m values, the probability of hitting that one value is exactly 1/m, which proves universality.

Perfect hashing: the two-level idea (55:16)

For the static dictionary problem (keys fixed in advance, only search needed), the lecture presents FKS perfect hashing: constant worst-case search time and linear worst-case space. The idea uses two levels of hashing. A first-level universal hash function h1 maps n keys into m = theta(n) slots. Instead of chaining colliding keys in a linked list, each slot j gets its own second-level hash table sized at l_j^2, where l_j is the number of keys landing in that slot. Sizing quadratically invokes the birthday paradox: with l_j items hashed into l_j^2 slots, the chance of zero collisions is at least one half.

Making it work: retries and space control (1:04:34)

The full construction has four steps: build the first-level table, then check that the total space (sum of l_j^2) is linear (retry step one if not), build each second-level table, then check each for zero collisions (retry that table's hash function if not). The lecture proves via a union bound that each second-level retry succeeds with probability at least one half, giving an expected constant number of trials, and that with high probability all l_j are O(log n), keeping each retry cheap. For the space check, it computes that the expected sum of l_j^2 is linear using indicator variables and linearity of expectation, then applies Markov's inequality to show the probability of exceeding a constant multiple of n is at most one half, again giving expected constant retries. The total build time works out to roughly n log^2 n.

Before you watch

  • Know hashing with chaining and the load-factor bound from an introductory algorithms course (referred to here as "6.006").
  • Be comfortable with indicator random variables and linearity of expectation, and with union bound and Markov's inequality.
  • Recall the randomized quicksort analysis from the previous lecture, since it motivates avoiding average-case assumptions here.
  • Basic modular arithmetic (multiplicative inverses modulo a prime) is used directly in the universality proof.

Check your understanding

  1. Why does assuming a random hash function (universal hashing) avoid the need to assume the input keys are random?
  2. Walk through how indicator random variables and linearity of expectation are used to prove the 1 + alpha chain-length bound.
  3. Why must the table size m be prime for the dot-product hash family's universality proof to go through?
  4. Why does sizing each second-level table at l_j^2 rather than theta(l_j) make sense, given the birthday paradox?
  5. What guarantees that the perfect hashing construction terminates quickly, both for the first-level space bound and the second-level collision-free property?

Vocabulary

dictionary problem (noun)
The task of storing items so you can insert, delete, and check for an exact match quickly.
The dictionary problem doesn't need to answer questions about neighboring keys.
hashing with chaining (noun)
A hash table design where multiple items mapped to the same slot are stored in a linked list.
Hashing with chaining handles collisions by keeping a list at each slot.
collision (noun)
A situation where two different items are mapped to the same location.
A collision happens when two keys hash to the same slot.
load factor (noun)
The ratio of stored items to the total number of slots in a hash table.
A higher load factor generally means more collisions.
universal hash family (noun)
A set of hash functions where a randomly chosen one guarantees a low collision probability for any input.
Choosing from a universal hash family avoids depending on random keys.
indicator random variable (noun)
A variable that is 1 if an event happens and 0 otherwise, used to simplify probability calculations.
An indicator random variable marks whether two keys collide.
linearity of expectation (noun)
A rule allowing you to add up expected values even when the events are related.
Linearity of expectation lets you sum probabilities across all key pairs.
modular arithmetic (noun)
Arithmetic where numbers wrap around after reaching a fixed value.
The hash function uses modular arithmetic to stay within the table size.
multiplicative inverse (noun)
A number that, when multiplied by another, gives a result of one under modular arithmetic.
A prime table size guarantees every digit has a multiplicative inverse.
perfect hashing (noun)
A hashing scheme designed so that no two keys ever collide.
Perfect hashing gives constant worst-case search time for a fixed key set.
birthday paradox (noun)
The surprising fact that collisions become likely with fewer items than intuition suggests.
The birthday paradox explains why a small table can still have many collisions.
Markov's inequality (noun)
A rule that bounds the probability a value is much larger than its average.
Markov's inequality bounds the chance the total space grows too large.
static (adjective)
Fixed and not changing after being set up.
Perfect hashing works well for a static set of keys known in advance.
worst-case (adjective)
Describing the slowest possible outcome, even under unlucky input.
Perfect hashing gives constant worst-case search time.
adversarial (adjective)
Chosen on purpose to be as difficult as possible for an algorithm.
Real input keys might be adversarial, not random.
expected value (noun)
The average result you would get if you repeated a random process many times.
The expected chain length is at most 1 plus the load factor.
randomize (verb)
To add a random choice into a process on purpose.
We randomize the choice of hash function instead of assuming random keys.
uniform (adjective)
Spread out evenly, with every option equally likely.
The hash function is drawn uniformly at random from the family.
prime number (noun)
A whole number greater than one with no divisors except one and itself.
The table size must be a prime number for the proof to work.
dot product (noun)
A calculation that multiplies matching entries of two lists of numbers and adds the results.
The hash function is a dot product of the key and a random vector, taken modulo m.
slot (noun)
One storage position in a table.
Each key is hashed to one slot in the table.
constant time (phrase)
Taking the same, fixed amount of time no matter how large the input is.
Choosing a hash function from the family takes constant time.
union bound (noun)
A rule that the chance of any one of several bad events happening is at most the sum of their chances.
A union bound shows the retry succeeds with probability at least one half.
converge (verb)
To gradually settle toward a final, stable result.
The retry process converges after an expected constant number of tries.
retry (verb)
To attempt something again after a failed attempt.
If the second-level table has a collision, we retry with a new hash function.
quadratic (adjective)
Growing in proportion to the square of a quantity.
Each second-level table is sized quadratically in the number of keys it holds.
bound (noun)
A limit on how large or small something can be.
The lecture proves an upper bound on the expected chain length.

From the YouTube description

MIT 6.046J Design and Analysis of Algorithms, Spring 2015
View the complete course: http://ocw.mit.edu/6-046JS15
Instructor: Erik Demaine

In this lecture, Professor Demaine reviews hashing in the context of randomized algorithms.

License: Creative Commons BY-NC-SA
More information at http://ocw.mit.edu/terms
More courses at http://ocw.mit.edu

← 7. Randomization: Skip Lists · Recitation 5: Dynamic Programming →