Flajolet–Martin algorithm explained

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 count-distinct problem). The algorithm was introduced by Philippe Flajolet and G. Nigel Martin in their 1984 article "Probabilistic Counting Algorithms for Data Base Applications".[1] Later it has been refined in "LogLog counting of large cardinalities" by Marianne Durand and Philippe Flajolet,[2] and "HyperLogLog: The analysis of a near-optimal cardinality estimation algorithm" by Philippe Flajolet et al.[3]

In their 2010 article "An optimal algorithm for the distinct elements problem",[4] 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

hash(x)

that maps input

x

to integers in the range

[0;2L-1]

, and where the outputs are sufficiently uniformly distributed. Note that the set of integers from 0 to

2L-1

corresponds to the set of binary strings of length

L

. For any non-negative integer

y

, define

bit(y,k)

to be the

k

-th bit in the binary representation of

y

, such that:

y=\sumkbit(y,k)2k.

We then define a function

\rho(y)

that outputs the position of the least-significant set bit in the binary representation of

y

, and

L

if no such set bit can be found as all bits are zero:

\rho(y)= \begin{cases} min\{k\geq0\midbit(y,k)0\}&y>0\\ L&y=0 \end{cases}

Note that with the above definition we are using 0-indexing for the positions, starting from the least significant bit. For example,

\rho(13)=\rho(11012)=0

, since the least significant bit is a 1 (0th position), and

\rho(8)=\rho(10002)=3

, since the least significant set 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

2k

(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.

M

is as follows:
  1. Initialize a bit-vector BITMAP to be of length

L

and contain all 0s.
  1. For each element

x

in

M

:
    1. Calculate the index

i=\rho(hash(x))

.
    1. Set

BITMAP[i]=1

.
  1. Let

R

denote the smallest index

i

such that

BITMAP[i]=0

.
  1. Estimate the cardinality of

M

as

2R/\phi

, where

\phi0.77351

.

The idea is that if

n

is the number of distinct elements in the multiset

M

, then

BITMAP[0]

is accessed approximately

n/2

times,

BITMAP[1]

is accessed approximately

n/4

times and so on. Consequently, if

i\gglog2n

, then

BITMAP[i]

is almost certainly 0, and if

i\lllog2n

, then

BITMAP[i]

is almost certainly 1. If

ilog2n

, then

BITMAP[i]

can be expected to be either 1 or 0.

The correction factor

\phi0.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

2R/\phi

, where

R

is integer. A common solution is to combine both the mean and the median: Create

kl

hash functions and split them into

k

distinct groups (each of size

l

). Within each group use the mean for aggregating together the

l

results, and finally take the median of the

k

group estimates as the final estimate.[5]

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.[3]

See also

Additional sources

Notes and References

  1. 10.1016/0022-0000(85)90041-8 . Probabilistic counting algorithms for data base applications . Journal of Computer and System Sciences . 31 . 2 . 182 - 209 . 1985 . Flajolet . Philippe . Martin . G. Nigel . 2016-12-11.
  2. Book: 10.1007/978-3-540-39658-1_55 . Loglog Counting of Large Cardinalities . http://algo.inria.fr/flajolet/Publications/DuFl03-LNCS.pdf . 2016-12-11 . Algorithms - ESA 2003 . 2832 . 605 . Lecture Notes in Computer Science . 2003 . Durand . Marianne . Flajolet . Philippe . 978-3-540-20064-2.
  3. 10.1.1.76.4286 . Philippe . Flajolet . Éric . Fusy . Olivier . Gandouet . Frédéric . Meunier . Hyperloglog: The analysis of a near-optimal cardinality estimation algorithm . 2016-12-11 . 2007 . AH . 127–146 . Discrete Mathematics and Theoretical Computer Science Proceedings . Nancy, France.
  4. Book: 10.1145/1807085.1807094 . An optimal algorithm for the distinct elements problem. http://researcher.watson.ibm.com/researcher/files/us-dpwoodru/knw11.pdf . 2016-12-11 . Proceedings of the twenty-ninth ACM SIGMOD-SIGACT-SIGART symposium on Principles of database systems of data - PODS '10 . 41 . 2010 . Kane . Daniel M. . Nelson . Jelani . Woodruff . David P. . 978-1-4503-0033-9. 10006932 .
  5. Book: Leskovec, Rajaraman, Ullman . Mining of Massive Datasets . 2014 . Cambridge University Press . 144 . 2nd . 2022-05-30.