Lecture 4.5 - Bloom filter
Bloom Filters
Introduction
- Approximate set membership problem
- Trade-off between the space and the false positive probability
- Generalizes the hashing ideas
Analogy Think of a Bloom filter like a bouncer with a fuzzy memory of a guest list — they might occasionally let in someone who isn't on the list (false positive), but they will
never turn away someone who actually is on the list.
Approximate Set Membership Problem
- Suppose we have a set (universe)
- Represent in such a way that we can quickly answer "Is an element of ?"
- To take as little space as possible, we allow false positives (i.e. , but we answer yes)
- If , we must answer yes (no false negatives allowed)
Bloom Filters — Structure
Related
- Consist of an array of bits (space), and independent random hash functions:
Steps:
- Initially set the array to 0
- For all , set for
- An entry can be set to 1 multiple times — only the first time has an effect
- To check if , check whether all locations for are set to 1
- If not all are 1 → for sure
- If all are 1 → we assume (this may yield a false positive)
[Image] — diagram showing:
- Initial array all set to 0
- Elements each hashed times, setting bit locations to 1
- Checking element : if a 0 appears among its hash locations →
- If only 1s appear among its hash locations → conclude (possible false positive)
Analogy It's like flipping switches on a wall of light bulbs whenever a guest arrives. To check if someone came, you look at "their" bulbs — if even one is off, they definitely didn't come. But if all their bulbs are on, it might just be because
other guests happened to flip the same switches.
Example — Finding IP Address in a Log File
We hash each IP with 3 hash functions:
- 192.168.1.10: → set bits [3, 12, 17]
- 10.0.10.5: → set bits [7, 12, 19]
- 172.16.0.22: → set bits [5, 9, 14]
Bloom Filter bit array after insertion:
| Index | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Bits | 0 | 0 | 0 | 1 | 0 | 1 | 0 | 1 | 0 | 1 | 0 | 0 | 1 | 0 | 1 | 0 | 0 | 1 | 0 | 1 |
- Notice bit 12 is shared between IP 192.168.1.10 and 10.0.10.5 — this is exactly the kind of collision that causes false positives.
Why Multiple Hashes?
- Using only one hash causes many collisions, resulting in a high false-positive rate
- Multiple hashes give each item a more distinctive combination of bit positions
- Using too many hashes → every insertion sets many bits → the filter fills up quickly (also increases false-positive rate)
- The optimal number of hash functions is approximately:
where:
- = the Bloom filter size (number of bits)
- = the expected number of inserted elements
Analogy Too few hash functions = the bouncer only checks one ID feature (easy to fake). Too many hash functions = the bouncer checks so many features that the guest list fills up the entire notebook fast. There's a sweet spot for how many checks give the best accuracy for the space you have.
The Probability of a False Positive
- We assume the hash functions are random
- After all the elements of are hashed into the bloom filter, the probability that a specific bit is still 0 is:
Analogy Each hash insertion is like randomly throwing a dart at
balloons. After many darts (insertions × hash functions), this formula tells you the chance a specific balloon is still un-popped (still 0).
Exercise
- If you want, say, 100 entries with a 1% false-positive rate, you need approximately:
Use the optimal formulas (derived from the false-positive probability, not shown explicitly in the slides but standard results):
where and .
- If we want , we cannot assume that, becuase infinity array size.
- You need to have unlimited array size.
Reference
- From Probability and Computing: Randomized Algorithms and Probabilistic Analysis, p.109–111
- By Michael Mitzenmacher, Eli Upfal