# ${\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{aligned}\Pr \left[\bigwedge _{i\in I}{\mathcal {E}}_{i}\right]&=\prod _{i\in I}\Pr[{\mathcal {E}}_{i}].\end{aligned}}}

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{aligned}\Pr \left[\bigwedge _{i\in I}(X_{i}=x_{i})\right]&=\prod _{i\in I}\Pr[X_{i}=x_{i}].\end{aligned}}}

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|\leq k}$ {\displaystyle {\begin{aligned}\Pr \left[\bigwedge _{i\in I}{\mathcal {E}}_{i}\right]&=\prod _{i\in I}\Pr[{\mathcal {E}}_{i}].\end{aligned}}} 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|\leq k}$ and any values ${\displaystyle x_{i}}$, where ${\displaystyle i\in I}$, {\displaystyle {\begin{aligned}\Pr \left[\bigwedge _{i\in I}(X_{i}=x_{i})\right]&=\prod _{i\in I}\Pr[X_{i}=x_{i}].\end{aligned}}}

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{aligned}\Pr \left[X_{i}=a\wedge X_{j}=b\right]&=\Pr[X_{i}=a]\cdot \Pr[X_{j}=b].\end{aligned}}}

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 .
• 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 >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{aligned}\Pr \left[Y_{j}=b\right]&={\frac {1}{2}}.\end{aligned}}} For any ${\displaystyle Y_{j},Y_{\ell }}$ that ${\displaystyle j\neq \ell }$ and any ${\displaystyle a,b\in \{0,1\}}$, {\displaystyle {\begin{aligned}\Pr \left[Y_{j}=a\wedge Y_{\ell }=b\right]&={\frac {1}{4}}.\end{aligned}}}

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{aligned}Y_{i}=(X_{0}+i\cdot X_{1}){\bmod {p}}&\quad {\mbox{for }}i\in [p].\end{aligned}}}
 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{aligned}\Pr \left[(X_{0}+i\cdot X_{1}){\bmod {p}}=a\right]&={\frac {1}{p}}.\end{aligned}}} Due to the law of total probability, {\displaystyle {\begin{aligned}\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{aligned}}} 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{aligned}\Pr \left[Y_{i}=a\wedge Y_{j}=b\right]&={\frac {1}{p^{2}}}.\end{aligned}}} 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 }$

## Tools for limited independence

Let ${\displaystyle X_{1},X_{2},\ldots ,X_{n}}$ be random variables. The variance of their sum is

{\displaystyle {\begin{aligned}\mathbf {Var} \left[\sum _{i=1}^{n}X_{i}\right]=\sum _{i=1}^{n}\mathbf {Var} [X_{i}]+\sum _{i\neq j}\mathbf {cov} (X_{i},X_{j}).\end{aligned}}}

If ${\displaystyle X_{1},X_{2},\ldots ,X_{n}}$ are pairwise independent, then ${\displaystyle \mathbf {cov} (X_{i},X_{j})=0}$ for any ${\displaystyle i\neq j}$ since the covariance of a pair of independent random variables is 0. This gives us the following theorem of linearity of variance for pairwise independent random variables.

 Theorem For pairwise independent random variables ${\displaystyle X_{1},X_{2},\ldots ,X_{n}}$, {\displaystyle {\begin{aligned}\mathbf {Var} \left[\sum _{i=1}^{n}X_{i}\right]=\sum _{i=1}^{n}\mathbf {Var} [X_{i}].\end{aligned}}}

