# Distinct Elements

Consider the following problem of counting distinct elements: Suppose that ${\displaystyle \Omega }$ is a sufficiently large universe.

• Input: a sequence of (not necessarily distinct) elements ${\displaystyle x_{1},x_{2},\ldots ,x_{n}\in \Omega }$;
• Output: an estimation of the total number of distinct elements ${\displaystyle z=|\{x_{1},x_{2},\ldots ,x_{n}\}|}$.

A straightforward way of solving this problem is to maintain a dictionary data structure, which costs at least linear (${\displaystyle O(n)}$) space. For big data, where ${\displaystyle n}$ is very large, this is still too expensive. However, due to an information-theoretical argument, linear space is necessary if you want to compute the exact value of ${\displaystyle z}$.

Our goal is to relax the problem a little bit to significantly reduce the space cost by tolerating approximate answers. The form of approximation we consider is ${\displaystyle (\epsilon ,\delta )}$-estimator.

 ${\displaystyle (\epsilon ,\delta )}$-estimator A random variable ${\displaystyle {\widehat {Z}}}$ is an ${\displaystyle (\epsilon ,\delta )}$-estimator of a quantity ${\displaystyle z}$ if ${\displaystyle \Pr[\,(1-\epsilon )z\leq {\widehat {Z}}\leq (1+\epsilon )z\,]\geq 1-\delta }$. ${\displaystyle {\widehat {Z}}}$ is said to be an unbiased estimator of ${\displaystyle z}$ if ${\displaystyle \mathbb {E} [{\widehat {Z}}]=z}$.

Usually ${\displaystyle \epsilon }$ is called approximation error and ${\displaystyle \delta }$ is called confidence error.

We now present an elegant algorithm introduced by Flajolet and Martin in 1984. The algorithm can be implemented in data stream model: The input elements ${\displaystyle x_{1},x_{2},\ldots ,x_{n}}$ is presented to the algorithm one at a time, where the size of data ${\displaystyle n}$ is unknown to the algorithm. The algorithm maintains a value ${\displaystyle {\widehat {Z}}}$ which is an ${\displaystyle (\epsilon ,\delta )}$-estimator of the total number of distinct elements ${\displaystyle z=|\{x_{1},x_{2},\ldots ,x_{n}\}|}$, using only a small amount of memory space to memorize (with loss) the data set ${\displaystyle \{x_{1},x_{2},\ldots ,x_{n}\}}$.

A famous quotation of Flajolet describes the performance of this algorithm as:

"Using only memory equivalent to 5 lines of printed text, you can estimate with a typical accuracy of 5% and in a single pass the total vocabulary of Shakespeare."


## An estimator by hashing

Suppose that we can access to an idealized random hash function ${\displaystyle h:\Omega \to [0,1]}$ which is uniformly distributed over all mappings from the universe ${\displaystyle \Omega }$ to unit interval ${\displaystyle [0,1]}$.

Recall that the input sequence ${\displaystyle x_{1},x_{2},\ldots ,x_{n}\in \Omega }$ consists of ${\displaystyle z=|\{x_{1},x_{2},\ldots ,x_{n}\}|}$ distinct elements. These elements are mapped by the random function ${\displaystyle h}$ to ${\displaystyle z}$ hash values uniformly and independently distributed in ${\displaystyle [0,1]}$. We could maintain these hash values instead of the original elements, but this would still be too expensive because in the worst case we still have up to ${\displaystyle n}$ distinct values to maintain. However, due to the idealized random hash function, the unit interval ${\displaystyle [0,1]}$ will be partitioned into ${\displaystyle z+1}$ subintervals by these ${\displaystyle z}$ uniform and independent hash values. The typical length of the subinterval gives an estimation of the number ${\displaystyle z}$.

 Proposition ${\displaystyle \mathbb {E} \left[\min _{1\leq i\leq n}h(x_{i})\right]={\frac {1}{z+1}}}$.
