$\displaystyle{ k }$-wise independence

Recall the definition of independence between events:

 Definition (Independent events) Events $\displaystyle{ \mathcal{E}_1, \mathcal{E}_2, \ldots, \mathcal{E}_n }$ are mutually independent if, for any subset $\displaystyle{ I\subseteq\{1,2,\ldots,n\} }$, \displaystyle{ \begin{align} \Pr\left[\bigwedge_{i\in I}\mathcal{E}_i\right] &= \prod_{i\in I}\Pr[\mathcal{E}_i]. \end{align} }

Similarly, we can define independence between random variables:

 Definition (Independent variables) Random variables $\displaystyle{ X_1, X_2, \ldots, X_n }$ are mutually independent if, for any subset $\displaystyle{ I\subseteq\{1,2,\ldots,n\} }$ and any values $\displaystyle{ x_i }$, where $\displaystyle{ i\in I }$, \displaystyle{ \begin{align} \Pr\left[\bigwedge_{i\in I}(X_i=x_i)\right] &= \prod_{i\in I}\Pr[X_i=x_i]. \end{align} }

Mutual independence is an ideal condition of independence. The limited notion of independence is usually defined by the k-wise independence.

 Definition (k-wise Independenc) 1. Events $\displaystyle{ \mathcal{E}_1, \mathcal{E}_2, \ldots, \mathcal{E}_n }$ are k-wise independent if, for any subset $\displaystyle{ I\subseteq\{1,2,\ldots,n\} }$ with $\displaystyle{ |I|\le k }$ \displaystyle{ \begin{align} \Pr\left[\bigwedge_{i\in I}\mathcal{E}_i\right] &= \prod_{i\in I}\Pr[\mathcal{E}_i]. \end{align} } 2. Random variables $\displaystyle{ X_1, X_2, \ldots, X_n }$ are k-wise independent if, for any subset $\displaystyle{ I\subseteq\{1,2,\ldots,n\} }$ with $\displaystyle{ |I|\le k }$ and any values $\displaystyle{ x_i }$, where $\displaystyle{ i\in I }$, \displaystyle{ \begin{align} \Pr\left[\bigwedge_{i\in I}(X_i=x_i)\right] &= \prod_{i\in I}\Pr[X_i=x_i]. \end{align} }

A very common case is pairwise independence, i.e. the 2-wise independence.

 Definition (pairwise Independent random variables) Random variables $\displaystyle{ X_1, X_2, \ldots, X_n }$ are pairwise independent if, for any $\displaystyle{ X_i,X_j }$ where $\displaystyle{ i\neq j }$ and any values $\displaystyle{ a,b }$ \displaystyle{ \begin{align} \Pr\left[X_i=a\wedge X_j=b\right] &= \Pr[X_i=a]\cdot\Pr[X_j=b]. \end{align} }

Note that the definition of k-wise independence is hereditary:

• If $\displaystyle{ X_1, X_2, \ldots, X_n }$ are k-wise independent, then they are also $\displaystyle{ \ell }$-wise independent for any $\displaystyle{ \ell\lt k }$.
• If $\displaystyle{ X_1, X_2, \ldots, X_n }$ are NOT k-wise independent, then they cannot be $\displaystyle{ \ell }$-wise independent for any $\displaystyle{ \ell\gt k }$.

Pairwise Independent Bits

Suppose we have $\displaystyle{ m }$ mutually independent and uniform random bits $\displaystyle{ X_1,\ldots, X_m }$. We are going to extract $\displaystyle{ n=2^m-1 }$ pairwise independent bits from these $\displaystyle{ m }$ mutually independent bits.

Enumerate all the nonempty subsets of $\displaystyle{ \{1,2,\ldots,m\} }$ in some order. Let $\displaystyle{ S_j }$ be the $\displaystyle{ j }$th subset. Let

$\displaystyle{ Y_j=\bigoplus_{i\in S_j} X_i, }$

where $\displaystyle{ \oplus }$ is the exclusive-or, whose truth table is as follows.

 $\displaystyle{ a }$ $\displaystyle{ b }$ $\displaystyle{ a }$$\displaystyle{ \oplus }$$\displaystyle{ b }$ 0 0 0 0 1 1 1 0 1 1 1 0

There are $\displaystyle{ n=2^m-1 }$ such $\displaystyle{ Y_j }$, because there are $\displaystyle{ 2^m-1 }$ nonempty subsets of $\displaystyle{ \{1,2,\ldots,m\} }$. An equivalent definition of $\displaystyle{ Y_j }$ is

$\displaystyle{ Y_j=\left(\sum_{i\in S_j}X_i\right)\bmod 2 }$.

Sometimes, $\displaystyle{ Y_j }$ is called the parity of the bits in $\displaystyle{ S_j }$.

