TExcelTAD
Home › Streaming, Sketching & Sublinear Algorithms › Streaming, Sketching and Sublinear Algorithms: What This Course Covers

Streaming, Sketching and Sublinear Algorithms: What This Course Covers

Classical algorithm analysis assumes the input sits in memory and the algorithm may look at it as often as it likes. Many modern inputs do not fit that picture. A network router sees billions of packets a day and keeps a few megabytes of state; a search engine's query log is read once, in order, by a job that must report the most frequent queries; a graph with a billion edges arrives as a list of edge insertions and deletions. In these settings the scarce resource is memory, and often the algorithm gets one pass over the data. A second family of problems goes further: the input is in memory but so large that even reading it all is too slow, and the algorithm may only query a few positions. Both families are sublinear: sublinear in space for streaming, sublinear in time for property testing.

This course, a Part II course of the track, develops both. It builds directly on Count-Min Sketch and HyperLogLog and Bloom Filters, which introduced two sketches as data structures, and on the probability tools of Randomized Algorithms: Chernoff bounds, Chebyshev's inequality and k-wise independent hashing. Here the emphasis is on the analysis: exact error guarantees with their conditions, the variants that improve them, and the lower bounds that show where no algorithm can go.

The topic order follows the standard references on the subject: S. Muthukrishnan's survey Data Streams: Algorithms and Applications (2005), Cormode and Yi's Small Summaries for Big Data (2020) and Goldreich's lecture notes and book on property testing (2017). Every explanation, program and exercise here is written from scratch, and results are cited by author and year so that you can find the original papers.

A taste in one pass