Proof.
 The input sequence ${\displaystyle x_{1},x_{2},\ldots ,x_{n}\in \Omega }$ consisting of ${\displaystyle z}$ distinct elements are mapped to ${\displaystyle z}$ random hash values uniformly and independently distributed in ${\displaystyle [0,1]}$. These ${\displaystyle z}$ hash values partition the unit interval ${\displaystyle [0,1]}$ into ${\displaystyle z+1}$ subintervals ${\displaystyle [0,v_{1}],[v_{1},v_{2}],[v_{2},v_{3}]\ldots ,[v_{z-1},v_{z}],[v_{z},1]}$, where ${\displaystyle v_{i}}$ denotes the ${\displaystyle i}$-th smallest value among all hash values ${\displaystyle \{h(x_{1}),h(x_{2}),\ldots ,h(x_{n})\}}$. Clearly we have ${\displaystyle v_{1}=\min _{1\leq i\leq n}h(x_{i})}$. Meanwhile, since all hash values are uniformly and independently distributed in ${\displaystyle [0,1]}$, the lengths of all subintervals ${\displaystyle v_{1},v_{2}-v_{1},v_{3}-v_{2},\ldots ,v_{z}-v_{z-1},1-v_{z}}$ are identically distributed. By symmetry, they have the same expectation, therefore ${\displaystyle (z+1)\mathbb {E} [v_{1}]=\mathbb {E} [v_{1}]+\sum _{i=1}^{z-1}\mathbb {E} [v_{i+1}-v_{i}]+\mathbb {E} [1-v_{z}]=\mathbb {E} \left[v_{1}+(v_{2}-v_{1})+(v_{3}-v_{2})+\cdots +(v_{z}-v_{z-1})+1-v_{z}\right]=1,}$ which implies that ${\displaystyle \mathbb {E} \left[\min _{1\leq i\leq n}h(x_{i})\right]=\mathbb {E} [v_{1}]={\frac {1}{z+1}}}$.
${\displaystyle \square }$

The quantity ${\displaystyle \min _{1\leq i\leq n}h(x_{i})}$ can be computed with small space cost (for storing the current smallest hash value) by scan the input sequence in a single pass. Because as we proved its expectation is ${\displaystyle {\frac {1}{z+1}}}$, the smallest hash value ${\displaystyle Y=\min _{1\leq i\leq n}h(x_{i})}$ gives an unbiased estimator for ${\displaystyle {\frac {1}{z+1}}}$. However, ${\displaystyle {\frac {1}{Y}}-1}$ is not necessarily a good estimator for ${\displaystyle z}$. Actually, it is a rather poor estimator. Consider for example when ${\displaystyle z=1}$, all input elements are the same. In this case, there is only one hash value and ${\displaystyle Y=\min _{1\leq i\leq n}h(x_{i})}$ is distributed uniformly over ${\displaystyle [0,1]}$, thus ${\displaystyle {\frac {1}{Y}}-1}$ fails to be close enough to the correct answer 1 with high probability.

## Flajolet-Martin algorithm

The reason that the above estimator of a single hash function performs poorly is that the unbiased estimator ${\displaystyle \min _{1\leq i\leq n}h(x_{i})}$ has large variance. So a natural way to reduce this variance is to have multiple independent hash functions and take the average. This is precisely what Flajolet-Martin algorithm does.

Suppose that we can access to ${\displaystyle k}$ independent random hash functions ${\displaystyle h_{1},h_{2},\ldots ,h_{k}}$, where each ${\displaystyle h_{j}:\Omega \to [0,1]}$ is uniformly and independently distributed over all functions mapping ${\displaystyle \Omega }$ to ${\displaystyle [0,1]}$. Here ${\displaystyle k}$ is a parameter to be fixed by the desired approximation error ${\displaystyle \epsilon }$ and confidence error ${\displaystyle \delta }$. The Flajolet-Martin algorithm is given by the following pseudocode.

 Flajolet-Martin algorithm (Flajolet and Martin 1984) Suppose that ${\displaystyle h_{1},h_{2},\ldots ,h_{k}:\Omega \to [0,1]}$ are ${\displaystyle k}$ uniform and independent random hash functions, where ${\displaystyle k}$ is a parameter to be fixed later. Scan the input sequence ${\displaystyle x_{1},x_{2},\ldots ,x_{n}\in \Omega }$ in a single pass to compute: ${\displaystyle Y_{j}=\min _{1\leq i\leq n}h_{j}(x_{i})}$ for every ${\displaystyle j=1,2,\ldots ,k}$; average value ${\displaystyle {\overline {Y}}={\frac {1}{k}}\sum _{j=1}^{k}Y_{j}}$; return ${\displaystyle {\widehat {Z}}={\frac {1}{\overline {Y}}}-1}$ as the estimator.

