Sub topic 2
Hashing and collisions refresher
This chapter assumes you know basic Python and have seen hash tables. It reviews the two ideas the sketch depends on: mapping items to a small range of slots, and what happens when two items share a slot.
From item to column
A hash function turns any item, such as a string, into a number. A count-min sketch needs a number between 0 and w-1, where w is the number of columns. The usual way to get one is the remainder operator:
column = hash_value % wThe same item must always produce the same column. Otherwise a count added now could not be found later.
Use a stable hash
Python's built-in hash() for strings changes between runs, because Python randomizes it for security. That is fine for a dictionary inside one process. It breaks any sketch that is saved to disk or shared between machines. For those cases, use a hash that is the same everywhere:
import hashlib
def stable_hash(item, seed=0):
data = f"{seed}:{item}".encode()
return int.from_bytes(hashlib.blake2b(data, digest_size=8).digest(), "big")
w = 10
words = ["apple", "banana", "cherry", "date", "elderberry", "fig", "grape"]
buckets = {}
for word in words:
col = stable_hash(word) % w
buckets.setdefault(col, []).append(word)
for col in sorted(buckets):
print(col, buckets[col])The seed argument gives you a different hash function for each value. The sketch will use this idea: one hash function per row.
Collisions are unavoidable
A collision happens when two different items map to the same column. When the stream has more distinct items than w, collisions are guaranteed. That is the pigeonhole principle. Even with few items they are common. Seven words in ten columns avoid all collisions with probability only 10·9·8·7·6·5·4 / 10⁷ ≈ 0.06. So about 94% of the time, at least one pair collides.
A hash table handles collisions by storing the colliding items in a list or probing for another slot. A sketch does neither. It keeps one counter per column, so colliding items share the counter and their counts add together.
How likely is a collision?
A good hash function spreads items roughly evenly. For a fixed item x and another item y, the chance that y lands in x's column is about 1/w. If n other items arrive, the expected number landing with x is about n/w.
This number is the key to the sketch's error. If every arrival of an item counts, the extra count in x's column is the total number of arrivals of colliding items, which on average is about N/w for a stream of length N. Making w larger shrinks that extra count. The chapter on the (ε, δ) guarantee makes this precise.
Why one row is not enough
With a single hash function, an unlucky collision between a rare item and a very frequent one makes the rare item look frequent. A single row cannot tell. If you hash the same item with several independent functions, the item collides with different neighbors in each row. It is unlikely that every row has a bad collision. The sketch exploits this by keeping several rows and combining them.
Summary of what you need
- A hash maps an item to a column with
hash_value % w. - The mapping must be the same every time, across processes.
- Collisions happen, and the expected number of other items in your column is about n/w.
- Different seeds give different hash functions, so collisions differ from row to row.
Exercise
A sketch has w = 100 columns and has seen n = 5,000 other items, each landing in a uniformly random column. About how many of those items are expected to share a column with one particular item?
Show answer
Each other item lands in that column with probability 1/100, so the expected number is 5,000 / 100 = 50.