# K-independent hashing

A family of hash functions is said to be ${\displaystyle k}$-independent or ${\displaystyle k}$-universal[1] if selecting a hash function at random from the family guarantees that the hash codes of any designated ${\displaystyle k}$ keys are independent random variables (see precise mathematical definitions below). Such families allow good average case performance in randomized algorithms or data structures, even if the input data is chosen by an adversary. The trade-offs between the degree of independence and the efficiency of evaluating the hash function are well studied, and many ${\displaystyle k}$-independent families have been proposed.

## Introduction

The goal of hashing is usually to map keys from some large domain (universe) ${\displaystyle U}$ into a smaller range, such as ${\displaystyle m}$ bins (labelled ${\displaystyle [m]=\{0,\dots ,m-1\}}$). In the analysis of randomized algorithms and data structures, it is often desirable for the hash codes of various keys to "behave randomly". For instance, if the hash code of each key were an independent random choice in ${\displaystyle [m]}$, the number of keys per bin could be analyzed using the Chernoff bound. A deterministic hash function cannot offer any such guarantee in an adversarial setting, as the adversary may choose the keys to be the precisely the preimage of a bin. Furthermore, a deterministic hash function does not allow for rehashing: sometimes the input data turns out to be bad for the hash function (e.g. there are too many collisions), so one would like to change the hash function.

The solution to these problems is to pick a function randomly from a large family of hash functions. The randomness in choosing the hash function can be used to guarantee some desired random behavior of the hash codes of any keys of interest. The first definition along these lines was universal hashing, which guarantees a low collision probability for any two designated keys. The concept of ${\displaystyle k}$-independent hashing, introduced by Wegman and Carter in 1981,[2] strengthens the guarantees of random behavior to families of ${\displaystyle k}$ designated keys, and adds a guarantee on the uniform distribution of hash codes.

### Mathematical Definitions

The strictest definition, introduced by Wegman and Carter[2] under the name "strongly universal${\displaystyle _{k}}$ hash family", is the following. A family of hash functions ${\displaystyle H=\{h:U\to [m]\}}$ is ${\displaystyle k}$-independent if for any ${\displaystyle k}$ distinct keys ${\displaystyle (x_{1},\dots ,x_{k})\in U^{k}}$ and any ${\displaystyle k}$ hash codes (not necessarily distinct) ${\displaystyle (y_{1},\dots ,y_{k})\in [m]^{k}}$, we have:

${\displaystyle \Pr _{h\in H}\left[h(x_{1})=y_{1}\land \cdots \land h(x_{k})=y_{k}\right]=m^{-k}}$

This definition is equivalent to the following two conditions:

1. for any fixed ${\displaystyle x\in U}$, as ${\displaystyle h}$ is drawn randomly from ${\displaystyle H}$, ${\displaystyle h(x)}$ is uniformly distributed in ${\displaystyle [m]}$.
2. for any fixed, distinct keys ${\displaystyle x_{1},\dots ,x_{k}\in U}$, as ${\displaystyle h}$ is drawn randomly from ${\displaystyle H}$, ${\displaystyle h(x_{1}),\dots ,h(x_{k})}$ are independent random variables.

Often it is inconvenient to achieve the perfect joint probability of ${\displaystyle m^{-k}}$ due to rounding issues. Following,[3] one may define a ${\displaystyle (\mu ,k)}$-independent family to satisfy:

${\displaystyle \forall }$ distinct ${\displaystyle (x_{1},\dots ,x_{k})\in U^{k}}$ and ${\displaystyle \forall (y_{1},\dots ,y_{k})\in [m]^{k}}$, ${\displaystyle ~~\Pr _{h\in H}\left[h(x_{1})=y_{1}\land \cdots \land h(x_{k})=y_{k}\right]\leq \mu /m^{k}}$

Observe that, even if ${\displaystyle \mu }$ is close to 1, ${\displaystyle h(x_{i})}$ are no longer independent random variables, which is often a problem in the analysis of randomized algorithms. Therefore, a more common alternative to dealing with rounding issues is to prove that the hash family is close in statistical distance to a ${\displaystyle k}$-independent family, which allows black-box use of the independence properties.