Count-distinct problem

From HandWiki

In computer science, the count-distinct problem[1] (also known in applied mathematics as the cardinality estimation problem) is the problem of finding the number of distinct elements in a data stream with repeated elements. This is a well-known problem with numerous applications. The elements might represent IP addresses of packets passing through a router, unique visitors to a web site, elements in a large database, motifs in a DNA sequence, or elements of RFID/sensor networks.

Formal definition

Instance: Consider a stream of elements [math]\displaystyle{ x_1, x_2, \ldots, x_s }[/math] with repetitions, and an integer [math]\displaystyle{ m }[/math]. Let [math]\displaystyle{ n }[/math] denote the number of distinct elements in the stream, represented as [math]\displaystyle{ \{e_1, e_2, \ldots, e_n\} }[/math], where [math]\displaystyle{ n = |\{e_1, e_2, \ldots, e_n\}| }[/math].
Objective: Find an estimate [math]\displaystyle{ \widehat{n} }[/math] of [math]\displaystyle{ n }[/math] using only [math]\displaystyle{ m }[/math] storage units, where [math]\displaystyle{ m \ll n }[/math].

An example of an instance for the cardinality estimation problem is the stream: [math]\displaystyle{ a,b,a,c,d,b,d }[/math]. For this instance, [math]\displaystyle{ n = |\left\{ {a,b,c,d}\right\}| = 4 }[/math].

Naive solution

The naive solution to the problem is as follows:

 Initialize a counter, c, to zero, [math]\displaystyle{  c \leftarrow 0  }[/math].
 Initialize an efficient dictionary data structure, D, such as hash table or search tree in which insertion and membership can be performed quickly.  
 For each element [math]\displaystyle{  x_i  }[/math], a membership query is issued. 
     If [math]\displaystyle{  x_i  }[/math] is not a member of D ([math]\displaystyle{  x_i \notin D  }[/math])
         Add [math]\displaystyle{  x_i  }[/math] to D
         Increase c by one, [math]\displaystyle{  c \leftarrow c + 1 }[/math]
     Otherwise ([math]\displaystyle{  x_i \in D  }[/math]) do nothing.
 Output [math]\displaystyle{  n = c  }[/math].

As long as the number of distinct elements is not too big, D fits in main memory and an exact answer can be retrieved. However, this approach does not scale for bounded storage, or if the computation performed for each element [math]\displaystyle{ x_i }[/math] should be minimized. In such a case, several streaming algorithms have been proposed that use a fixed number of storage units.

HyperLogLog algorithm

Main page: HyperLogLog

Streaming algorithms

To handle the bounded storage constraint, streaming algorithms use a randomization to produce a non-exact estimation of the distinct number of elements, [math]\displaystyle{ n }[/math]. State-of-the-art estimators hash every element [math]\displaystyle{ e_j }[/math] into a low-dimensional data sketch using a hash function, [math]\displaystyle{ h(e_j) }[/math]. The different techniques can be classified according to the data sketches they store.

Min/max sketches

Min/max sketches[2][3] store only the minimum/maximum hashed values. Examples of known min/max sketch estimators: Chassaing et al.[4] presents max sketch which is the minimum-variance unbiased estimator for the problem. The continuous max sketches estimator[5] is the maximum likelihood estimator. The estimator of choice in practice is the HyperLogLog algorithm.[6]

The intuition behind such estimators is that each sketch carries information about the desired quantity. For example, when every element [math]\displaystyle{ e_j }[/math] is associated with a uniform RV, [math]\displaystyle{ h(e_j) \sim U(0,1) }[/math], the expected minimum value of [math]\displaystyle{ h(e_1),h(e_2), \ldots, h(e_n) }[/math] is [math]\displaystyle{ 1/(n+1) }[/math]. The hash function guarantees that [math]\displaystyle{ h(e_j) }[/math] is identical for all the appearances of [math]\displaystyle{ e_j }[/math]. Thus, the existence of duplicates does not affect the value of the extreme order statistics.