The algorithm is easy to implement in data stream model, with a space cost of storing ${\displaystyle k}$ hash values. The following theorem guarantees that the algorithm returns an ${\displaystyle (\epsilon ,\delta )}$-estimator of the total number of distinct elements for a suitable ${\displaystyle k=O\left({\frac {1}{\epsilon ^{2}\delta }}\right)}$.

 Theorem For any ${\displaystyle \epsilon ,\delta <1/2}$, if ${\displaystyle k\geq \left\lceil {\frac {4}{\epsilon ^{2}\delta }}\right\rceil }$ then the output ${\displaystyle {\widehat {Z}}}$ always gives an ${\displaystyle (\epsilon ,\delta )}$-estimator of the correct answer ${\displaystyle z}$.

In the following we prove this main theorem for Flajolet-Martin algorithm.

An obstacle to analyze the estimator ${\displaystyle {\widehat {Z}}={\frac {1}{\overline {Y}}}-1}$ is that it is a nonlinear function of ${\displaystyle {\overline {Y}}}$ who is easier to analyze. Nevertheless, we observe that ${\displaystyle {\widehat {Z}}}$ is an ${\displaystyle (\epsilon ,\delta )}$-estimator of ${\displaystyle z}$ as long as ${\displaystyle {\overline {Y}}}$ is an ${\displaystyle (\epsilon /2,\delta )}$-estimator of ${\displaystyle {\frac {1}{z+1}}}$. This can be deduced by just verifying the following:

${\displaystyle {\frac {1-\epsilon /2}{z+1}}\leq {\overline {Y}}\leq {\frac {1+\epsilon /2}{z+1}}\implies (1-\epsilon )z\leq {\frac {1}{\overline {Y}}}-1\leq (1+\epsilon )z}$,

for ${\displaystyle \epsilon <{\frac {1}{2}}}$. Therefore,

${\displaystyle \Pr \left[\,(1-\epsilon )z\leq {\widehat {Z}}\leq (1+\epsilon )z\,\right]\geq \Pr \left[\,{\frac {1-\epsilon /2}{z+1}}\leq {\overline {Y}}\leq {\frac {1+\epsilon /2}{z+1}}\,\right]=\Pr \left[\,\left|{\overline {Y}}-{\frac {1}{z+1}}\right|\leq {\frac {\epsilon /2}{z+1}}\,\right]}$.

It is then sufficient to show that ${\displaystyle \Pr \left[\,\left|{\overline {Y}}-{\frac {1}{z+1}}\right|\leq {\frac {\epsilon /2}{z+1}}\,\right]\geq 1-\delta }$ for proving the main theorem above. We will see that this is equivalent to show the concentration inequality

${\displaystyle \Pr \left[\,\left|{\overline {Y}}-\mathbb {E} \left[{\overline {Y}}\right]\right|\leq {\frac {\epsilon /2}{z+1}}\,\right]\geq 1-\delta \quad \qquad ({\color {red}*})}$.
 Lemma The followings hold for each ${\displaystyle Y_{j}}$, ${\displaystyle j=1,2\ldots ,k}$, and ${\displaystyle {\overline {Y}}={\frac {1}{k}}\sum _{j=1}^{k}Y_{j}}$: ${\displaystyle \mathbb {E} \left[{\overline {Y}}\right]=\mathbb {E} \left[Y_{j}\right]={\frac {1}{z+1}}}$; ${\displaystyle \mathbf {Var} \left[Y_{j}\right]\leq {\frac {1}{(z+1)^{2}}}}$, and consequently ${\displaystyle \mathbf {Var} \left[{\overline {Y}}\right]\leq {\frac {1}{k(z+1)^{2}}}}$.