We claim that $\displaystyle{ Y_j }$ are pairwise independent and uniform.

 Theorem For any $\displaystyle{ Y_j }$ and any $\displaystyle{ b\in\{0,1\} }$, \displaystyle{ \begin{align} \Pr\left[Y_j=b\right] &= \frac{1}{2}. \end{align} } For any $\displaystyle{ Y_j,Y_\ell }$ that $\displaystyle{ j\neq\ell }$ and any $\displaystyle{ a,b\in\{0,1\} }$, \displaystyle{ \begin{align} \Pr\left[Y_j=a\wedge Y_\ell=b\right] &= \frac{1}{4}. \end{align} }

The proof is left for your exercise.

Therefore, we extract exponentially many pairwise independent uniform random bits from a sequence of mutually independent uniform random bits.

Note that $\displaystyle{ Y_j }$ are not 3-wise independent. For example, consider the subsets $\displaystyle{ S_1=\{1\},S_2=\{2\},S_3=\{1,2\} }$ and the corresponding random bits $\displaystyle{ Y_1,Y_2,Y_3 }$. Any two of $\displaystyle{ Y_1,Y_2,Y_3 }$ would decide the value of the third one.

Pairwise Independent Variables

We now consider constructing pairwise independent random variables ranging over $\displaystyle{ [p]=\{0,1,2,\ldots,p-1\} }$ for some prime $\displaystyle{ p }$. Unlike the above construction, now we only need two independent random sources $\displaystyle{ X_0,X_1 }$, which are uniformly and independently distributed over $\displaystyle{ [p] }$.

Let $\displaystyle{ Y_0,Y_1,\ldots, Y_{p-1} }$ be defined as:

\displaystyle{ \begin{align} Y_i=(X_0+i\cdot X_1)\bmod p &\quad \mbox{for }i\in[p]. \end{align} }
 Theorem The random variables $\displaystyle{ Y_0,Y_1,\ldots, Y_{p-1} }$ are pairwise independent uniform random variables over $\displaystyle{ [p] }$.
Proof.
 We first show that $\displaystyle{ Y_i }$ are uniform. That is, we will show that for any $\displaystyle{ i,a\in[p] }$, \displaystyle{ \begin{align} \Pr\left[(X_0+i\cdot X_1)\bmod p=a\right] &= \frac{1}{p}. \end{align} } Due to the law of total probability, \displaystyle{ \begin{align} \Pr\left[(X_0+i\cdot X_1)\bmod p=a\right] &= \sum_{j\in[p]}\Pr[X_1=j]\cdot\Pr\left[(X_0+ij)\bmod p=a\right]\\ &=\frac{1}{p}\sum_{j\in[p]}\Pr\left[X_0\equiv(a-ij)\pmod{p}\right]. \end{align} } For prime $\displaystyle{ p }$, for any $\displaystyle{ i,j,a\in[p] }$, there is exact one value in $\displaystyle{ [p] }$ of $\displaystyle{ X_0 }$ satisfying $\displaystyle{ X_0\equiv(a-ij)\pmod{p} }$. Thus, $\displaystyle{ \Pr\left[X_0\equiv(a-ij)\pmod{p}\right]=1/p }$ and the above probability is $\displaystyle{ \frac{1}{p} }$. We then show that $\displaystyle{ Y_i }$ are pairwise independent, i.e. we will show that for any $\displaystyle{ Y_i,Y_j }$ that $\displaystyle{ i\neq j }$ and any $\displaystyle{ a,b\in[p] }$, \displaystyle{ \begin{align} \Pr\left[Y_i=a\wedge Y_j=b\right] &= \frac{1}{p^2}. \end{align} } The event $\displaystyle{ Y_i=a\wedge Y_j=b }$ is equivalent to that $\displaystyle{ \begin{cases} (X_0+iX_1)\equiv a\pmod{p}\\ (X_0+jX_1)\equiv b\pmod{p} \end{cases} }$ Due to the Chinese remainder theorem, there exists a unique solution of $\displaystyle{ X_0 }$ and $\displaystyle{ X_1 }$ in $\displaystyle{ [p] }$ to the above linear congruential system. Thus the probability of the event is $\displaystyle{ \frac{1}{p^2} }$.
$\displaystyle{ \square }$

Application: Derandomizing MAX-CUT

Let $\displaystyle{ G(V,E) }$ be an undirected graph, and $\displaystyle{ S\subset V }$ be a vertex set. The cut defined by $\displaystyle{ S }$ is $\displaystyle{ C(S,\bar{S})=|\{uv\in E\mid u\in S, v\not\in S\}| }$.

Given as input an undirected graph $\displaystyle{ G(V,E) }$, find the $\displaystyle{ S\subset V }$ whose cut value $\displaystyle{ C(S,\bar{S}) }$ is maximized. This problem is called the maximum cut (MAX-CUT) problem, which is NP-hard. The decision version of one of the weighted version of the problem is one of the Karp's 21 NP-complete problems. The problem has a $\displaystyle{ 0.878 }$-approximation algorithm by rounding a semidefinite programming. Assuming that the unique game conjecture (UGC), there does not exist a poly-time algorithm with better approximation ratio unless $\displaystyle{ P=NP }$.

