# 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.

## Construction of 2-universal family of hash functions

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".

Suppose that a fixed set ${\displaystyle S=\{x_{1},x_{2},\ldots ,x_{n}\}}$ of ${\displaystyle n}$ distinct items are mapped to random locations ${\displaystyle h(x_{1}),h(x_{2}),\ldots ,h(x_{n})}$ by a pairwise independent hash function ${\displaystyle h}$ from ${\displaystyle [N]}$ to ${\displaystyle [M]}$. The load of a entry ${\displaystyle i\in [M]}$ of the table is the number of items in ${\displaystyle S}$ mapped to ${\displaystyle i}$. We want to bound the maximum load.

For uniform random hash function, this is exactly the maximum load in the balls-into-bins game. And we know that for ${\displaystyle n=M}$, the maximum load is ${\displaystyle O(\ln /\ln \ln n)}$ with high probability. This bound can be proved either by counting or by the Chernoff bound.

For pairwise independent hash functions, neither of previous techniques works any more. Nevertheless, we find that a bound on the maximum load can be directly implied by our analysis of collision number.

Let ${\displaystyle Y}$ be a random variable which denotes the maximum load, i.e. the max number of balls in a bin by seeing elements as balls and has values as bins. Then the collision pairs contributed by this heaviest loaded bin is ${\displaystyle {Y \choose 2}}$, so the total number of collision pairs is at least ${\displaystyle {Y \choose 2}}$.

By our previous analysis, the expected number of collision pairs is ${\displaystyle \mathbf {E} [X]<{\frac {n^{2}}{2M}}}$. Therefore,

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

which implies that

${\displaystyle \Pr \left[Y\geq {\frac {n}{\sqrt {\epsilon M}}}\right]\leq \epsilon .}$

In particular, when ${\displaystyle n=M}$, i.e. when ${\displaystyle n}$ items are mapped to ${\displaystyle n}$ locations by a pairwise independent hash function, the maximum load is at most ${\displaystyle {\sqrt {2n}}}$ with probability at least 1/2. This bound is much weaker than the ${\displaystyle O(\ln n/\ln \ln n)}$ bound for uniform hash functions, but it is extremely general and holds for any 2-universal hash families. In fact, it was show by Alon et al that there exists 2-universal hash families which yields a maximum load that matches the above bound.

• Alon, Dietzfelbinger, Miltersen, Petrank, and Tardos. Linear hash functions. Journal of the ACM (JACM), 1999.