In many applications, data comes as a stream S={x1,x2,...,xn}, where each xi,i∈[n] belongs to the domain U, with size ∣U∣=N.
With out lost of generality, suppose each element in U appear at least once in S.
Problem. Estimate N, i.e., the number of distinct elements in S.
Naive solutions include
Sorting. With quicksort, this can be preformed in O(nlogn) expected time and O(n) space.
Hashing. With hashing (e.g., Cuckoo hashing), this can be done in O(n) time and O(n) space with two level hashing.
In real world application, n could be extremely large.
Therefore, the demand for O(n) space is expansive or even impossible.
Question. Can we perform this with O~(1) space?
If we allow some error, this is possible.
Flajolet and Martin Algorithm
Let H be the collection of all functions from U to [0,1].
Suppose that we pick a function h uniformly at randomly from H, then it maps elements in U independently and uniformly at random to real numbers in [0,1].
Question: can we find another family of U so that the following analysis still hold?
The algorithm works as follows.
Flajolet and Martin Algorithm.
Denote h(S)={h(x1),h(x2),...,h(xn)};
Let Z←minh(S);
Return1/Z−1.
Expectation
Observation.Pr[Z≥z]=(1−z)N and Pr[Z≤z]=1−(1−z)N.
Hence the density function is given as follows.
Density Function. Let p be the density function of Z, then
p(z)=∂z∂Pr[Z≤z]=N(1−z)N−1.
We are ready to discuss the expectation.
Theorem.E[Z]=1/(N+1).
Proof 1.
E[Z]=∫z=01z⋅p(z)dz=N+11.
□
Proof 2.
Another way to prove this is to introduce an additional element xn+1 which is different from any other element in U and let S′=S∪{xn+1}.
What is the probability that h(xn+1) is the smallest in h(S′), i.e., minh(S′)=h(xn+1)? Conditioning on the minimum value of {h(x1),h(x2),...,h(xn)} (Z) being some z (z∈[0,1]), the probability becomes
But, by symmetry, Pr[minh(S′)=h(xn+1)]=1/(N+1). Therefore, E[Z]=1/(N+1).
□
Next we upper bound and lower the value of Z.
Variance
As we need to bound the two side error of Z, we can't use Markov inequality but rely on Chebyshev's inequality. In this section, we study the variance of Z.
By symmetry, we can prove the inequality of the other hand.
Remark 2: Suppose that we have a function h that maps each of the N elements to a non-negative real number according to exponential distribution exp(−1) independently (the same element is mapped to the same number), then the minimum of these real numbers serves also as an unbiased estimator of N1. To verify this,
Similarly, we can show that if we repeat the experiment k=O(ϵ2ln1/δ) times, with probability at least 1−δ, the e1 smallest elements is an 1±ϵ estimator of N1.
Solution 2
Suppose we only want to know whether N≥t or N≤t/2 for a given integer t, is this possible?
The answer is yes and the solution is amazingly easy. We hash each element in U uniformly at random into the range [1,2,3,...,t]. If N≥t, the probability that no elements is assigned the value t is given by
(1−1/t)N≤(1−1/t)t≤1/e
The second inequality holds since (1−1/t)≤e1/t.
On the other hand, when N≤t/2, the probability that no element is assigned the value t is greater than
(1−1/t)N≥(1−1/t)t/2≥1/e
The second inequality holds since (1−1/t)≥e−2/t=1−t2+2!1(t2)2−3!1(t2)3+... when 1/t≤1/2.
We can boost the probability to some defined threshold 1−δ by repeating k=log1/δ times and check whether the majority of the answers are "yes". Note that the expected number of "yes" is more than (1−1/e)k and by chernoff bound the probability that less than 0.5k "yes" are returned is given by δ.
Now, to estimate N, we can set up logn values of t:1,2,4,8,...,n and test for each value of t whether N≥t or N≤t/2. We can find an interval [t1,t2], such that N∈[t1,t2] and N/2≤t1≤t2≤2⋅N. The space complexity is O(lognlog1/δ).
Reference
[1] M. Charikar, T. Li, and A. Xue, “Lecture 1: Counting Distinct Elements,”, CS369G: Algorithmic Techniques for Big Data.