Here we give a very simple $\displaystyle{ 0.5 }$-approximation algorithm. The "algorithm" has a one-line description:

• Put each $\displaystyle{ v\in V }$ into $\displaystyle{ S }$ independently with probability 1/2.

We then analyze the approximation ratio of this algorithm.

For each $\displaystyle{ v\in V }$, let $\displaystyle{ Y_v }$ indicate whether $\displaystyle{ v\in S }$, that is

$\displaystyle{ Y_v=\begin{cases}1& v\in S,\\ 0& v\not\in S.\end{cases} }$

For each edge $\displaystyle{ uv\in E }$, let $\displaystyle{ Y_{uv} }$ indicate whether $\displaystyle{ uv }$ contribute to the cut $\displaystyle{ C(S,\bar{S}) }$, i.e. whether $\displaystyle{ u\in S, v\not\in S }$ or $\displaystyle{ u\not\in S, v\in S }$, that is

$\displaystyle{ Y_{uv}=\begin{cases}1&Y_u\neq Y_v,\\0&\text{otherwise}.\end{cases} }$

Then $\displaystyle{ C(S,\bar{S})=\sum_{uv\in E}Y_{uv} }$. Due to the linearity of expectation,

$\displaystyle{ \mathbf{E}\left[C(S,\bar{S})\right]=\sum_{uv\in E}\mathbf{E}[Y_{uv}]=\sum_{uv\in E}\Pr[Y_u\neq Y_v]=\frac{|E|}{2} }$.

The maximum cut of a graph is at most $\displaystyle{ |E| }$. Thus, the algorithm returns in expectation a cut with size at least half of the maximum cut.

We then show how to dereandomize this algorithm by pairwise independent bits.

Suppose that $\displaystyle{ |V|=n }$ and enumerate the $\displaystyle{ n }$ vertices by $\displaystyle{ v_1,v_2,\ldots, v_n }$ in an arbitrary order. Let $\displaystyle{ m=\lceil\log_2 (n+1)\rceil }$. Sample $\displaystyle{ m }$ bits $\displaystyle{ X_1,\ldots, X_m\in\{0,1\} }$ uniformly and independently at random. Enumerate all nonempty subsets of $\displaystyle{ \{1,2,\ldots,m\} }$ by $\displaystyle{ S_1,S_2,\ldots,S_{2^m-1} }$. For each vertex $\displaystyle{ v_j }$, let $\displaystyle{ Y_{v_j}=\bigoplus_{i\in S_j}X_i }$. The MAX-CUT algorithm uses these bits to construct the solution $\displaystyle{ S }$:

• For $\displaystyle{ j=1,2,\ldots,n }$, put $\displaystyle{ v_j }$ into $\displaystyle{ S }$ if $\displaystyle{ Y_{v_j}=1 }$.

We have shown that $\displaystyle{ Y_{v_j} }$, $\displaystyle{ j=1,2,\ldots,n }$, are uniform and pairwise independent. Thus we still have that $\displaystyle{ \Pr[Y_{u}\neq Y_{v}]=\frac{1}{2} }$. The above analysis still holds, so that the algorithm returns in expectation a cut with size at least $\displaystyle{ \frac{|E|}{2} }$.

Finally, we notice that there are only $\displaystyle{ m=\lceil\log_2 (n+1)\rceil }$ total random bits in the new algorithm. We can enumerate all $\displaystyle{ 2^m\le 2(n+1) }$ possible strings of $\displaystyle{ m }$ bits, run the above algorithm with the bit strings as the "random sources", and output the maximum cut returned. There must exist a bit string $\displaystyle{ X_1,\ldots, X_m\in\{0,1\} }$ on which the algorithm returns a cut of size $\displaystyle{ \ge \frac{|E|}{2} }$ (why?). This gives us a deterministic polynomial time (actually $\displaystyle{ O(n^2) }$ time) $\displaystyle{ 1/2 }$-approximation algorithm.

Universal Hashing

Hashing is one of the oldest tools in Computer Science. Knuth's memorandum in 1963 on analysis of hash tables is now considered to be the birth of the area of analysis of algorithms.

• Knuth. Notes on "open" addressing, July 22 1963. Unpublished memorandum.

The idea of hashing is simple: an unknown set $\displaystyle{ S }$ of $\displaystyle{ n }$ data items (or keys) are drawn from a large universe $\displaystyle{ U=[N] }$ where $\displaystyle{ N\gg n }$; in order to store $\displaystyle{ S }$ in a table of $\displaystyle{ M }$ entries (slots), we assume a consistent mapping (called a hash function) from the universe $\displaystyle{ U }$ to a small range $\displaystyle{ [M] }$.

This idea seems clever: we use a consistent mapping to deal with an arbitrary unknown data set. However, there is a fundamental flaw for hashing.

• For sufficiently large universe ($\displaystyle{ N\gt M(n-1) }$), for any function, there exists a bad data set $\displaystyle{ S }$, such that all items in $\displaystyle{ S }$ are mapped to the same entry in the table.