The theorem relies on that the covariances of pairwise independent random variables are 0, which in turn is actually a consequence of a more general theorem.

 Theorem (${\displaystyle k}$-wise independence fools ${\displaystyle k}$-degree polynomials) Let ${\displaystyle X_{1},X_{2},\ldots ,X_{n}}$ be mutually independent random variables and ${\displaystyle Y_{1},Y_{2},\ldots ,Y_{n}}$ be ${\displaystyle k}$-wise independent random variables, with that the marginal distribution of ${\displaystyle Y_{i}}$ is identical to the marginal distribution of ${\displaystyle X_{i}}$, ${\displaystyle 1\leq i\leq n}$, that is, ${\displaystyle \Pr[X_{i}=z]=\Pr[Y_{i}=z]}$ for any ${\displaystyle z}$, ${\displaystyle 1\leq i\leq n}$. Let ${\displaystyle f:\mathbb {R} ^{n}\rightarrow \mathbb {R} }$ be a multivariate polynomial of degree at most ${\displaystyle k}$. Then {\displaystyle {\begin{aligned}\mathbf {E} \left[f(X_{1},X_{2},\ldots ,X_{n})\right]=\mathbf {E} [f(Y_{1},Y_{2},\ldots ,Y_{n})].\end{aligned}}}

This phenomenon is sometimes called that the ${\displaystyle k}$-degree polynomials are fooled by ${\displaystyle k}$-wise independence. In other words, a ${\displaystyle k}$-degree polynomial behaves the same on the ${\displaystyle k}$-wise independent random variables as on the mutual independent random variables.

This theorem is implied by the following lemma.

 Lemma Let ${\displaystyle X_{1},X_{2},\ldots ,X_{k}}$ be ${\displaystyle k}$ mutually independent random variables. Then {\displaystyle {\begin{aligned}\mathbf {E} \left[\prod _{i=1}^{k}X_{i}\right]=\prod _{i=1}^{k}\mathbf {E} [X_{i}].\end{aligned}}}

The lemma can be proved by directly compute the expectation. We omit the detailed proof.

By the linearity of expectation, the expectation of a polynomial is reduced to the sum of the expectations of terms. For a k-degree polynomial, each term has at most ${\displaystyle k}$ variables. Due to the above lemma, with k-wise independence, the expectation of each term behaves exactly the same as mutual independence.

Since the ${\displaystyle k}$th moment is the expectation of a k-degree polynomial of random variables, the tools based on the ${\displaystyle k}$th moment can be safely used for the k-wise independence. In particular, Chebyshev's inequality for pairwise independent random variables:

 Chebyshev's inequality Let ${\displaystyle X=\sum _{i=1}^{n}X_{i}}$, where ${\displaystyle X_{1},X_{2},\ldots ,X_{n}}$ are pairwise independent Poisson trials. Let ${\displaystyle \mu =\mathbf {E} [X]}$. Then ${\displaystyle \Pr[|X-\mu |\geq t]\leq {\frac {\mathbf {Var} [X]}{t^{2}}}={\frac {\sum _{i=1}^{n}\mathbf {Var} [X_{i}]}{t^{2}}}.}$

## 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}\leq 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 \geq {\frac {|E|}{2}}}$ (why?). This gives us a deterministic polynomial time (actually ${\displaystyle O(n^{2})}$ time) ${\displaystyle 1/2}$-approximation algorithm.

## Application: Two-point sampling

Consider a Monte Carlo randomized algorithm with one-sided error for a decision problem ${\displaystyle f}$. We formulate the algorithm as a deterministic algorithm ${\displaystyle A}$ that takes as input ${\displaystyle x}$ and a uniform random number ${\displaystyle r\in [p]}$ where ${\displaystyle p}$ is a prime, such that for any input ${\displaystyle x}$:

• If ${\displaystyle f(x)=1}$, then ${\displaystyle \Pr[A(x,r)=1]\geq {\frac {1}{2}}}$, where the probability is taken over the random choice of ${\displaystyle r}$.
• If ${\displaystyle f(x)=0}$, then ${\displaystyle A(x,r)=0}$ for any ${\displaystyle r}$.

We call ${\displaystyle r}$ the random source for the algorithm.

