Lecture 4.5 - Bloom filter

Updated 14 Oct 2026

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 S=s1,s2,...,sm⊆US = {s_1, s_2, ..., s_m} \subseteq U (universe)
  • Represent SS in such a way that we can quickly answer "Is xx an element of SS?"
  • To take as little space as possible, we allow false positives (i.e. x∉Sx \notin S, but we answer yes)
  • If x∈Sx \in S, we must answer yes (no false negatives allowed)

Bloom Filters — Structure

  • Consist of an array A[n]A[n] of nn bits (space), and kk independent random hash functions:
    h1,...,hk:U→0,1,...,n−1h_1, ..., h_k : U \rightarrow {0, 1, ..., n-1}

Steps:

  1. Initially set the array to 0
  2. For all s∈Ss \in S, set A[hi(s)]=1A[h_i(s)] = 1 for 1≤i≤k1 \le i \le k
    • An entry can be set to 1 multiple times — only the first time has an effect
  3. To check if x∈Sx \in S, check whether all locations A[hi(x)]A[h_i(x)] for 1≤i≤k1 \le i \le k are set to 1
    • If not all are 1 → x∉Sx \notin S for sure
    • If all are 1 → we assume x∈Sx \in S (this may yield a false positive)

[Image] — diagram showing:

  • Initial array all set to 0
  • Elements x1,x2x_1, x_2 each hashed kk times, setting bit locations to 1
  • Checking element yy: if a 0 appears among its hash locations → y∉Sy \notin S
  • If only 1s appear among its hash locations → conclude y∈Sy \in S (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: h1=3,h2=12,h3=17h_1=3, h_2=12, h_3=17 → set bits [3, 12, 17]
  • 10.0.10.5: h1=7,h2=12,h3=19h_1=7, h_2=12, h_3=19 → set bits [7, 12, 19]
  • 172.16.0.22: h1=5,h2=9,h3=14h_1=5, h_2=9, h_3=14 → set bits [5, 9, 14]

Bloom Filter bit array after insertion:

Index012345678910111213141516171819
Bits00010101010010100101
  • 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:

kopt=mnln⁡2\boxed{k_{\text{opt}} = \frac{m}{n} \ln 2}

where:

  • mm = the Bloom filter size (number of bits)
  • nn = 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 SS are hashed into the bloom filter, the probability that a specific bit is still 0 is:

p=(1−1n)km≈e−km/n\boxed{p = \left(1 - \frac{1}{n}\right)^{km} \approx e^{-km/n}}

Analogy Each hash insertion is like randomly throwing a dart at

nn 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:

m=?k=?m = ? \quad k = ?

Use the optimal formulas (derived from the false-positive probability, not shown explicitly in the slides but standard results):

m=−nln⁡p(ln⁡2)2,k=mnln⁡2m = -\frac{n \ln p}{(\ln 2)^2}, \quad k = \frac{m}{n}\ln 2 where n=100n = 100 and p=0.01p = 0.01.

  • If we want p=0%p=0\%, we cannot assume that, becuase infinity array size. ln⁡(0)\ln(0)
    • 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