A simple use of pigeonhole principle can prove the above statement.

To overcome this situation, randomization is introduced into hashing. We assume that the hash function is a random mapping from $\displaystyle{ [N] }$ to $\displaystyle{ [M] }$. In order to ease the analysis, the following ideal assumption is used:

Simple Uniform Hash Assumption (SUHA or UHA, a.k.a. the random oracle model):

A uniform random function $\displaystyle{ h:[N]\rightarrow[M] }$ is available and the computation of $\displaystyle{ h }$ is efficient.

Families of universal hash functions

The assumption of completely random function simplifies the analysis. However, in practice, truly uniform random hash function is extremely expensive to compute and store. Thus, this simple assumption can hardly represent the reality.

There are two approaches for implementing practical hash functions. One is to use ad hoc implementations and wish they may work. The other approach is to construct class of hash functions which are efficient to compute and store but with weaker randomness guarantees, and then analyze the applications of hash functions based on this weaker assumption of randomness.

This route was took by Carter and Wegman in 1977 while they introduced universal families of hash functions.

 Definition (universal hash families) Let $\displaystyle{ [N] }$ be a universe with $\displaystyle{ N\ge M }$. A family of hash functions $\displaystyle{ \mathcal{H} }$ from $\displaystyle{ [N] }$ to $\displaystyle{ [M] }$ is said to be $\displaystyle{ k }$-universal if, for any items $\displaystyle{ x_1,x_2,\ldots,x_k\in [N] }$ and for a hash function $\displaystyle{ h }$ chosen uniformly at random from $\displaystyle{ \mathcal{H} }$, we have $\displaystyle{ \Pr[h(x_1)=h(x_2)=\cdots=h(x_k)]\le\frac{1}{M^{k-1}}. }$ A family of hash functions $\displaystyle{ \mathcal{H} }$ from $\displaystyle{ [N] }$ to $\displaystyle{ [M] }$ is said to be strongly $\displaystyle{ k }$-universal if, for any items $\displaystyle{ x_1,x_2,\ldots,x_k\in [N] }$, any values $\displaystyle{ y_1,y_2,\ldots,y_k\in[M] }$, and for a hash function $\displaystyle{ h }$ chosen uniformly at random from $\displaystyle{ \mathcal{H} }$, we have $\displaystyle{ \Pr[h(x_1)=y_1\wedge h(x_2)=y_2 \wedge \cdots \wedge h(x_k)=y_k]=\frac{1}{M^{k}}. }$

In particular, for a 2-universal family $\displaystyle{ \mathcal{H} }$, for any elements $\displaystyle{ x_1,x_2\in[N] }$, a uniform random $\displaystyle{ h\in\mathcal{H} }$ has

$\displaystyle{ \Pr[h(x_1)=h(x_2)]\le\frac{1}{M}. }$

For a strongly 2-universal family $\displaystyle{ \mathcal{H} }$, for any elements $\displaystyle{ x_1,x_2\in[N] }$ and any values $\displaystyle{ y_1,y_2\in[M] }$, a uniform random $\displaystyle{ h\in\mathcal{H} }$ has

$\displaystyle{ \Pr[h(x_1)=y_1\wedge h(x_2)=y_2]=\frac{1}{M^2}. }$

This behavior is exactly the same as uniform random hash functions on any pair of inputs. For this reason, a strongly 2-universal hash family are also called pairwise independent hash functions.

2-universal hash families

The construction of pairwise independent random variables via modulo a prime introduced in Section 1 already provides a way of constructing a strongly 2-universal hash family.

Let $\displaystyle{ p }$ be a prime. The function $\displaystyle{ h_{a,b}:[p]\rightarrow [p] }$ is defined by

$\displaystyle{ h_{a,b}(x)=(ax+b)\bmod p, }$

and the family is

$\displaystyle{ \mathcal{H}=\{h_{a,b}\mid a,b\in[p]\}. }$
 Lemma $\displaystyle{ \mathcal{H} }$ is strongly 2-universal.
Proof.
 In Section 1, we have proved the pairwise independence of the sequence of $\displaystyle{ (a i+b)\bmod p }$, for $\displaystyle{ i=0,1,\ldots, p-1 }$, which directly implies that $\displaystyle{ \mathcal{H} }$ is strongly 2-universal.
$\displaystyle{ \square }$
The original construction of Carter-Wegman

What if we want to have hash functions from $\displaystyle{ [N] }$ to $\displaystyle{ [M] }$ for non-prime $\displaystyle{ N }$ and $\displaystyle{ M }$? Carter and Wegman developed the following method.

Suppose that the universe is $\displaystyle{ [N] }$, and the functions map $\displaystyle{ [N] }$ to $\displaystyle{ [M] }$, where $\displaystyle{ N\ge M }$. For some prime $\displaystyle{ p\ge N }$, let

