1 of 37

CS599 Graph Analytics

Fall 2021

Lectures 14, 15, 17 10/21,10/26, 11/2

Charalampos E. Tsourakakis �ctsourak@bu.edu

2 of 37

Next few lectures agenda

  • Foundations of streaming algorithms (moment estimation, heavy hitters) �
  • Graph streaming algorithms
    • Connectivity
    • Triangles
    • Matchings

3 of 37

The Streaming model

Input

-Stream of elements

- Order may be adversarial

- One pass over the stream

Goals�- Compute as accurately as possible statistics of interest

  • E.g., Number of distinct elements appear in the stream, frequency moments, entropy etc.

- Key constraint: Minimum space algorithm

- Fast query and update times

10 4 1 10 1 9 3 ...

4 of 37

Moment estimation problem

  • Define the frequency of i as ��
  • p=0: distinct elements
  • p=1: length of stream
  • p=2: repeat size/self-join size/Gini coefficient/surprise index in DB
  • p=+inf: We define in this case �

10 4 1 10 1 9 3 ...

5 of 37

Types of streams

A convenient way to think of the stream is as a sequence of updates of the form (i,Δ) to the histogram x[1,..n] of the universe [n].

Common assumptions: (i) Δ integer (ii) log(mM) = log(n) where |Δ|<=M

  • Insert only stream: Δ=1��
  • Strict Turnstile: we may have deletions, but we are guaranteed that all xi values are non-negative throughout the stream
  • General turnstile: anything goes, negative updates and negative x coordinates

Gap!

6 of 37

Morris algorithm

  • Robert Morris has been credited historically with the first �streaming algorithm for estimating the F1 norm of a stream.
    • Cash register model/insertion only�
  • Key idea: instead of maintaining the actual length of the stream�m, keep the logarithm.
    • E.g., if m=145, then by knowing the order of magnitude ~102�we can tell that our number is between 100 and 999

  • This allows us to use loglog(m) bits to represent m approximately

7 of 37

How to save 1 bit?

  • Maintain a counter c (aka Morris counter)
  • init(): c← 0
  • process()�For each item in the stream
    • Increase c with probability 1/2
    • o/w keep same value
  • Output estimate 2c

Let Z be the value of the counter after m increments.

  • Z~Bin(m,½)
  • E[Z]=m/2
  • Var[Z] =m/4
  • Space complexity: lg(m/2)=lg(m)-1 → we saved one bit at the cost of accuracy.

8 of 37

How to save k bits?

  • Maintain a counter c (aka Morris counter)
  • init(): c← 0
  • process()�For each item in the stream
    • Increase c with probability 1/2k
    • o/w keep same value
  • Output estimate 2kc

Let Z be the value of the counter after m increments.

  • Z~Bin(m,2-k)
  • E[Z]=m/2k
  • Var[Z] = m/2k (1-1/2k)~m/2k
  • Space complexity: lg(m/2k)=lg(m)-k → we saved k bits

9 of 37

How to save k bits?

  • Another perspective as a birth process �
  • Counter values follow a binomial distribution:

0

1

2

X

p

p

p

p

1-p

1-p

1-p

10 of 37

Morris algorithm → birth process with adaptive sampling

  • Maintain a log-counter c (aka Morris counter)
  • init(): c← 0
  • process()�For each item in the stream
    • Increase c with probability 1/2c
    • o/w keep same value
  • Output estimate 2c-1

0

1

2

X

11 of 37

Why this estimator?

Claim: Define Xn to be the value of the counter after n increments. Then, E[2Xn]=n+1.

Proof (induction)

Base case: If n=0, Xn=0 and thus the claim holds.

Inductive step: By conditional expectation rule E[2Xn+1]=E[E[2Xn+1|Xn]] and the inductive hypothesis, we obtain the following expression:

12 of 37

Properties of Morris algorithm

  1. The expectation of the variable Z=2Xm satisfies the following:

E[Z]=m+1

Corollary: Morris algorithm outputs an unbiased estimator of m. �

  1. The variance of Z is equal to Var[Z]=m(m-1)/2

Observation: No improvement in terms of concentration as m grows since Var(Z)/E(Z)2 is constant.

13 of 37

Morris algorithm

14 of 37

Morris+

  • Suppose we run s Morris counters and we output the average
    • The average of this estimator remains the same but..
    • the variance scales down by a factor of 1/s

s

15 of 37

Reducing variance: Morris++

