- Source: Count-distinct problem
In computer science, the count-distinct problem
(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
x
1
,
x
2
,
…
,
x
s
{\displaystyle x_{1},x_{2},\ldots ,x_{s}}
with repetitions. Let
n
{\displaystyle n}
denote the number of distinct elements in the stream, with the set of distinct elements represented as
{
e
1
,
e
2
,
…
,
e
n
}
{\displaystyle \{e_{1},e_{2},\ldots ,e_{n}\}}
.
Objective: Find an estimate
n
^
{\displaystyle {\widehat {n}}}
of
n
{\displaystyle n}
using only
m
{\displaystyle m}
storage units, where
m
≪
n
{\displaystyle m\ll n}
.
An example of an instance for the cardinality estimation problem is the stream:
a
,
b
,
a
,
c
,
d
,
b
,
d
{\displaystyle a,b,a,c,d,b,d}
. For this instance,
n
=
|
{
a
,
b
,
c
,
d
}
|
=
4
{\displaystyle n=|\left\{{a,b,c,d}\right\}|=4}
.
Naive solution
The naive solution to the problem is as follows:
Initialize a counter, c, to zero,
c
←
0
{\displaystyle c\leftarrow 0}
.
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
x
i
{\displaystyle x_{i}}
, a membership query is issued.
If
x
i
{\displaystyle x_{i}}
is not a member of D (
x
i
∉
D
{\displaystyle x_{i}\notin D}
)
Add
x
i
{\displaystyle x_{i}}
to D
Increase c by one,
c
←
c
+
1
{\displaystyle c\leftarrow c+1}
Otherwise (
x
i
∈
D
{\displaystyle x_{i}\in D}
) do nothing.
Output
n
=
c
{\displaystyle n=c}
.
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
x
i
{\displaystyle x_{i}}
should be minimized. In such a case, several streaming algorithms have been proposed that use a fixed number of storage units.
HyperLogLog algorithm
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,
n
{\displaystyle n}
.
State-of-the-art estimators hash every element
e
j
{\displaystyle e_{j}}
into a low-dimensional data sketch using a hash function,
h
(
e
j
)
{\displaystyle h(e_{j})}
.
The different techniques can be classified according to the data sketches they store.
= Min/max sketches
=Min/max sketches store only the minimum/maximum hashed values. Examples of known min/max sketch estimators: Chassaing et al. presents max sketch which is the minimum-variance unbiased estimator for the problem. The continuous max sketches estimator is the maximum likelihood estimator. The estimator of choice in practice is the HyperLogLog algorithm.
The intuition behind such estimators is that each sketch carries information about the desired quantity. For example, when every element
e
j
{\displaystyle e_{j}}
is associated with a uniform RV,
h
(
e
j
)
∼
U
(
0
,
1
)
{\displaystyle h(e_{j})\sim U(0,1)}
, the expected minimum value of
h
(
e
1
)
,
h
(
e
2
)
,
…
,
h
(
e
n
)
{\displaystyle h(e_{1}),h(e_{2}),\ldots ,h(e_{n})}
is
1
/
(
n
+
1
)
{\displaystyle 1/(n+1)}
. The hash function guarantees that
h
(
e
j
)
{\displaystyle h(e_{j})}
is identical for all the appearances of
e
j
{\displaystyle e_{j}}
. 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 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.
= Bottom-m sketches
=Bottom-m sketches
are a generalization of min sketches, which maintain the
m
{\displaystyle m}
minimal values, where
m
≥
1
{\displaystyle m\geq 1}
.
See Cosma et al. for a theoretical overview of count-distinct estimation algorithms, and Metwally
for a practical overview with comparative simulation results.
= CVM Algorithm
=Compared to other approximation algorithms for the count-distinct problem the CVM Algorithm (named by Donald Knuth after the initials of Sourav Chakraborty, N. V. Vinodchandran, and Kuldeep S. Meel) uses sampling instead of hashing. The CVM Algorithm provides an unbiased estimator for the number of distinct elements in a stream, in addition to the standard (ε-δ) guarantees. Below is the CVM algorithm, including the slight modification by Donald Knuth.
Initialize
p
←
1
{\displaystyle p\leftarrow 1}
Initialize max buffer size
s
{\displaystyle s}
, where
s
≥
1
{\displaystyle s\geq 1}
Initialize an empty buffer, B
For each element
a
t
{\displaystyle a_{t}}
in data stream
A
{\displaystyle A}
of size
n
{\displaystyle n}
do:
If
(
a
t
,
u
)
,
∀
u
{\displaystyle (a_{t},u),\forall u}
is in B then
Delete
(
a
t
,
u
)
{\displaystyle (a_{t},u)}
from B
u
←
{\displaystyle u\leftarrow }
random number in
[
0
,
1
)
{\displaystyle [0,1)}
If
u
<
p
{\displaystyle u
then
If
|
B
|
<
s
{\displaystyle |B|
then
insert
(
a
t
,
u
)
{\displaystyle (a_{t},u)}
in B
else
(
a
′
,
u
′
)
{\displaystyle (a',u')}
such that
u
′
=
max
{
u
″
:
(
a
″
,
u
″
)
∈
B
,
∀
a
″
}
{\displaystyle u'=\max\{u'':(a'',u'')\in B,\forall a''\}}
/*
(
a
′
,
u
′
)
{\displaystyle (a',u')}
whose
u
′
{\displaystyle u'}
is maximum in B */
If
u
>
u
′
{\displaystyle u>u'}
then
p
←
u
{\displaystyle p\leftarrow u}
else
Replace
(
a
′
,
u
′
)
{\displaystyle (a',u')}
with
(
a
t
,
u
)
{\displaystyle (a_{t},u)}
p
←
u
′
{\displaystyle p\leftarrow u'}
End For
return
|
B
|
/
p
{\displaystyle |B|/p}
.
The previous version of the CVM algorithm is improved with the following modification by Donald Knuth, that adds the while loop to ensure B is reduced.
Initialize
p
←
1
{\displaystyle p\leftarrow 1}
Initialize max buffer size
s
{\displaystyle s}
, where
s
≥
1
{\displaystyle s\geq 1}
Initialize an empty buffer, B
For each element
a
t
{\displaystyle a_{t}}
in data stream
A
{\displaystyle A}
of size
n
{\displaystyle n}
do:
If
a
t
{\displaystyle a_{t}}
is in B then
Delete
a
t
{\displaystyle a_{t}}
from B
u
←
{\displaystyle u\leftarrow }
random number in
[
0
,
1
)
{\displaystyle [0,1)}
If
u
≤
p
{\displaystyle u\leq p}
then
Insert
(
a
t
,
u
)
{\displaystyle (a_{t},u)}
into B
While
|
B
|
=
s
∧
u
<
p
{\displaystyle |B|=s\wedge u
then
Remove every element of
(
a
′
,
u
′
)
{\displaystyle (a',u')}
of B with
u
′
>
p
2
{\displaystyle u'>{\frac {p}{2}}}
p
←
p
2
{\displaystyle p\leftarrow {\frac {p}{2}}}
End While
If
u
<
p
{\displaystyle u
then
Insert
(
a
t
,
u
)
{\displaystyle (a_{t},u)}
into B
End For
return
|
B
|
/
p
{\displaystyle |B|/p}
.
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
x
1
,
x
2
,
…
,
x
s
{\displaystyle x_{1},x_{2},\ldots ,x_{s}}
with repetitions, and an integer
m
{\displaystyle m}
. Let
n
{\displaystyle n}
be the number of distinct elements, namely
n
=
|
{
x
1
,
x
2
,
…
,
x
s
}
|
{\displaystyle n=|\left\{{x_{1},x_{2},\ldots ,x_{s}}\right\}|}
, and let these elements be
{
e
1
,
e
2
,
…
,
e
n
}
{\displaystyle \left\{{e_{1},e_{2},\ldots ,e_{n}}\right\}}
. Finally, let
w
j
{\displaystyle w_{j}}
be the weight of
e
j
{\displaystyle e_{j}}
.
Objective: Find an estimate
w
^
{\displaystyle {\widehat {w}}}
of
w
=
∑
j
=
1
n
w
j
{\displaystyle w=\sum _{j=1}^{n}w_{j}}
using only
m
{\displaystyle m}
storage units, where
m
≪
n
{\displaystyle m\ll n}
.
An example of an instance for the weighted problem is:
a
(
3
)
,
b
(
4
)
,
a
(
3
)
,
c
(
2
)
,
d
(
3
)
,
b
(
4
)
,
d
(
3
)
{\displaystyle a(3),b(4),a(3),c(2),d(3),b(4),d(3)}
. For this instance,
e
1
=
a
,
e
2
=
b
,
e
3
=
c
,
e
4
=
d
{\displaystyle e_{1}=a,e_{2}=b,e_{3}=c,e_{4}=d}
, the weights are
w
1
=
3
,
w
2
=
4
,
w
3
=
2
,
w
4
=
3
{\displaystyle w_{1}=3,w_{2}=4,w_{3}=2,w_{4}=3}
and
∑
w
j
=
12
{\displaystyle \sum {w_{j}}=12}
.
As an application example,
x
1
,
x
2
,
…
,
x
s
{\displaystyle x_{1},x_{2},\ldots ,x_{s}}
could be IP packets received by a server. Each packet belongs to one of
n
{\displaystyle n}
IP flows
e
1
,
e
2
,
…
,
e
n
{\displaystyle e_{1},e_{2},\ldots ,e_{n}}
. The weight
w
j
{\displaystyle w_{j}}
can be the load imposed by flow
e
j
{\displaystyle e_{j}}
on the server. Thus,
∑
j
=
1
n
w
j
{\displaystyle \sum _{j=1}^{n}{w_{j}}}
represents the total load imposed on the server by all the flows to which packets
x
1
,
x
2
,
…
,
x
s
{\displaystyle x_{1},x_{2},\ldots ,x_{s}}
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
.
For example, the weighted estimator proposed by Cohen et al. can be obtained when the continuous max sketches estimator is extended to solve the weighted problem.
In particular, the HyperLogLog algorithm 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
Count–min sketch
Streaming algorithm
Maximum likelihood
Minimum-variance unbiased estimator
References
Kata Kunci Pencarian:
- Daftar julukan kota di Amerika Serikat
- Count-distinct problem
- HyperLogLog
- Flajolet–Martin algorithm
- HLL
- Jelani Nelson
- Aggregate function
- Element distinctness problem
- Knapsack problem
- Eight queens puzzle
- Daniel Kane (mathematician)