There are other estimation techniques other than min/max sketches. The first paper on count-distinct estimation[7] describes the Flajolet–Martin algorithm, a bit pattern sketch. In this case, the elements are hashed into a bit vector and the sketch holds the logical OR of all hashed values. The first asymptotically space- and time-optimal algorithm for this problem was given by Daniel M. Kane, Jelani Nelson, and David P. Woodruff.[8]

Bottom-m sketches

Bottom-m sketches [9] are a generalization of min sketches, which maintain the [math]\displaystyle{ m }[/math] minimal values, where [math]\displaystyle{ m \geq 1 }[/math]. See Cosma et al.[2] for a theoretical overview of count-distinct estimation algorithms, and Metwally [10] for a practical overview with comparative simulation results.

CVM Algorithm

Compared to other approximation algorithms for the count-distinct problem the CVM Algorithm uses sampling instead of hashing. The CVM Algorithm provides an unbiased estimator for the number of distinct elements in a stream, however the bound on error is not currently known. Here is the algorithm:[11]

 Initialize a counter, t, to zero, [math]\displaystyle{  t \leftarrow 0  }[/math].
 Initialize an empty buffer, B.  
 For each element [math]\displaystyle{  a_t  }[/math] in data stream [math]\displaystyle{  A  }[/math] of size [math]\displaystyle{  n  }[/math] do: 
   [math]\displaystyle{  t \leftarrow t + 1  }[/math]
   [math]\displaystyle{  a \leftarrow a_t  }[/math]
   If [math]\displaystyle{  a  }[/math] is in B then
       Delete [math]\displaystyle{  a  }[/math] from B
   End If
   [math]\displaystyle{  u \leftarrow  }[/math] random number in [math]\displaystyle{  [0, 1)  }[/math]
   If [math]\displaystyle{  u \geq p  }[/math] then
       NEXT
   Else If [math]\displaystyle{  |B| \lt  s  }[/math] then
       Insert [math]\displaystyle{  (a, u)  }[/math] into B
       NEXT
   Else
       [math]\displaystyle{  (a', u') \leftarrow (a, u)  }[/math] of max [math]\displaystyle{  u  }[/math] in B
       If [math]\displaystyle{  u \gt  u'  }[/math] then
           [math]\displaystyle{  p \leftarrow u  }[/math]
       Else
           Delete [math]\displaystyle{  (a', u')  }[/math] from B
           Insert [math]\displaystyle{  (a, u)  }[/math] into B
           [math]\displaystyle{  p \leftarrow u'  }[/math]
       End If
   End If
 End For
return [math]\displaystyle{  |B| / p  }[/math].

Weighted count-distinct problem

In its weighted version, each element is associated with a weight and the goal is to estimate the total sum of weights. Formally,

Instance: A stream of weighted elements [math]\displaystyle{ x_1,x_2,\ldots,x_s }[/math] with repetitions, and an integer [math]\displaystyle{ m }[/math]. Let [math]\displaystyle{ n }[/math] be the number of distinct elements, namely [math]\displaystyle{ n = |\left\{ {x_1,x_2,\ldots,x_s}\right\}| }[/math], and let these elements be [math]\displaystyle{ \left\{ {e_1,e_2,\ldots,e_n}\right\} }[/math]. Finally, let [math]\displaystyle{ w_j }[/math] be the weight of [math]\displaystyle{ e_j }[/math].
Objective: Find an estimate [math]\displaystyle{ \widehat{w} }[/math] of [math]\displaystyle{ w = \sum_{j=1}^{n}w_j }[/math] using only [math]\displaystyle{ m }[/math] storage units, where [math]\displaystyle{ m \ll n }[/math].

An example of an instance for the weighted problem is: [math]\displaystyle{ a(3),b(4),a(3),c(2),d(3),b(4),d(3) }[/math]. For this instance, [math]\displaystyle{ e_1=a, e_2=b, e_3=c, e_4=d }[/math], the weights are [math]\displaystyle{ w_1=3, w_2=4, w_3=2, w_4=3 }[/math] and [math]\displaystyle{ \sum{w_j}=12 }[/math].

As an application example, [math]\displaystyle{ x_1,x_2,\ldots,x_s }[/math] could be IP packets received by a server. Each packet belongs to one of [math]\displaystyle{ n }[/math] IP flows [math]\displaystyle{ e_1,e_2,\ldots,e_n }[/math]. The weight [math]\displaystyle{ w_j }[/math] can be the load imposed by flow [math]\displaystyle{ e_j }[/math] on the server. Thus, [math]\displaystyle{ \sum_{j=1}^{n}{w_j} }[/math] represents the total load imposed on the server by all the flows to which packets [math]\displaystyle{ x_1,x_2,\ldots,x_s }[/math] belong.

Solving the weighted count-distinct problem

Any extreme order statistics estimator (min/max sketches) for the unweighted problem can be generalized to an estimator for the weighted problem .[12] For example, the weighted estimator proposed by Cohen et al.[5] can be obtained when the continuous max sketches estimator is extended to solve the weighted problem. In particular, the HyperLogLog algorithm[6] can be extended to solve the weighted problem. The extended HyperLogLog algorithm offers the best performance, in terms of statistical accuracy and memory usage, among all the other known algorithms for the weighted problem.

See also

References

  1. Ullman, Jeff; Rajaraman, Anand; Leskovec, Jure. Mining data streams. http://infolab.stanford.edu/~ullman/mmds/ch4.pdf. 
  2. 2.0 2.1 Cosma, Ioana A.; Clifford, Peter (2011). "A statistical analysis of probabilistic counting algorithms". Scandinavian Journal of Statistics. 
  3. Giroire, Frederic; Fusy, Eric (2007). 2007 Proceedings of the Fourth Workshop on Analytic Algorithmics and Combinatorics (ANALCO). pp. 223–231. doi:10.1137/1.9781611972979.9. ISBN 978-1-61197-297-9. 
  4. Chassaing, Philippe; Gerin, Lucas (2006). "Efficient estimation of the cardinality of large data sets". Proceedings of the 4th Colloquium on Mathematics and Computer Science. Bibcode2007math......1347C. 
  5. 5.0 5.1 Cohen, Edith (1997). "Size-estimation framework with applications to transitive closure and reachability". J. Comput. Syst. Sci. 55 (3): 441–453. doi:10.1006/jcss.1997.1534. 
  6. 6.0 6.1 Flajolet, Philippe; Fusy, Eric; Gandouet, Olivier; Meunier, Frederic (2007). "HyperLoglog: the analysis of a near-optimal cardinality estimation algorithm". Analysis of Algorithms. https://hal.archives-ouvertes.fr/docs/00/40/61/66/PDF/FlFuGaMe07.pdf. 
  7. Flajolet, Philippe; Martin, G. Nigel (1985). "Probabilistic counting algorithms for data base applications". J. Comput. Syst. Sci. 31 (2): 182–209. doi:10.1016/0022-0000(85)90041-8. https://hal.inria.fr/inria-00076244/file/RR-0313.pdf. 
  8. Kane, Daniel M.; Nelson, Jelani; Woodruff, David P. (2010). "An Optimal Algorithm for the Distinct Elements Problem". Proceedings of the 29th Annual ACM Symposium on Principles of Database Systems (PODS). https://dash.harvard.edu/bitstream/handle/1/13820438/f0.pdf;sequence=1. 
  9. Cohen, Edith; Kaplan, Haim (2008). "Tighter estimation using bottom k sketches". PVLDB. http://www.vldb.org/pvldb/1/1453884.pdf. 
  10. Metwally, Ahmed; Agrawal, Divyakant; Abbadi, Amr El (2008), Why go logarithmic if we can go linear?: Towards effective distinct counting of search traffic, Proceedings of the 11th international conference on Extending Database Technology: Advances in Database Technology, pp. 618–629 
  11. Knuth, Donald (May 2023). The CVM Algorithm for Estimating Distinct Elements in Streams. 
  12. Cohen, Reuven; Katzir, Liran; Yehezkel, Aviv (2014). "A Unified Scheme for Generalizing Cardinality Estimators to Sum Aggregation". Information Processing Letters 115 (2): 336–342. doi:10.1016/j.ipl.2014.10.009.