Claim: The space complexity with probability 1-δ is and we obtain an (ε,δ)-approximation scheme to F1 for insert-only streams. ���

avg1

avg2

avgt

s

Morris++: Median of t Moris+’s where

Moris+: avg of s independent �Morris counters

Moris+: avg of s independent �Morris counters

Moris+: avg of s independent �Morris counters

16 of 37

Reducing variance: change basis

  • The expectation of the variable Z=((1+a)Xm-1)/a satisfies the following:

E[Z]=m

  • The variance of Z is equal to Var[Z]=a m(m-1)/2

X

1/2X

1 - 1/2X

X

1

0

X

1/(1+a)X

1 - 1/(1+a)X

vs.

deterministic

Morris2

Morris1+a

17 of 37

How to set a?

  • Set a=2ε2δ and apply Chebyshev’s inequality ����
  • Space complexity: ��

X

1/(1+a)X

1 - 1/(1+a)X

Morris1+a

18 of 37

Optimal Algorithm for F1

  • Nelson and Yu proved recently that Morris algorithm�Is optimal by
    • Tightening the analysis of the space complexity�����for an (ε,δ)-approximation scheme to F1.�
    • Proving a tight lower bound, and thus practically nailing down the problem.

19 of 37

Merging Morris Counters

  • Imagine that we have distributed our computation across k machines.
  • We have two Morris counter values, c1 and c2. What should our estimate be?
  • Suppose we have the same base b=(1+a) for 1>=a>0.

Merge(c1,c2)

  • a=min(c1,c2), β=max(c1,c2)
  • For j← 0 to α-1
    • if U[0,1]<bj-β then β← β+1

Intuition: simulating updates in the second counter by observing that in order to have an increase from x to x+1 we need bx updates.

Stream 1

Stream 2

c1

c2

20 of 37

Flajolet-Martin (FM) sketch

  • Key assumption: We have an idealized hash function that maps each element of the universe into a string of random bits (i.e., Pr(bit=0)=Pr(bit=1)=½) ����

  • Intuition: If we see the prefix 11110xx, probably we have seen more than 32 distinct items.�
  • Idea: Keep track of prefixes of the form 1k0

1

1

1

1

0

x

x

21 of 37

Flajolet-Martin (FM) sketch

  • FM estimator: Clearly, 2R where R is the largest size of such a prefix approximates the number of distinct elements. However, it turns out there is some small bias: ��
  • Flajolet and Martin proved the following remarkable result: ���� where ν(p)=#bits equal to 1 in binary representation of p�
  • Thus, an unbiased (modulo negligible terms) estimator becomes �
  • Fascinating analysis of an algorithm

22 of 37

Flajolet-Martin (FM) sketch

Important functions �1. h(x) = hash function that transforms x into a uniform binary string

2. ρ(x) = position of leftmost 0 (e.g., ρ(111010100000)=4)

�Algorithm

  • Initialize a bitmask with L bits to 0, i.e., BITMASK �
  • For each element x in the stream
    • BITMASK[ρ(h(x))] ← 1 �
  • Set R← ρ(BITMASK)�
  • Output

0

0

….

0

23 of 37

Flajolet-Martin (FM) sketch

  • Is the constant 1/φ unexpected in the estimator?
    • Intuition: since if we have a prefix 1111...10 there are likely to be more than 2R and less than 2R+1 elements. �
  • Variance of the estimator is 1.257, which in practice means it can be off by a factor of 2. �
  • Question: how do we reduce variance?
    • Idea 1: use multiple hash functions
    • Idea 2: generalize FM counters from binary strings to (d+1)-ary strings, where we count up to a certain threshold d how many hashes hit a bit of the bitmask.
    • Idea 3: combine the above

24 of 37

Stochastic Averaging

  • We improve the accuracy of the algorithm by a multiplicative factor of by taking the average of k different hash function ⇒ expensive �
  • Better idea: use the first bits to create substreams! ��Split the elements in k=2l substreams by using the first l bits of the hash. E.g, for l=2�h(v) = b1b2 b3b4b5...

Substream id hash value

25 of 37

Flajolet-Martin theorem

The estimator Z is asymptotically unbiased, i.e., En[Z] → n and the coefficient �

of variation using k bitmasks is Memory: O(klogn)

Example: can count cardinalities up to 109 with error <=6% using 4kBs of memory (=4096 bytes)**

“Caveat” of their work: Practical implementations of the hash function are not discussed.