$\displaystyle{ h_{a,b}(x)=((ax+b)\bmod p)\bmod M, }$

and the family

$\displaystyle{ \mathcal{H}=\{h_{a,b}\mid 1\le a\le p-1, b\in[p]\}. }$

Note that unlike the first construction, now $\displaystyle{ a\neq 0 }$.

 Lemma (Carter-Wegman) $\displaystyle{ \mathcal{H} }$ is 2-universal.
Proof.
 Due to the definition of $\displaystyle{ \mathcal{H} }$, there are $\displaystyle{ p(p-1) }$ many different hash functions in $\displaystyle{ \mathcal{H} }$, because each hash function in $\displaystyle{ \mathcal{H} }$ corresponds to a pair of $\displaystyle{ 1\le a\le p-1 }$ and $\displaystyle{ b\in[p] }$. We only need to count for any particular pair of $\displaystyle{ x_1,x_2\in[N] }$ that $\displaystyle{ x_1\neq x_2 }$, the number of hash functions that $\displaystyle{ h(x_1)=h(x_2) }$. We first note that for any $\displaystyle{ x_1\neq x_2 }$, $\displaystyle{ a x_1+b\not\equiv a x_2+b \pmod p }$. This is because $\displaystyle{ a x_1+b\equiv a x_2+b \pmod p }$ would imply that $\displaystyle{ a(x_1-x_2)\equiv 0\pmod p }$, which can never happen since $\displaystyle{ 1\le a\le p-1 }$ and $\displaystyle{ x_1\neq x_2 }$ (note that $\displaystyle{ x_1,x_2\in[N] }$ for an $\displaystyle{ N\le p }$). Therefore, we can assume that $\displaystyle{ (a x_1+b)\bmod p=u }$ and $\displaystyle{ (a x_2+b)\bmod p=v }$ for $\displaystyle{ u\neq v }$. Due to the Chinese remainder theorem, for any $\displaystyle{ x_1,x_2\in[N] }$ that $\displaystyle{ x_1\neq x_2 }$, for any $\displaystyle{ u,v\in[p] }$ that $\displaystyle{ u\neq v }$, there is exact one solution to $\displaystyle{ (a,b) }$ satisfying: $\displaystyle{ \begin{cases} a x_1+b \equiv u \pmod p\\ a x_2+b \equiv v \pmod p. \end{cases} }$ After modulo $\displaystyle{ M }$, every $\displaystyle{ u\in[p] }$ has at most $\displaystyle{ \lceil p/M\rceil -1 }$ many $\displaystyle{ v\in[p] }$ that $\displaystyle{ v\neq u }$ but $\displaystyle{ v\equiv u\pmod M }$. Therefore, for every pair of $\displaystyle{ x_1,x_2\in[N] }$ that $\displaystyle{ x_1\neq x_2 }$, there exist at most $\displaystyle{ p(\lceil p/M\rceil -1)\le p(p-1)/M }$ pairs of $\displaystyle{ 1\le a\le p-1 }$ and $\displaystyle{ b\in[p] }$ such that $\displaystyle{ ((ax_1+b)\bmod p)\bmod M=((ax_2+b)\bmod p)\bmod M }$, which means there are at most $\displaystyle{ p(p-1)/M }$ many hash functions $\displaystyle{ h\in\mathcal{H} }$ having $\displaystyle{ h(x_1)=h(x_2) }$ for $\displaystyle{ x_1\neq x_2 }$. For $\displaystyle{ h }$ uniformly chosen from $\displaystyle{ \mathcal{H} }$, for any $\displaystyle{ x_1\neq x_2 }$, $\displaystyle{ \Pr[h(x_1)=h(x_2)]\le \frac{p(p-1)/M}{p(p-1)}=\frac{1}{M}. }$ We prove that $\displaystyle{ \mathcal{H} }$ is 2-universal.
$\displaystyle{ \square }$
A construction used in practice

The main issue of Carter-Wegman construction is the efficiency. The mod operation is very slow, and has been so for more than 30 years.

The following construction is due to Dietzfelbinger et al. It was published in 1997 and has been practically used in various applications of universal hashing.

The family of hash functions is from $\displaystyle{ [2^u] }$ to $\displaystyle{ [2^v] }$. With a binary representation, the functions map binary strings of length $\displaystyle{ u }$ to binary strings of length $\displaystyle{ v }$. Let

$\displaystyle{ h_{a}(x)=\left\lfloor\frac{a\cdot x\bmod 2^u}{2^{u-v}}\right\rfloor, }$

and the family

$\displaystyle{ \mathcal{H}=\{h_{a}\mid a\in[2^v]\mbox{ and }a\mbox{ is odd}\}. }$

This family of hash functions does not exactly meet the requirement of 2-universal family. However, Dietzfelbinger et al proved that $\displaystyle{ \mathcal{H} }$ is close to a 2-universal family. Specifically, for any input values $\displaystyle{ x_1,x_2\in[2^u] }$, for a uniformly random $\displaystyle{ h\in\mathcal{H} }$,

