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

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