For the ${\displaystyle x}$ that ${\displaystyle f(x)=1}$, we call the ${\displaystyle r}$ that makes ${\displaystyle A(x,r)=1}$ a witness for ${\displaystyle x}$. For a positive ${\displaystyle x}$, at least half of ${\displaystyle [p]}$ are witnesses. The random source ${\displaystyle r}$ has polynomial number of bits, which means that ${\displaystyle p}$ is exponentially large, thus it is infeasible to find the witness for an input ${\displaystyle x}$ by exhaustive search. Deterministic overcomes this by having sophisticated deterministic rules for efficiently searching for a witness. Randomization, on the other hard, reduce this to a bit of luck, by randomly choosing an ${\displaystyle r}$ and winning with a probability of 1/2.

We can boost the accuracy (equivalently, reduce the error) of any Monte Carlo randomized algorithm with one-sided error by running the algorithm for a number of times.

Suppose that we sample ${\displaystyle t}$ values ${\displaystyle r_{1},r_{2},\ldots ,r_{t}}$ uniformly and independently from ${\displaystyle [p]}$, and run the following scheme:

 ${\displaystyle B(x,r_{1},r_{2},\ldots ,r_{t}):}$ return ${\displaystyle \bigvee _{i=1}^{t}A(x,r_{i})}$;

That is, return 1 if any instance of ${\displaystyle A(x,r_{i})=1}$. For any ${\displaystyle x}$ that ${\displaystyle f(x)=1}$, due to the independence of ${\displaystyle r_{1},r_{2},\ldots ,r_{t}}$, the probability that ${\displaystyle B(x,r_{1},r_{2},\ldots ,r_{t})}$ returns an incorrect result is at most ${\displaystyle 2^{-t}}$. On the other hand, ${\displaystyle B}$ never makes mistakes for the ${\displaystyle x}$ that ${\displaystyle f(x)=0}$ since ${\displaystyle A}$ has no false positives. Thus, the error of the Monte Carlo algorithm is reduced to ${\displaystyle 2^{-t}}$.

Sampling ${\displaystyle t}$ mutually independent random numbers from ${\displaystyle [p]}$ can be quite expensive since it requires ${\displaystyle \Omega (t\log p)}$ random bits. Suppose that we can only afford ${\displaystyle O(\log p)}$ random bits. In particular, we sample two independent uniform random number ${\displaystyle a}$ and ${\displaystyle b}$ from ${\displaystyle [p]}$. If we use ${\displaystyle a}$ and ${\displaystyle b}$ directly bu running two independent instances ${\displaystyle A(x,a)}$ and ${\displaystyle A(x,b)}$, we only get an error upper bound of 1/4.

The following scheme reduces the error significantly with the same number of random bits:

 Algorithm Choose two independent uniform random number ${\displaystyle a}$ and ${\displaystyle b}$ from ${\displaystyle [p]}$. Construct ${\displaystyle t}$ random number ${\displaystyle r_{1},r_{2},\ldots ,r_{t}}$ by: {\displaystyle {\begin{aligned}\forall 1\leq i\leq t,&\quad {\mbox{let }}r_{i}=(a\cdot i+b){\bmod {p}}.\end{aligned}}} Run ${\displaystyle B(x,r_{1},r_{2},\ldots ,r_{t}):}$.

Due to the discussion in the last section, we know that for ${\displaystyle t\leq p}$, ${\displaystyle r_{1},r_{2},\ldots ,r_{t}}$ are pairwise independent and uniform over ${\displaystyle [p]}$. Let ${\displaystyle X_{i}=A(x,r_{i})}$ and ${\displaystyle X=\sum _{i=1}^{t}X_{i}}$. Due to the uniformity of ${\displaystyle r_{i}}$ and our definition of ${\displaystyle A}$, for any ${\displaystyle x}$ that ${\displaystyle f(x)=1}$, it holds that

${\displaystyle \Pr[X_{i}=1]=\Pr[A(x,r_{i})=1]\geq {\frac {1}{2}}.}$

By the linearity of expectations,

${\displaystyle \mathbf {E} [X]=\sum _{i=1}^{t}\mathbf {E} [X_{i}]=\sum _{i=1}^{t}\Pr[X_{i}=1]\geq {\frac {t}{2}}.}$