One pass over 60 000 page views, three small summaries, and the exact answers for comparison. The summaries are Misra-Gries with 9 counters (frequent items), a reservoir sample of 5 views (a uniform sample without knowing the stream's length) and k minimum values, which estimates the number of distinct pages from the 256 smallest hash values.

python · try it yourself
import heapq, random
from collections import Counter

rng = random.Random(13)
U = 20000
cum, s = [], 0.0
for i in range(U):
    s += 1 / (i + 1) ** 1.3
    cum.append(s)
stream = rng.choices(range(U), cum_weights=cum, k=60000)   # Zipf-like page ids

# 1. Misra-Gries with 9 counters: every page above m/10 views survives
mg = {}
for x in stream:
    if x in mg:
        mg[x] += 1
    elif len(mg) < 9:
        mg[x] = 1
    else:
        for y in list(mg):
            mg[y] -= 1
            if mg[y] == 0:
                del mg[y]

# 2. Reservoir sample of 5
res = []
for t, x in enumerate(stream):
    if t < 5:
        res.append(x)
    else:
        j = rng.randrange(t + 1)
        if j < 5:
            res[j] = x

# 3. k minimum values: distinct count from the 256 smallest hash values
MASK = (1 << 64) - 1
def mix64(x):
    x = (x + 0x9E3779B97F4A7C15) & MASK
    x = ((x ^ (x >> 30)) * 0xBF58476D1CE4E5B9) & MASK
    x = ((x ^ (x >> 27)) * 0x94D049BB133111EB) & MASK
    return x ^ (x >> 31)
k, heap, inheap = 256, [], set()
for x in stream:
    h = mix64(x)
    if h in inheap:
        continue
    if len(heap) < k:
        heapq.heappush(heap, -h)
        inheap.add(h)
    elif h < -heap[0]:
        inheap.discard(-heapq.heappushpop(heap, -h))
        inheap.add(h)
kmv = (k - 1) / (-heap[0] / 2 ** 64)

true = Counter(stream)
print(f"stream: {len(stream)} items; exact answers need a table of {len(true)} entries")
print("Misra-Gries candidates:", ", ".join(f"page {x}: >= {c} (true {true[x]})" for x, c in sorted(mg.items())))
print("reservoir sample of 5:", sorted(res))
print(f"distinct pages: exact {len(true)}, KMV estimate from {k} hashes {kmv:.0f} ({kmv / len(true) - 1:+.1%})")
stream: 60000 items; exact answers need a table of 4084 entries
Misra-Gries candidates: page 0: >= 11307 (true 15987), page 1: >= 1893 (true 6572)
reservoir sample of 5: [0, 1, 3, 4, 108]
distinct pages: exact 4084, KMV estimate from 256 hashes 4337 (+6.2%)

The three summaries together hold fewer than 300 numbers, against a table of several thousand entries for the exact answers, and the gap grows without bound: their size does not depend on the stream at all. What they give up is exactness. Misra-Gries undercounts by at most m/10m/10 for a stream of length mm, the reservoir is a random sample rather than a statistic, and the distinct-count estimate is off by a few per cent. Each lesson of the course makes such a guarantee precise and proves it, or proves that nothing better is possible.

What the course covers

Section Topics Main results
Stream models insertion-only, cash register and turnstile streams; space in bits and words; deterministic vs randomized; approximate counting; reservoir and weighted sampling exact counting of distinct items needs Ω(n)\Omega(n) bits; Morris counting in O(log⁡log⁡m)O(\log \log m) bits; reservoir sampling is exactly uniform
Heavy hitters Misra-Gries and its merge, Space-Saving, count-min, Count-Sketch, turnstile heavy hitters additive error m/km/k with k−1k - 1 counters, deterministic; ε∥f∥1\varepsilon \lVert f \rVert_1 and ε∥f∥2\varepsilon \lVert f \rVert_2 with probability 1−δ1 - \delta
Frequency moments and cardinality FkF_k, the AMS sketch, Flajolet-Martin and KMV, HyperLogLog, communication lower bounds (1±ε)F2(1 \pm \varepsilon) F_2 in O(ε−2log⁡δ−1)O(\varepsilon^{-2} \log \delta^{-1}) words; 1.04/m1.04/\sqrt{m} for HyperLogLog; Ω(n)\Omega(n) for exact F0F_0 and for F∞F_\infty
Graph streams semi-streaming, union-find, ℓ0\ell_0-sampling, the AGM sketch, cut and spectral sparsifiers connectivity in O(nlog⁡n)O(n \log n) words even with deletions (via O(npolylog⁡n)O(n \operatorname{polylog} n) bits of sketches); (1±ε)(1 \pm \varepsilon) cuts with O(nlog⁡n/ε2)O(n \log n / \varepsilon^2) edges
Property testing query complexity, ε\varepsilon-far, sortedness, monotonicity, linearity (BLR), bipartiteness, sublinear estimation O(log⁡n/ε)O(\log n / \varepsilon) for sortedness; O(1/ε)O(1/\varepsilon) for linearity; counting components in time independent of nn

The Exercises collect problems from every section, and the Mini Project builds a one-pass log analyser that counts distinct users with HyperLogLog, finds top pages with Misra-Gries and Space-Saving and keeps a reservoir sample, and compares every answer with the exact one.

What you should know first

Conventions

  • A stream is a1,a2,…,ama_1, a_2, \ldots, a_m of length mm over the universe [n]={1,…,n}[n] = \{1, \ldots, n\} (programs use 0,…,n−10, \ldots, n - 1). Its frequency vector is f∈Znf \in \mathbb{Z}^n, with fif_i the number of occurrences of ii (or, in turnstile streams, the sum of its updates).
  • Norms: ∥f∥1=∑i∣fi∣\lVert f \rVert_1 = \sum_i \lvert f_i \rvert, ∥f∥2=(∑ifi2)1/2\lVert f \rVert_2 = (\sum_i f_i^2)^{1/2}. Moments: Fk=∑ifikF_k = \sum_i f_i^k, so F0F_0 is the number of distinct items (with 00=00^0 = 0), F1=mF_1 = m in insertion-only streams and F2=∥f∥22F_2 = \lVert f \rVert_2^2.
  • Space is counted in words of O(log⁡n+log⁡m)O(\log n + \log m) bits unless a bound says bits; the difference matters for lower bounds and for HyperLogLog. O~(⋅)\tilde{O}(\cdot) hides polylogarithmic factors.
  • An (ε,δ)(\varepsilon, \delta)-approximation of a quantity QQ returns Q^\hat{Q} with Pr⁡[∣Q^−Q∣≤εQ]≥1−δ\Pr[\lvert \hat{Q} - Q \rvert \le \varepsilon Q] \ge 1 - \delta. Additive guarantees, such as ε∥f∥1\varepsilon \lVert f \rVert_1, are stated as such.
  • Every random choice in a program comes from a generator with a fixed seed, so the printed numbers are exactly what you will see. Hash functions are either ((ax+b) mod p) mod w((a x + b) \bmod p) \bmod w with p=261−1p = 2^{61} - 1 (pairwise independent) or the splitmix64 mixer mix64 (a fixed, well-mixing function used where the analysis assumes an ideal hash).
  • Pseudocode is in the track's style (How to read pseudocode).

Test yourself

What is the scarce resource in the data stream model?

  1. Running time per item only
  2. Memory: the algorithm sees the stream once, in order, and keeps space far smaller than the stream or the universe
  3. The number of processors
  4. Disk bandwidth

What distinguishes a property tester from a streaming algorithm?

  1. Testers are deterministic
  2. A tester has random access to the input and reads only a small number of positions; a streaming algorithm reads everything once with little memory
  3. Testers need more memory
  4. There is no difference

For a stream over the universe [n][n], what does the moment F0F_0 count?

  1. The stream length
  2. The number of distinct items, those with fi≠0f_i \ne 0
  3. The most frequent item's count
  4. The sum of squared frequencies