Proof.
 As in the case of single hash function, by symmetry it holds that ${\displaystyle \mathbb {E} [Y_{j}]={\frac {1}{z+1}}}$ for every ${\displaystyle j=1,2,\ldots ,k}$. Therefore, ${\displaystyle \mathbb {E} \left[{\overline {Y}}\right]={\frac {1}{k}}\sum _{j=1}^{k}\mathbb {E} [Y_{j}]={\frac {1}{z+1}}}$. Recall that each ${\displaystyle Y_{j}}$ is the minimum of ${\displaystyle z}$ random hash values uniformly and independently distributed over ${\displaystyle [0,1]}$. By geometry probability, it holds that for any ${\displaystyle y\in [0,1]}$, ${\displaystyle \Pr[Y_{j}>y]=(1-y)^{z}}$, which means ${\displaystyle \Pr[Y_{j}\leq y]=1-(1-y)^{z}}$. Taking the derivative with respect to ${\displaystyle y}$, we obtain the probability density function of random variable ${\displaystyle Y_{j}}$, which is ${\displaystyle z(1-y)^{z-1}}$. We then compute the second moment. ${\displaystyle \mathbb {E} [Y_{j}^{2}]=\int _{0}^{1}y^{2}z(1-y)^{z-1}\,\mathrm {d} y={\frac {2}{(z+1)(z+2)}}}$. The variance is bounded as ${\displaystyle \mathbf {Var} \left[Y_{j}\right]=\mathbb {E} \left[Y_{j}^{2}\right]-\mathbb {E} \left[Y_{j}\right]^{2}={\frac {2}{(z+1)(z+2)}}-{\frac {1}{(z+1)^{2}}}\leq {\frac {1}{(z+1)^{2}}}}$. Due to the (pairwise) independence between ${\displaystyle Y_{j}}$'s, ${\displaystyle \mathbf {Var} \left[{\overline {Y}}\right]=\mathbf {Var} \left[{\frac {1}{k}}\sum _{j=1}^{k}Y_{j}\right]={\frac {1}{k^{2}}}\sum _{j=1}^{k}\mathbf {Var} \left[Y_{j}\right]\leq {\frac {1}{k(z+1)^{2}}}}$.
${\displaystyle \square }$

We resume to prove the inequality ${\displaystyle ({\color {red}*})}$. By Chebyshev's inequality, it holds that

${\displaystyle \Pr \left[\,\left|{\overline {Y}}-\mathbb {E} \left[{\overline {Y}}\right]\right|>{\frac {\epsilon /2}{z+1}}\,\right]\leq {\frac {4}{\epsilon ^{2}}}(z+1)^{2}\mathbf {Var} \left[{\overline {Y}}\right]\leq {\frac {4}{\epsilon ^{2}k}}}$.

When ${\displaystyle k\geq \left\lceil {\frac {4}{\epsilon ^{2}\delta }}\right\rceil }$, this probability is at most ${\displaystyle \delta }$. The inequality ${\displaystyle ({\color {red}*})}$ is proved. As we discussed above, this proves the above main theorem for Flajolet-Martin algorithm.

## Uniform Hash Assumption (UHA)

In above we assume we can access to idealized random hash functions ${\displaystyle h:\Omega \to [0,1]}$ with real values. With a more careful calculation, one can show the same performance guarantee for hash functions with discrete values as ${\displaystyle h:\Omega \to [M]}$ where ${\displaystyle M=\mathrm {poly} (n)}$, that is, the hash values are strings of ${\displaystyle O(\log n)}$ bits.

Even with such improved analysis, a uniform random discrete function in form of ${\displaystyle h:[N]\to [M]}$ is not really efficient to store or to compute. By an information-theretical argument, it takes at least ${\displaystyle \Omega (N\log M)}$ bits to represent such a random hash function because this is the entropy of such uniform random function.

For the convenience of analysis, it is common to assume the following Uniform Hash Assumption (UHA) also known as Simple Uniform Hash Assumption (SUHA).

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

# Set Membership

A basic question in Computer Science is:

"${\displaystyle {\mbox{Is }}x\in S?}$"

for a set ${\displaystyle S}$ and an element ${\displaystyle x}$. This is the set membership problem.

Formally, given an arbitrary set ${\displaystyle S}$ of ${\displaystyle n}$ elements from a universe ${\displaystyle \Omega }$, we want to use a succinct data structure to represent this set ${\displaystyle S}$, so that upon each query of any element ${\displaystyle x}$ from the universe ${\displaystyle [N]}$, the question of whether ${\displaystyle x\in S}$ is efficiently answered. The complexity of such data structure is measured in two-fold:

