Sub topic 3
Choosing Hash Functions: Pairwise Independence
Assume basic Python, hash tables, probability, and Big-O notation. A count-min sketch needs one hash function per row. A good family of hash functions makes collisions predictable rather than adversarial.
A pairwise-independent family means that, for any two different items, their hash locations behave nearly like independent random choices. A common construction is:
def column(x, a, b, p, width):
return ((a * x + b) % p) % widthChoose different random a and b values for each row. Store these parameters with the sketch. Do not use Python's built-in hash() when sketches must communicate across processes, because its results can vary between processes.
Expected Collisions and Markov's Inequality
For a queried item x, each row contains its true count plus counts from other items that collide with x. Counters never decrease in the insertion-only model, so the row estimate is never below the true count.
Let X be the extra count from collisions in one row. Pairwise independence gives:
E[X] <= N / w, whereNis the total stream count.- Markov's inequality gives
P(X > εN) <= 1 / (εw). - Choosing
w = ceil(e / ε)makes this probability at most1/e. - With
d = ceil(ln(1/δ))independent rows, the probability that every row is bad is at mostδ.
Therefore, with probability at least 1 - δ:
true_count(x) <= estimate(x) <= true_count(x) + εN
> callout note > The guarantee depends on non-negative updates and suitable hash independence. It is probabilistic, not a promise that every possible hash choice works.
Heavy Hitters and Conservative Updates
A sketch cannot discover item names by itself. Keep a small heap of candidate items alongside it. For each arriving item, update the sketch, query its estimate, and retain the largest estimated candidates. The heap gives approximate heavy hitters while the sketch stores their frequencies compactly.
A conservative update reduces collision error. First find the row minimum, then raise only counters below the new target. This preserves the no-undercount property for non-negative counts and usually reduces inflated counters.
Merging and Deletions
Sketches from separate machines or time windows can be merged by adding corresponding counters. They must have identical width, depth, and hash parameters.
Deletions create the turnstile model, where updates may be negative. The simple minimum estimator can then underestimate because negative updates may cancel collisions. For frequent additions and removals, consider Count Sketch or another signed-frequency structure.
Choosing the Tool
Use a count-min sketch when memory is fixed, updates are mostly non-negative, and one-sided approximate counts are useful. Use exact counting when the distinct-item set fits in memory or exact answers are required. Use Count Sketch when signed updates or lower-error behavior around cancellations matter.
Exercise
Why must two sketches use the same hash parameters before they are merged?
Show answer
Corresponding cells represent the same item buckets only when their dimensions and hash functions match.