* See also Mitzenmacher-Vadhan: Why simple hash functions work: Exploiting the entropy in a data stream

26 of 37

K Minimum Values (kMV) sketch

Basic idea: Use a “good enough” hash function h:[n] → R and the k smallest hashed values.

KMV-Init(k)

  • Pick hash function h
  • Initialize L=[] for k ( item, hash(item) ) pairs

KMV-Update(x)�- If x is not in L

L← L U {(x,h(x))}� if |L|>k then remove x with largest h(x) value from L

KMV-Query()

v← largest hash value in L�Return (k-1) R/v

27 of 37

KMV analysis

Let A be the set of distinct elements.

  • What is the probability that our estimator will exceed (1+ε)|Α|?

���

�Let’s assume h is chosen from a family of pairwise independent hash functions, and that R is large enough to ensure hash collisions are unlikely to occur (e.g., R=n3)

R

k=3

28 of 37

AMS sketch for Fp

Theorem: For every ε,δ>0, there exists a randomized algorithm A that computes a number Y so that the probability that Y deviates from Fp by more than εFp is at most δ. ��Furthermore, algorithm A uses memory bits.

Lower bound: For any fixed p>5, δ<½, any randomized algorithm that outputs a number � �Zp such that

29 of 37

AMS sketch for Fk

  • Suppose the length of the stream m=F1 is known a priori.��Algorithm�
  • Choose a random position p in the stream member, and let xp=v. �
  • Let �
  • Output �

Remarks�In order to get an (ε,δ)-approximation scheme we set�and we take the median of s2 variables each being the average of s1 independent Xs.

30 of 37

Reservoir sampling

  • If we do not know m a priori, we use standard reservoir sampling. �
  • When the element xj arrives we update our sample with probability 1/j.�
    • In case the replacement takes place, we reset r=1.�
    • If we do not replace our sample, then if xj is equal to the sample value v, we increase the value of r by 1.

31 of 37

Improved AMS sketch for F2

  • The previous theorem yields that we can get an estimate for F2 using�memory bits. �
  • For F2, a logarithmic number of bits suffices with the following different algorithm:�Algorithm
  • Let h:[n] → {-1,+1} be chosen from a 4-wise independent hash family H.
  • z← 0
  • For each element x in the stream
    1. z← z+h(x)
  • Output z2�
  • Space Usage:

32 of 37

AMS sketch

  • Flajolet and Martin assumed the existence of a family of hash functions that exhibit ideal properties. ��
  • Alon, Matias, Szegedy provided a slight modification of the FM sketch that uses 2-wise independent hash functions �
  • Construction of 2-wise independent hash function
    • F=GF(2d) where 2d>n
    • Choose a,b uar and independently from F
    • h(x)=ax+b where product and addition are computed in the field F
      • E.g., h(x) = ax+b mod p where p is prime��

33 of 37

AMS Sketch for F0

  • For each element x in the stream
    • Compute r(h(x))=largest value r such that the rightmost bits of h(x) are 0.
    • R=max(R,r(h(x))
  • Output Y=2R

Due to the random choice of a,b

  1. h(x) is uniformly distributed over F. Therefore, �
  2. The mapping is pairwise independent

In other words, for x,y distinct

34 of 37

AMS Sketch for F0

35 of 37

AMS Sketch for F0

Theorem: For every c>2 there exists an algorithm that outputs an estimate Y of F0 such that the probability that the ratio between Y and F0 is not between 1/c and c is at most 2/c.

Space usage: O(logn) to store the hash functions, O(loglogn) to store r.

Median trick: the failure probability can become δ using the standard median trick, i.e., repeat the process log(1/δ) times and output the median. This yields a (O(1), δ) approximation scheme. ��Remark: The intuition is the same as FM, namely on average one out of F0 distinct numbers will satisfy ρ(h(x))>=log2(F0), so yields a good approximation of log2(F0).�

36 of 37

Triangle counting via moment estimation

  • Define Ti to be the number of triples with i edges between them.��
  • Let’s compute the moments of the virtual stream. Clearly: �

Stream update:(u,v) Virtual stream: (u,v,x1),...,(u,v,xn-2)

37 of 37

Triangle counting via moment estimation

  • The number of distinct elements in the virtual stream is �
  • The second moment F2 is given by the following equation������
  • Idea: Thus, we keep two sketches, one for estimating F0 and one for estimation F2 and at the end of the stream we solve a linear system to compute T3. �
  • Space: �