Suvarna Garge (Editor)

Flajolet–Martin algorithm

Updated on
Edit
Like
Comment
Share on FacebookTweet on TwitterShare on LinkedInShare on Reddit

The Flajolet–Martin algorithm is an algorithm for approximating the number of distinct elements in a stream with a single pass and space-consumption logarithmic in the maximal number of possible distinct elements in the stream. The algorithm was introduced by Philippe Flajolet and G. Nigel Martin in their 1984 article "Probabilistic Counting Algorithms for Data Base Applications". Later it has been refined in "LogLog counting of large cardinalities" by Marianne Durand and Philippe Flajolet, and "HyperLogLog: The analysis of a near-optimal cardinality estimation algorithm" by Philippe Flajolet et al.

Contents

In their 2010 article "An optimal algorithm for the distinct elements problem", Daniel M. Kane, Jelani Nelson and David P. Woodruff give an improved algorithm, which uses nearly optimal space and has optimal O(1) update and reporting times.

The algorithm

Assume that we are given a hash function h a s h ( x ) that maps input x to integers in the range [ 0 ; 2 L 1 ] , and where the outputs are sufficiently uniformly distributed. Note that the set of integers from 0 to 2 L 1 corresponds to the set of binary strings of length L . For any non-negative integer y , define b i t ( y , k ) to be the k -th bit in the binary representation of y , such that:

y = k 0 b i t ( y , k ) 2 k .

We then define a function ρ ( y ) that outputs the position of the least significant bit in the binary representation of y :

ρ ( y ) = min k 0 b i t ( y , k ) 0 ,

where ρ ( 0 ) = L . Note that with the above definition we are using 0-indexing for the positions. For example, ρ ( 13 ) = ρ ( 1101 2 ) = 0 , since the least significant bit is a 1 (0th position), and ρ ( 8 ) = ρ ( 1000 2 ) = 3 , since the least significant bit is at the 3rd position. At this point, note that under the assumption that the output of our hash function is uniformly distributed, then the probability of observing a hash output ending with 2 k (a one, followed by k zeroes) is 2 ( k + 1 ) , since this corresponds to flipping k heads and then a tail with a fair coin.

Now the Flajolet–Martin algorithm for estimating the cardinality of a multiset M is as follows:

  1. Initialize a bit-vector BITMAP to be of length L and contain all 0s.
  2. For each element x in M :
    1. Calculate the index i = ρ ( h a s h ( x ) ) .
    2. Set B I T M A P [ i ] = 1 .
  3. Let R denote the smallest index i such that B I T M A P [ i ] = 0 .
  4. Estimate the cardinality of M as 2 R / ϕ , where ϕ 0.77351 .

The idea is that if n is the number of distinct elements in the multiset M , then B I T M A P [ 0 ] is accessed approximately n / 2 times, B I T M A P [ 1 ] is accessed approximately n / 4 times and so on. Consequently, if i log 2 n , then B I T M A P [ i ] is almost certainly 0, and if i log 2 n , then B I T M A P [ i ] is almost certainly 1. If i log 2 n , then B I T M A P [ i ] can be expected to be either 1 or 0.

The correction factor ϕ 0.77351 is found by calculations, which can be found in the original article.

Improving accuracy

A problem with the Flajolet–Martin algorithm in the above form is that the results vary significantly. A common solution has been to run the algorithm multiple times with k different hash functions and combine the results from the different runs. One idea is to take the mean of the k results together from each hash function, obtaining a single estimate of the cardinality. The problem with this is that averaging is very susceptible to outliers (which are likely here). A different idea is to use the median, which is less prone to be influences by outliers. The problem with this is that the results can only take form 2 R / ϕ , where R is integer. A common solution is to combine both the mean and the median: Create k l hash functions and split them into k distinct groups (each of size l ). Within each group use the median for aggregating together the l results, and finally take the mean of the k group estimates as the final estimate.

The 2007 HyperLogLog algorithm splits the multiset into subsets and estimates their cardinalities, then it uses the harmonic mean to combine them into an estimate for the original cardinality.

References

Flajolet–Martin algorithm Wikipedia