Flajolet–Martin algorithm

From HandWiki
Revision as of 07:32, 27 June 2023 by JMinHep (talk | contribs) (over-write)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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

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

[math]\displaystyle{ y = \sum_{k \geq 0} \mathrm{bit}(y,k) 2^k. }[/math]

We then define a function [math]\displaystyle{ \rho(y) }[/math] that outputs the position of the least-significant set bit in the binary representation of [math]\displaystyle{ y }[/math], and [math]\displaystyle{ L }[/math] if no such set bit can be found as all bits are zero:

[math]\displaystyle{ \rho(y) = \begin{cases} \min \{k \geq 0 \mid \mathrm{bit}(y,k) \neq 0 \} & y\gt 0 \\ L & y=0 \end{cases} }[/math]

Note that with the above definition we are using 0-indexing for the positions, starting from the least significant bit. For example, [math]\displaystyle{ \rho(13) = \rho(1101_{2}) = 0 }[/math], since the least significant bit is a 1 (0th position), and [math]\displaystyle{ \rho(8) = \rho(1000_{2}) = 3 }[/math], 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 [math]\displaystyle{ 2^k }[/math] (a one, followed by [math]\displaystyle{ k }[/math] zeroes) is [math]\displaystyle{ 2^{-(k+1)} }[/math], since this corresponds to flipping [math]\displaystyle{ k }[/math] heads and then a tail with a fair coin.

Now the Flajolet–Martin algorithm for estimating the cardinality of a multiset [math]\displaystyle{ M }[/math] is as follows:

  1. Initialize a bit-vector BITMAP to be of length [math]\displaystyle{ L }[/math] and contain all 0s.
  2. For each element [math]\displaystyle{ x }[/math] in [math]\displaystyle{ M }[/math]:
    1. Calculate the index [math]\displaystyle{ i = \rho(\mathrm{hash}(x)) }[/math].
    2. Set [math]\displaystyle{ \mathrm{BITMAP}[i] = 1 }[/math].
  3. Let [math]\displaystyle{ R }[/math] denote the smallest index [math]\displaystyle{ i }[/math] such that [math]\displaystyle{ \mathrm{BITMAP}[i] = 0 }[/math].
  4. Estimate the cardinality of [math]\displaystyle{ M }[/math] as [math]\displaystyle{ 2^R / \phi }[/math], where [math]\displaystyle{ \phi \approx 0.77351 }[/math].

The idea is that if [math]\displaystyle{ n }[/math] is the number of distinct elements in the multiset [math]\displaystyle{ M }[/math], then [math]\displaystyle{ \mathrm{BITMAP}[0] }[/math] is accessed approximately [math]\displaystyle{ n/2 }[/math] times, [math]\displaystyle{ \mathrm{BITMAP}[1] }[/math] is accessed approximately [math]\displaystyle{ n/4 }[/math] times and so on. Consequently, if [math]\displaystyle{ i \gg \log_2 n }[/math], then [math]\displaystyle{ \mathrm{BITMAP}[i] }[/math] is almost certainly 0, and if [math]\displaystyle{ i \ll \log_2 n }[/math], then [math]\displaystyle{ \mathrm{BITMAP}[i] }[/math] is almost certainly 1. If [math]\displaystyle{ i \approx \log_2 n }[/math], then [math]\displaystyle{ \mathrm{BITMAP}[i] }[/math] can be expected to be either 1 or 0.

The correction factor [math]\displaystyle{ \phi \approx 0.77351 }[/math] 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 [math]\displaystyle{ k }[/math] different hash functions and combine the results from the different runs. One idea is to take the mean of the [math]\displaystyle{ k }[/math] 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 [math]\displaystyle{ 2^R / \phi }[/math], where [math]\displaystyle{ R }[/math] is integer. A common solution is to combine both the mean and the median: Create [math]\displaystyle{ k \cdot l }[/math] hash functions and split them into [math]\displaystyle{ k }[/math] distinct groups (each of size [math]\displaystyle{ l }[/math]). Within each group use the mean for aggregating together the [math]\displaystyle{ l }[/math] results, and finally take the median of the [math]\displaystyle{ k }[/math] 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

References

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

Additional sources