Docsity

Streaming Algorithms for Distinct Element Counting: Lecture 2 by Ashwin Lall, Study notes of Computer Science

Partial preview of the text

Download Streaming Algorithms for Distinct Element Counting: Lecture 2 by Ashwin Lall and more Study notes Computer Science in PDF only on Docsity!

Lecture 2: Streaming Algorithms for Counting

Distinct Elements

Ashwin Lall

20th August, 2008

Streaming Algorithms

Notation

A stream is an ordered tuple over the alphabet

{ 1 , 2 , 3 ,... , n }.

source or destination address ( n = 232 ) source or destination port ( n = 64K) or any combination of flow header fields ( n ≈ 2100 ) Frequency of i th item is mi , and the length of the stream is

m =

∑^ n

i = 1

mi.

The stream is represented by ( a 1 , a 2 , a 3 ,... , am ), where each at ∈ { 1 , 2 , 3 ,... , n }.

Some Sample Streaming Problems

Estimating frequency moments (e.g., F 2 is a measure of the “skewness”)

Fk =

∑^ n

i = 1

mki

Estimating entropy (Useful metric for anomaly detection)

H = −

∑^ n

i = 1

mi m

log

( (^) m i m

Counting the number of distinct elements:

D = ||{ i | mi 6 = 0 }||

Motivation

What does it mean when (^1) the number of destination ports spikes? Port scan

(^2) the number of source addresses spikes? Flash crowd

(^3) the number of source addresses to a fixed destination address spikes? DDoS attack

First-attempt Solutions

Maintain a record for every distinct flow

  • Needs O ( n ) memory or at least O ( m )

Sample (e.g., 1 in 100 packets) - Cisco’s NetFlow

  • Hohn and Veitch show that sampling can give inaccurate results

Use a lossy set maintenance data structure (Bloom filter)

  • Still requires O ( m ) space

Brief Proof

Consider the probability of a position in the bitmap being zero at the end: ( 1 − 1 / B ) D^ ≈ e − D / B , where D is the number of distinct items. The expected number of zeroes is B times this (since the bitmap is of size B ), so we have: B − c ≈ B ∗ e − D / B.

Solving for D , we get D = B ln

B B − c

This turns out to be a maximum likelihood estimator for the number of distinct elements.

Bitmap Example

Estimated number of distinct elements = 4 ∗ ln( 4 / 1 ) = 5. 55 ≈ 6

The Coupon Collector Problem - Solution

We look at the number of coupons that he must pick to get the i th distinct coupon: (^1) To get the first coupon, he only needs one try. (^2) To get the second distinct coupon (any of C − 1 of C ) he needs expected (^) CC − 1 tries. · · · (^3) To get the i th distinct coupon (any of C − i + 1 of C ) he needs expected (^) C − Ci + 1 tries. Hence, expected number of tries is 1 + (^) CC − 1 + (^) CC − 2 +... C 1 = C ∗ ( 1 + 1 / 2 + 1 / 3 +.. .+ 1 / C ) ≈ C ln C.

Multi-resolution Bitmaps

A virtual bitmap is one where the flows are first sampled before being entered into the bitmap. This cuts down the amount of memory by about the sampling factor.

However, from the Coupon Collector’s problem, we know that B ln B distinct flows will (in expectation) fill the bitmap entirely with 1’s.

If we are unsure how large the number of distinct items can get beforehand, we can use bitmaps of various sizes and approximate the value from the one with the most appropriate size.

Min-hash Example

Number of distinct elements ≈ 16 / 3 − 1 = 4. 33