• space cost: size of the data structure to represent a set ${\displaystyle S}$ of size ${\displaystyle n}$;
• time cost: time complexity of answering each query by accessing to the data structure.

Suppose that the universe ${\displaystyle \Omega }$ is of size ${\displaystyle N}$. Clearly, the membership problem can be solved by a dictionary data structure, e.g.:

• sorted table / balanced search tree: with space cost ${\displaystyle O(n\log N)}$ bits and time cost ${\displaystyle O(\log n)}$;
• perfect hashing of Fredman, Komlós & Szemerédi: with space cost ${\displaystyle O(n\log N)}$ bits and time cost ${\displaystyle O(1)}$.

Note that ${\displaystyle \log {N \choose n}=\Theta \left(n\log {\frac {N}{n}}\right)}$ is the entropy of sets ${\displaystyle S}$ of ${\displaystyle n}$ elements from a universe ${\displaystyle \Omega }$ of size ${\displaystyle N}$. Therefore it is necessary to use so many bits to represent a set without losing any information. Nevertheless, we can do better than this if we use a loss representation of the input set ${\displaystyle S}$ and tolerate a bounded error in answering queries. Such lossy representation of data is sometimes called a sketch.

## Bloom filter

The Bloom filter is a space-efficient hash table that solves the approximate membership problem with one-sided error (false positive).

Given a set ${\displaystyle S}$ of ${\displaystyle n}$ elements from a universe ${\displaystyle \Omega }$, a Bloom filter consists of an array ${\displaystyle A}$ of ${\displaystyle cn}$ bits, and ${\displaystyle k}$ hash functions ${\displaystyle h_{1},h_{2},\ldots ,h_{k}}$ map ${\displaystyle \Omega }$ to ${\displaystyle [cn]}$, where both ${\displaystyle c}$ and ${\displaystyle k}$ are parameters that we can try to optimize later.

As before, we assume the Uniform Hash Assumption (UHA): ${\displaystyle h_{1},h_{2},\ldots ,h_{k}}$ are mutually independent hash function where each ${\displaystyle h_{i}}$ is a uniform random hash function ${\displaystyle h_{i}:\Omega \to [cn]}$.

The Bloom filter works as follows:

 Bloom filter (Bloom 1970) Suppose ${\displaystyle h_{1},h_{2},\ldots ,h_{k}:\Omega \to [cn]}$ are uniform and independent random hash functions. Data structure construction: Given a set ${\displaystyle S\subset \Omega }$ of size ${\displaystyle n=|S|}$, the data structure is a Boolean array ${\displaystyle A}$ of ${\displaystyle cn}$ bits constructed as initialize all ${\displaystyle cn}$ bits of the Boolean array ${\displaystyle A}$ to 0; for each ${\displaystyle x\in S}$, let ${\displaystyle A[h_{i}(x)]=1}$ for all ${\displaystyle 1\leq i\leq k}$. Query resolution: Upon each query of an arbitrary ${\displaystyle x\in \Omega }$, answer "yes" if ${\displaystyle A[h_{i}(x)]=1}$ for all ${\displaystyle 1\leq i\leq k}$ and "no" if otherwise.

The Boolean array is our data structure, whose size is ${\displaystyle cn}$ bits. With Uniform Hash Assumption (UHA), the time cost of the data structure for answering each query is ${\displaystyle O(k)}$.

When the answer returned by the algorithm is "no", it holds that ${\displaystyle A[h_{i}(x)]=0}$ for some ${\displaystyle 1\leq i\leq k}$, in which case the query ${\displaystyle x}$ must not belong to the set ${\displaystyle S}$. Thus, the Bloom filter has no false negatives.

On the other hand, when the answer returned by the algorithm is "yes", ${\displaystyle A[h_{i}(x)]=1}$ for all ${\displaystyle 1\leq i\leq k}$. It is still possible for some ${\displaystyle x\not \in S}$ that all bits ${\displaystyle A[h_{i}(x)]}$ are set by elements in ${\displaystyle S}$. We want to bound such false positive, that is, the following probability for an ${\displaystyle x\not \in S}$:

${\displaystyle \Pr[\,\forall 1\leq i\leq k,A[h_{i}(x)]=1\,]}$,

which by independence between different hash functions and by symmetry is equal to:

${\displaystyle \Pr[\,A[h_{1}(x)]=1\,]^{k}=(1-\Pr[\,A[h_{1}(x)]=0\,])^{k}}$.

For an element ${\displaystyle x\not \in S}$, its hash value ${\displaystyle h_{1}(x)}$ is independent of all hash values ${\displaystyle h_{i}(y)}$ for all ${\displaystyle 1\leq i\leq k}$ and all ${\displaystyle y\in S}$. This is due to the Uniform Hash Assumption. The hash value ${\displaystyle h_{1}(x)}$ of ${\displaystyle x\not \in S}$ is then independent of the content of the array ${\displaystyle A}$. Therefore, the probability of this position ${\displaystyle A[h_{1}(x)]}$ missed by all ${\displaystyle kn}$ updates to the Boolean array ${\displaystyle A}$ caused by all ${\displaystyle n}$ elements in ${\displaystyle S}$ is:

${\displaystyle \Pr[\,A[h_{1}(x)]=0\,]=\left(1-{\frac {1}{cn}}\right)^{kn}\approx e^{-k/c}.}$

Putting everything together, for any ${\displaystyle x\not \in S}$, the false positive is bounded as:

{\displaystyle {\begin{aligned}\Pr[\,{\text{wrongly answer ''yes''}}\,]&=\Pr[\,\forall 1\leq i\leq k,A[h_{i}(x)]=1\,]\\&=\Pr[\,A[h_{1}(x)]=1\,]^{k}=(1-\Pr[\,A[h_{1}(x)]=0\,])^{k}\\&=\left(1-\left(1-{\frac {1}{cn}}\right)^{kn}\right)^{k}\\&\approx \left(1-e^{-k/c}\right)^{k}\end{aligned}}}

which is ${\displaystyle (0.6185)^{c}}$ when ${\displaystyle k=c\ln 2}$.

Bloom filter solves the membership query with a small constant error of false positives using a data structure of ${\displaystyle O(n)}$ bits which answers each query with ${\displaystyle O(1)}$ time cost.

# Frequency Estimation

Suppose that ${\displaystyle \Omega }$ is the data universe. The frequency estimation problem is defined as follows.

• Data: a sequence of (not necessarily distinct) elements ${\displaystyle x_{1},x_{2},\ldots ,x_{n}\in \Omega }$;
• Query: an element ${\displaystyle x\in \Omega }$;
• Output: an estimation ${\displaystyle {\hat {f}}_{x}}$ of the frequency ${\displaystyle f_{x}\triangleq |\{i\mid x_{i}=x\}|}$ of ${\displaystyle x}$ in input data.

We still want to give an algorithm in the data stream model: the algorithm scan the input sequence ${\displaystyle x_{1},x_{2},\ldots ,x_{n}}$ to construct a succinct data structure, such that upon each query of ${\displaystyle x\in \Omega }$, the algorithm returns an estimation of the frequency ${\displaystyle f_{x}}$.

Clearly this problem can always be solved by storing all appeared distinct elements along with their frequencies. However, the space cost of this straightforward solution is rather high. Instead, we want to use a lossy representation (a sketch) of input data which uses significantly less space but can still answer queries with tolarable accuracy.

Formally, upon each query of ${\displaystyle x\in \Omega }$, the algorithm should return an answer ${\displaystyle {\hat {f}}_{x}}$ satisfying:

${\displaystyle \Pr \left[\,\left|{\hat {f}}_{x}-f_{x}\right|\leq \epsilon n\,\right]\geq 1-\delta }$.

Note that this notion of approximation is with bounded additive error which is weaker than the notion of ${\displaystyle (\epsilon ,\delta )}$-estimator, whose error bound is multiplicative.

With such weak accuracy guarantee, its is possible to give a succinct data structure whose size is determined only by the error bounds ${\displaystyle \epsilon }$ and ${\displaystyle \delta }$ but independent of ${\displaystyle n}$, because only the frequencies of those heavy hitters (elements ${\displaystyle x}$ with high frequencies ${\displaystyle f_{x}>\epsilon n}$) need to be memorized, and there are at most ${\displaystyle 1/\epsilon }$ many such heavy hitters.