$\displaystyle{ \Pr[h(x_1)=h(x_2)]\le\frac{1}{2^{v-1}}. }$

So $\displaystyle{ \mathcal{H} }$ is within an approximation ratio of 2 to being 2-universal. The proof uses the fact that odd numbers are relative prime to a power of 2.

The function is extremely simple to compute in c language. We exploit that C-multiplication (*) of unsigned u-bit numbers is done $\displaystyle{ \bmod 2^u }$, and have a one-line C-code for computing the hash function:

h_a(x) = (a*x)>>(u-v)


The bit-wise shifting is a lot faster than modular. It explains the popularity of this scheme in practice than the original Carter-Wegman construction.

Collision number

Consider a 2-universal family $\displaystyle{ \mathcal{H} }$ of hash functions from $\displaystyle{ [N] }$ to $\displaystyle{ [M] }$. Let $\displaystyle{ h }$ be a hash function chosen uniformly from $\displaystyle{ \mathcal{H} }$. For a fixed set $\displaystyle{ S }$ of $\displaystyle{ n }$ distinct elements from $\displaystyle{ [N] }$, say $\displaystyle{ S=\{x_1,x_2,\ldots,x_n\} }$, the elements are mapped to the hash values $\displaystyle{ h(x_1), h(x_2), \ldots, h(x_n) }$. This can be seen as throwing $\displaystyle{ n }$ balls to $\displaystyle{ M }$ bins, with pairwise independent choices of bins.

As in the balls-into-bins with full independence, we are curious about the questions such as the birthday problem or the maximum load. These questions are interesting not only because they are natural to ask in a balls-into-bins setting, but in the context of hashing, they are closely related to the performance of hash functions.

The old techniques for analyzing balls-into-bins rely too much on the independence of the choice of the bin for each ball, therefore can hardly be extended to the setting of 2-universal hash families. However, it turns out several balls-into-bins questions can somehow be answered by analyzing a very natural quantity: the number of collision pairs.

A collision pair for hashing is a pair of elements $\displaystyle{ x_1,x_2\in S }$ which are mapped to the same hash value, i.e. $\displaystyle{ h(x_1)=h(x_2) }$. Formally, for a fixed set of elements $\displaystyle{ S=\{x_1,x_2,\ldots,x_n\} }$, for any $\displaystyle{ 1\le i,j\le n }$, let the random variable

$\displaystyle{ X_{ij} = \begin{cases} 1 & \text{if }h(x_i)=h(x_j),\\ 0 & \text{otherwise.} \end{cases} }$

The total number of collision pairs among the $\displaystyle{ n }$ items $\displaystyle{ x_1,x_2,\ldots,x_n }$ is

$\displaystyle{ X=\sum_{i\lt j} X_{ij}.\, }$

Since $\displaystyle{ \mathcal{H} }$ is 2-universal, for any $\displaystyle{ i\neq j }$,

$\displaystyle{ \Pr[X_{ij}=1]=\Pr[h(x_i)=h(x_j)]\le\frac{1}{M}. }$

The expected number of collision pairs is

$\displaystyle{ \mathbf{E}[X]=\mathbf{E}\left[\sum_{i\lt j}X_{ij}\right]=\sum_{i\lt j}\mathbf{E}[X_{ij}]=\sum_{i\lt j}\Pr[X_{ij}=1]\le{n\choose 2}\frac{1}{M}\lt \frac{n^2}{2M}. }$

In particular, for $\displaystyle{ n=M }$, i.e. $\displaystyle{ n }$ items are mapped to $\displaystyle{ n }$ hash values by a pairwise independent hash function, the expected collision number is $\displaystyle{ \mathbf{E}[X]\lt \frac{n^2}{2M}=\frac{n}{2} }$.

Birthday problem

In the context of hash functions, the birthday problem ask for the probability that there is no collision at all. Since collision is something that we want to avoid in the applications of hash functions, we would like to lower bound the probability of zero-collision, i.e. to upper bound the probability that there exists a collision pair.

The above analysis gives us an estimation on the expected number of collision pairs, such that $\displaystyle{ \mathbf{E}[X]\lt \frac{n^2}{2M} }$. Apply the Markov's inequality, for $\displaystyle{ 0\lt \epsilon\lt 1 }$, we have

$\displaystyle{ \Pr\left[X\ge \frac{n^2}{2\epsilon M}\right]\le\Pr\left[X\ge \frac{1}{\epsilon}\mathbf{E}[X]\right]\le\epsilon. }$

When $\displaystyle{ n\le\sqrt{2\epsilon M} }$, the number of collision pairs is $\displaystyle{ X\ge1 }$ with probability at most $\displaystyle{ \epsilon }$, therefore with probability at least $\displaystyle{ 1-\epsilon }$, there is no collision at all. Therefore, we have the following theorem.

 Theorem If $\displaystyle{ h }$ is chosen uniformly from a 2-universal family of hash functions mapping the universe $\displaystyle{ [N] }$ to $\displaystyle{ [M] }$ where $\displaystyle{ N\ge M }$, then for any set $\displaystyle{ S\subset [N] }$ of $\displaystyle{ n }$ items, where $\displaystyle{ n\le\sqrt{2\epsilon M} }$, the probability that there exits a collision pair is $\displaystyle{ \Pr[\mbox{collision occurs}]\le\epsilon. }$