Since ${\displaystyle X_{i}}$ is Bernoulli trial with a probability of success at least ${\displaystyle p=1/2}$. We can estimate the variance of each ${\displaystyle X_{i}}$ as follows.

${\displaystyle \mathbf {Var} [X_{i}]=p(1-p)\leq {\frac {1}{4}}.}$

Applying Chebyshev's inequality, we have that for any ${\displaystyle x}$ that ${\displaystyle f(x)=1}$,

{\displaystyle {\begin{aligned}\Pr \left[\bigvee _{i=1}^{t}A(x,r_{i})=0\right]&=\Pr[X=0]\\&\leq \Pr[|X-\mathbf {E} [X]|\geq \mathbf {E} [X]]\\&\leq \Pr \left[|X-\mathbf {E} [X]|\geq {\frac {t}{2}}\right]\\&\leq {\frac {4}{t^{2}}}\sum _{i=1}^{t}\mathbf {Var} [X_{i}]\\&\leq {\frac {1}{t}}.\end{aligned}}}

The error is reduced to ${\displaystyle 1/t}$ with only two random numbers. This scheme works as long as ${\displaystyle t\leq p}$.

# 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>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\geq 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})]\leq {\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})]\leq {\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 (ai+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\geq M}$. For some prime ${\displaystyle p\geq N}$, let

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

and the family

${\displaystyle {\mathcal {H}}=\{h_{a,b}\mid 1\leq a\leq 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\leq a\leq 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 ax_{1}+b\not \equiv ax_{2}+b{\pmod {p}}}$. This is because ${\displaystyle ax_{1}+b\equiv ax_{2}+b{\pmod {p}}}$ would imply that ${\displaystyle a(x_{1}-x_{2})\equiv 0{\pmod {p}}}$, which can never happen since ${\displaystyle 1\leq a\leq p-1}$ and ${\displaystyle x_{1}\neq x_{2}}$ (note that ${\displaystyle x_{1},x_{2}\in [N]}$ for an ${\displaystyle N\leq p}$). Therefore, we can assume that ${\displaystyle (ax_{1}+b){\bmod {p}}=u}$ and ${\displaystyle (ax_{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}ax_{1}+b\equiv u{\pmod {p}}\\ax_{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)\leq p(p-1)/M}$ pairs of ${\displaystyle 1\leq a\leq 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})]\leq {\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})]\leq {\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\leq i,j\leq 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

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})]\leq {\frac {1}{M}}.}$

The expected number of collision pairs is

${\displaystyle \mathbf {E} [X]=\mathbf {E} \left[\sum _{i

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]<{\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]<{\frac {n^{2}}{2M}}}$. Apply the Markov's inequality, for ${\displaystyle 0<\epsilon <1}$, we have

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

When ${\displaystyle n\leq {\sqrt {2\epsilon M}}}$, the number of collision pairs is ${\displaystyle X\geq 1}$ 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\geq M}$, then for any set ${\displaystyle S\subset [N]}$ of ${\displaystyle n}$ items, where ${\displaystyle n\leq {\sqrt {2\epsilon M}}}$, the probability that there exits a collision pair is ${\displaystyle \Pr[{\mbox{collision occurs}}]\leq \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.

## Perfect hashing using quadratic space

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]\geq {\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\leq a\leq 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}^{n}Y_{i}^{2}-\sum _{i=1}^{n}Y_{i}\right)={\frac {1}{2}}\left(\sum _{i=1}^{n}Y_{i}^{2}-n\right).}$

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

${\displaystyle \sum _{i=1}^{n}Y_{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]\leq {\frac {n}{2}}}$. Thus,

${\displaystyle \mathbf {E} \left[\sum _{i=1}^{n}Y_{i}^{2}\right]=\mathbf {E} [2X+n]\leq 2n.}$

Due to Markov's inequality, ${\displaystyle \sum _{i=1}^{n}Y_{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.