## Count-min sketch

The count-min sketch given by Cormode and Muthukrishnan is an elegant data structure for frequency estimation.

The data structure is a two-dimensional ${\displaystyle k\times m}$ integer array, where ${\displaystyle k}$ and ${\displaystyle m}$ are two parameters to be determined by the error bounds ${\displaystyle \epsilon }$ and ${\displaystyle \delta }$. We still adopt the Uniform Hash Assumption to assume that we have access to ${\displaystyle k}$ mutually independent uniform random hash functions ${\displaystyle h_{1},h_{2},\ldots ,h_{k}:\Omega \to [m]}$.

 Count-min sketch (Cormode and Muthukrishnan 2003) Suppose ${\displaystyle h_{1},h_{2},\ldots ,h_{k}:\Omega \to [m]}$ are uniform and independent random hash functions. Data structure construction: Given a sequence ${\displaystyle x_{1},x_{2},\ldots ,x_{n}\in \Omega }$, the data structure is a two-dimensional ${\displaystyle k\times m}$ integer array ${\displaystyle CMS[k][m]}$ constructed as initialize all entries of ${\displaystyle CMS[k][m]}$ to 0; for ${\displaystyle i=1,2,\ldots ,n}$, upon receiving ${\displaystyle x_{i}}$: for every ${\displaystyle 1\leq j\leq k}$, evaluate ${\displaystyle h_{j}(x_{i})}$ and ${\displaystyle CMS[j][h_{j}(x_{i})]++}$. Query resolution: Upon each query of an arbitrary ${\displaystyle x\in \Omega }$, return ${\displaystyle {\hat {f}}_{x}=\min _{1\leq j\leq k}CMS[j][h_{j}(x)]}$.

It is easy to see that the space cost of count-min sketch is ${\displaystyle O(km)}$ memory words, or ${\displaystyle O(km\log n)}$ bits. Each query is answered within time cost ${\displaystyle O(k)}$, assuming that an evaluation of hash function can be done in unit or constant time. We then analyze the error bounds.

First, it is easy to observe that for any query ${\displaystyle x\in \Omega }$ and every hash function ${\displaystyle 1\leq j\leq k}$, it always holds for the corresponding entry in the count-min sketch

${\displaystyle CMS[j][h_{j}(x)]\geq f_{x}}$,

because the appearances of element ${\displaystyle x}$ in the input sequence contribute at least ${\displaystyle f_{x}}$ to the value of ${\displaystyle CMS[j][h_{j}(x)]}$.

Therefore, for any query ${\displaystyle x\in \Omega }$ it always holds for the answer ${\displaystyle {\hat {f}}_{x}=\min _{1\leq j\leq k}CMS[j][h_{j}(x)]\geq f_{x}}$, which means

${\displaystyle \Pr \left[\,\left|{\hat {f}}_{x}-f_{x}\right|\geq \epsilon n\,\right]=\Pr \left[\,{\hat {f}}_{x}-f_{x}\geq \epsilon n\,\right]=\prod _{j=1}^{k}\Pr[\,CMS[j][h_{j}(x)]-f_{x}\geq \epsilon n\,],\quad \qquad ({\color {red}\diamondsuit })}$

where the second equation is due to the mutual independence of random hash functions ${\displaystyle h_{1},h_{2},\ldots ,h_{k}}$.

It remains to upper bound the probability ${\displaystyle \Pr[\,CMS[j][h_{j}(x)]-f_{x}\geq \epsilon n\,]}$, which can be done by calculating the expectation of ${\displaystyle CMS[j][h_{j}(x)]}$.

 Proposition For any ${\displaystyle x\in \Omega }$ and every ${\displaystyle 1\leq j\leq k}$, it holds that ${\displaystyle \mathbb {E} \left[CMS[j][h_{j}(x)]\right]\leq f_{x}+{\frac {n}{m}}}$.