Recall that for mutually independent choices of bins, for some $\displaystyle{ n=\sqrt{2M\ln(1/\epsilon)} }$, the probability that a collision occurs is about $\displaystyle{ \epsilon }$. For constant $\displaystyle{ \epsilon }$, this gives an essentially same bound as the pairwise independent setting. Therefore, the behavior of pairwise independent hash function is essentially the same as the uniform random hash function for the birthday problem. This is easy to understand, because birthday problem is about the behavior of collisions, and the definition of 2-universal hash function can be interpreted as "functions that the probability of collision is as low as a uniform random function".

Perfect Hashing

Perfect hashing is a data structure for storing a static dictionary. In a static dictionary, a set $\displaystyle{ S }$ of $\displaystyle{ n }$ items from the universe $\displaystyle{ [N] }$ are preprocessed and stored in a table. Once the table is constructed, it will nit be changed any more, but will only be used for search operations: a search for an item gives the location of the item in the table or returns that the item is not in the table. You may think of an application that we store an encyclopedia in a DVD, so that searches are very efficient but there will be no updates to the data.

This problem can be solved by binary search on a sorted table or balanced search trees in $\displaystyle{ O(\log n) }$ time for a set $\displaystyle{ S }$ of $\displaystyle{ n }$ elements. We show how to solve this problem with $\displaystyle{ O(1) }$ time by perfect hashing.

The idea of perfect hashing is that we use a hash function $\displaystyle{ h }$ to map the $\displaystyle{ n }$ items to distinct entries of the table; store every item $\displaystyle{ x\in S }$ in the entry $\displaystyle{ h(x) }$; and also store the hash function $\displaystyle{ h }$ in a fixed location in the table (usually the beginning of the table). The algorithm for searching for an item is as follows:

search for $\displaystyle{ x }$ in table $\displaystyle{ T }$:
1. retrieve $\displaystyle{ h }$ from a fixed location in the table;
2. if $\displaystyle{ x=T[h(x)] }$ return $\displaystyle{ h(x) }$; else return NOT_FOUND;

This scheme works as long as that the hash function satisfies the following two conditions:

• The description of $\displaystyle{ h }$ is sufficiently short, so that $\displaystyle{ h }$ can be stored in one entry (or in constant many entries) of the table.
• $\displaystyle{ h }$ has no collisions on $\displaystyle{ S }$, i.e. there is no pair of items $\displaystyle{ x_1,x_2\in S }$ that are mapped to the same value by $\displaystyle{ h }$.

The first condition is easy to guarantee for 2-universal hash families. As shown by Carter-Wegman construction, a 2-universal hash function can be uniquely represented by two integers $\displaystyle{ a }$ and $\displaystyle{ b }$, which can be stored in two entries (or just one, if the word length is sufficiently large) of the table.

Our discussion is now focused on the second condition. We find that it relies on the perfectness of the hash function for a data set $\displaystyle{ S }$.

A hash function $\displaystyle{ h }$ is perfect for a set $\displaystyle{ S }$ of items if $\displaystyle{ h }$ maps all items in $\displaystyle{ S }$ to different values, i.e. there is no collision.

We have shown by the birthday problem for 2-universal hashing that when $\displaystyle{ n }$ items are mapped to $\displaystyle{ n^2 }$ values, for an $\displaystyle{ h }$ chosen uniformly from a 2-universal family of hash functions, the probability that a collision occurs is at most 1/2. Thus

$\displaystyle{ \Pr[h\mbox{ is perfect for }S]\ge\frac{1}{2} }$

for a table of $\displaystyle{ n^2 }$ entries.

The construction of perfect hashing is straightforward then:

For a set $\displaystyle{ S }$ of $\displaystyle{ n }$ elements:
1. uniformly choose an $\displaystyle{ h }$ from a 2-universal family $\displaystyle{ \mathcal{H} }$; (for Carter-Wegman's construction, it means uniformly choose two integer $\displaystyle{ 1\le a\le p-1 }$ and $\displaystyle{ b\in[p] }$ for a sufficiently large prime $\displaystyle{ p }$.)
2. check whether $\displaystyle{ h }$ is perfect for $\displaystyle{ S }$;
3. if $\displaystyle{ h }$ is NOT perfect for $\displaystyle{ S }$, start over again; otherwise, construct the table;

This is a Las Vegas randomized algorithm, which construct a perfect hashing for a fixed set $\displaystyle{ S }$ with expectedly at most two trials (due to geometric distribution). The resulting data structure is a $\displaystyle{ O(n^2) }$-size static dictionary of $\displaystyle{ n }$ elements which answers every search in deterministic $\displaystyle{ O(1) }$ time.

FKS perfect hashing

In the last section we see how to use $\displaystyle{ O(n^2) }$ space and constant time for answering search in a set. Now we see how to do it with linear space and constant time. This solves the problem of searching asymptotically optimal for both time and space.

This was once seemingly impossible, until Yao's seminal paper:

• Yao. Should tables be sorted? Journal of the ACM (JACM), 1981.

Yao's paper shows a possibility of achieving linear space and constant time at the same time by exploiting the power of hashing, but assumes an unrealistically large universe.

Inspired by Yao's work, Fredman, Komlós, and Szemerédi discover the first linear-space and constant-time static dictionary in a realistic setting:

• Fredman, Komlós, and Szemerédi. Storing a sparse table with O(1) worst case access time. Journal of the ACM (JACM), 1984.

The idea of FKS hashing is to arrange hash table in two levels:

• In the first level, $\displaystyle{ n }$ items are hashed to $\displaystyle{ n }$ buckets by a 2-universal hash function $\displaystyle{ h }$.
Let $\displaystyle{ B_i }$ be the set of items hashed to the $\displaystyle{ i }$th bucket.
• In the second level, construct a $\displaystyle{ |B_i|^2 }$-size perfect hashing for each bucket $\displaystyle{ B_i }$.

The data structure can be stored in a table. The first few entries are reserved to store the primary hash function $\displaystyle{ h }$. To help the searching algorithm locate a bucket, we use the next $\displaystyle{ n }$ entries of the table as the "pointers" to the bucket: each entry stores the address of the first entry of the space to store a bucket. In the rest of table, the $\displaystyle{ n }$ buckets are stored in order, each using a $\displaystyle{ |B_i|^2 }$ space as required by perfect hashing.

It is easy to see that the search time is constant. To search for an item $\displaystyle{ x }$, the algorithm does the followings:

• Retrieve $\displaystyle{ h }$.
• Retrieve the address for bucket $\displaystyle{ h(x) }$.
• Search by perfect hashing within bucket $\displaystyle{ h(x) }$.

Each line takes constant time. So the worst-case search time is constant.

We then need to guarantee that the space is linear to $\displaystyle{ n }$. At the first glance, this seems impossible because each instance of perfect hashing for a bucket costs a square-size of space. We will prove that although the individual buckets use square-sized spaces, the sum of the them is still linear.

For a fixed set $\displaystyle{ S }$ of $\displaystyle{ n }$ items, for a hash function $\displaystyle{ h }$ chosen uniformly from a 2-universe family which maps the items to $\displaystyle{ [n] }$, called $\displaystyle{ n }$ buckets, let $\displaystyle{ Y_i=|B_i| }$ be the number of items in $\displaystyle{ S }$ mapped to the $\displaystyle{ i }$th bucket. We are going to bound the following quantity:

$\displaystyle{ Y=\sum_{i=1}^n Y_i^2. }$

Since each bucket $\displaystyle{ B_i }$ use a space of $\displaystyle{ Y_i^2 }$ for perfect hashing. $\displaystyle{ Y }$ gives the size of the space for storing the buckets.

We will show that $\displaystyle{ Y }$ is related to the total number of collision pairs. (Indeed, the number of collision pairs can be computed by a degree-2 polynomial, just like $\displaystyle{ Y }$.)

Note that a bucket of $\displaystyle{ Y_i }$ items contributes $\displaystyle{ {Y_i\choose 2} }$ collision pairs. Let $\displaystyle{ X }$ be the total number of collision pairs. $\displaystyle{ X }$ can be computed by summing over the collision pairs in every bucket:

$\displaystyle{ X=\sum_{i=1}^n{Y_i\choose 2}=\sum_{i=1}^n\frac{Y_i(Y_i-1)}{2}=\frac{1}{2}\left(\sum_{i=1}^nY_i^2-\sum_{i=1}^nY_i\right)=\frac{1}{2}\left(\sum_{i=1}^nY_i^2-n\right). }$

Therefore, the sum of squares of the sizes of buckets is related to collision number by:

$\displaystyle{ \sum_{i=1}^nY_i^2=2X+n. }$

By our analysis of the collision number, we know that for $\displaystyle{ n }$ items mapped to $\displaystyle{ n }$ buckets, the expected number of collision pairs is: $\displaystyle{ \mathbf{E}[X]\le \frac{n}{2} }$. Thus,

$\displaystyle{ \mathbf{E}\left[\sum_{i=1}^nY_i^2\right]=\mathbf{E}[2X+n]\le 2n. }$

Due to Markov's inequality, $\displaystyle{ \sum_{i=1}^nY_i^2=O(n) }$ with a constant probability. For any set $\displaystyle{ S }$, we can find a suitable $\displaystyle{ h }$ after expected constant number of trials, and FKS can be constructed with guaranteed (instead of expected) linear-size which answers each search in constant time.