Proof.
 The value of ${\displaystyle CMS[j][h_{j}(x)]}$ is constituted by the frequency ${\displaystyle f_{x}}$ of ${\displaystyle x}$ and the frequencies ${\displaystyle f_{y}}$ of all other elements ${\displaystyle y\neq x}$ among ${\displaystyle x_{1},x_{2},\ldots ,x_{n}}$, thus {\displaystyle {\begin{aligned}CMS[j][h_{j}(x)]&=f_{x}+\sum _{\scriptstyle y\in \{x_{1},\ldots ,x_{n}\}\setminus \{x\} \atop \scriptstyle h_{j}(y)=h_{j}(x)}f_{y}\\&=f_{x}+\sum _{y\in \{x_{1},\ldots ,x_{n}\}\setminus \{x\}}f_{y}\cdot I[h_{j}(y)=h_{j}(x)]\end{aligned}}} where ${\displaystyle I[h_{j}(y)=h_{j}(x)]}$ denotes the Boolean random variable that indicates the occurrence of event ${\displaystyle h_{j}(y)=h_{j}(x)}$. By linearity of expectation, ${\displaystyle \mathbb {E} [CMS[j][h_{j}(x)]]=f_{x}+\sum _{y\in \{x_{1},x_{2},\ldots ,x_{n}\}\setminus \{x\}}f_{y}\cdot \Pr[h_{j}(y)=h_{j}(x)]}$. Due to Uniform Hash Assumption (UHA), ${\displaystyle h_{j}:\Omega \to [m]}$ is a uniform random function. For any ${\displaystyle y\neq x}$, the probability of hash collision is ${\displaystyle \Pr[h_{j}(y)=h_{j}(x)]={\frac {1}{m}}}$. Therefore, {\displaystyle {\begin{aligned}\mathbb {E} [CMS[j][h_{j}(x)]]&=f_{x}+{\frac {1}{m}}\sum _{y\in \{x_{1},\ldots ,x_{n}\}\setminus \{x\}}f_{y}\\&\leq f_{x}+{\frac {1}{m}}\sum _{y\in \{x_{1},\ldots ,x_{n}\}}f_{y}\\&=f_{x}+{\frac {n}{m}},\end{aligned}}} where the last equation is due to the obvious identity ${\displaystyle \sum _{y\in \{x_{1},\ldots ,x_{n}\}}f_{y}=n}$.
${\displaystyle \square }$

The above proposition shows that for any ${\displaystyle x\in \Omega }$ and every ${\displaystyle 1\leq j\leq k}$

${\displaystyle \mathbb {E} \left[CMS[j][h_{j}(x)]-f_{x}\right]\leq {\frac {n}{m}}}$.

Recall that ${\displaystyle CMS[j][h_{j}(x)]\geq f_{x}}$ always holds, thus ${\displaystyle CMS[j][h_{j}(x)]-f_{x}}$ is a positive random variable. By Markov's inequality, we have

${\displaystyle \Pr[\,CMS[j][h_{j}(x)]-f_{x}\geq \epsilon n\,]\leq {\frac {1}{\epsilon m}}}$.

Combining with above equation ${\displaystyle ({\color {red}\diamondsuit })}$, we have

${\displaystyle \Pr \left[\,\left|{\hat {f}}_{x}-f_{x}\right|\geq \epsilon n\,\right]=(\Pr[\,CMS[j][h_{j}(x)]-f_{x}\geq \epsilon n\,])^{k}\leq {\frac {1}{(\epsilon m)^{k}}}}$.

By setting ${\displaystyle m=\left\lceil {\frac {\mathrm {e} }{\epsilon }}\right\rceil }$ and ${\displaystyle k=\left\lceil \ln {\frac {1}{\delta }}\right\rceil }$, the above error probability is bounded as ${\displaystyle {\frac {1}{(\epsilon m)^{k}}}\leq \delta }$.

For any positive ${\displaystyle \epsilon }$ and ${\displaystyle \delta }$, the count-min sketch gives a data structure of size ${\displaystyle O(km)=O\left({\frac {1}{\epsilon }}\log {\frac {1}{\delta }}\right)}$ (in memory words) and answering each query ${\displaystyle x\in \Omega }$ in time ${\displaystyle O(k)=O\left({\frac {1}{\epsilon }}\right)}$ with the following accuracy guarantee:

${\displaystyle \Pr \left[\,\left|{\hat {f}}_{x}-f_{x}\right|\leq \epsilon n\,\right]\geq 1-\delta }$.