Algorithm Search⌘K

Proofs: Bloom filter

These are the human proofs of Bloom filter, at full length.

The false positive rate of a Bloom filter full

This proof shows: false-positive-rate.

Model. Ideal hashing: the values hi(y)h_i(y), over all keys y∈Uy \in U and all i∈{1,…,k}i \in \{1, \ldots, k\}, are independent and uniform on {0,…,m−1}\{0, \ldots, m - 1\}. Insert nn distinct keys SS, and let x∉Sx \notin S. Let XX be the number of positions jj with B[j]=1B[j] = 1, and let ε=Pr⁡[QUERY(x)=true]\varepsilon = \Pr[\text{QUERY}(x) = \text{true}].

Claim 1 (the exact rate). ε=E[(X/m)k]\varepsilon = \mathbb{E}[(X/m)^k].

Proof. By the correctness proof, BB is a function of the knkn values hi(y)h_i(y) for y∈Sy \in S, and QUERY(x)(x) returns true exactly when B[hi(x)]=1B[h_i(x)] = 1 for each ii. The kk values hi(x)h_i(x) are independent of the values for SS, because x∉Sx \notin S, and independent of each other. Given BB, each hi(x)h_i(x) hits a 1 bit with probability X/mX/m, so the conditional probability that all kk hit a 1 bit is (X/m)k(X/m)^k. Taking the expectation over BB gives the claim. □\square

Claim 2 (the mean of XX). E[X]=m(1−(1−1/m)kn)\mathbb{E}[X] = m\left(1 - (1 - 1/m)^{kn}\right).

Proof. Position jj stays 0 exactly when none of the knkn independent values hi(y)h_i(y), y∈Sy \in S, equals jj. Each misses jj with probability 1−1/m1 - 1/m, so position jj is 0 with probability (1−1/m)kn(1 - 1/m)^{kn}. By linearity of expectation over the mm positions, E[m−X]=m(1−1/m)kn\mathbb{E}[m - X] = m(1 - 1/m)^{kn}. □\square

Claim 3 (the classical formula is a lower bound).

ε≥(1−(1−1m)kn)k≥(1−e−kn/m)k.\varepsilon \ge \left(1 - \left(1 - \tfrac1m\right)^{kn}\right)^k \ge \left(1 - e^{-kn/m}\right)^k.

Proof. The function t↦tkt \mapsto t^k is convex on [0,1][0, 1] for k≥1k \ge 1, so by Jensen's inequality E[(X/m)k]≥(E[X]/m)k\mathbb{E}[(X/m)^k] \ge (\mathbb{E}[X]/m)^k. Claim 2 gives the first inequality. The second follows from 1−1/m≤e−1/m1 - 1/m \le e^{-1/m}, which makes 1−(1−1/m)kn≥1−e−kn/m1 - (1 - 1/m)^{kn} \ge 1 - e^{-kn/m}, and from the fact that t↦tkt \mapsto t^k is increasing on [0,1][0, 1]. □\square

Claim 4 (concentration). For each λ>0\lambda > 0, Pr⁡[X≥E[X]+λ]≤e−2λ2/(kn)\Pr[X \ge \mathbb{E}[X] + \lambda] \le e^{-2\lambda^2/(kn)}. Hence, with p=E[X]/mp = \mathbb{E}[X]/m,

ε≤(p+λm)k+e−2λ2/(kn).\varepsilon \le \left(p + \tfrac{\lambda}{m}\right)^k + e^{-2\lambda^2/(kn)}.

Proof. XX is a function of the knkn independent values hi(y)h_i(y). Changing one of them moves at most one 1 bit, so it changes XX by at most 1. The bounded differences inequality (McDiarmid's form of the Azuma inequality) then gives the tail bound. For the second part, split on the event E={X<E[X]+λ}E = \{X < \mathbb{E}[X] + \lambda\}. On EE, (X/m)k≤(p+λ/m)k(X/m)^k \le (p + \lambda/m)^k. Off EE, (X/m)k≤1(X/m)^k \le 1, and Pr⁡[Eˉ]\Pr[\bar E] is at most the tail bound. Claim 1 adds the two parts. □\square

Consequence. Take λ=knln⁡m\lambda = \sqrt{kn \ln m}. Then the second term is m−2m^{-2}, and λ/m=O((log⁡m)/m)\lambda/m = O(\sqrt{(\log m)/m}) when kn=O(m)kn = O(m). So ε=(1−(1−1/m)kn)k+O(k(log⁡m)/m)+O(m−2)\varepsilon = (1 - (1 - 1/m)^{kn})^k + O(k\sqrt{(\log m)/m}) + O(m^{-2}), and for fixed kk and a fixed ratio m/nm/n the rate tends to (1−e−kn/m)k(1 - e^{-kn/m})^k as m→∞m \to \infty, since (1−1/m)kn→e−kn/m(1 - 1/m)^{kn} \to e^{-kn/m}.

Probes of a query. For x∉Sx \notin S, given BB, the rounds of line L6 look at independent uniform positions, and each one is 0 with probability 1−X/m1 - X/m. The loop stops at the first 0 or after kk rounds, so the number of rounds is at most a geometric random variable with mean 1/(1−X/m)1/(1 - X/m) when X<mX < m. ■\blacksquare

A Bloom filter has no false negatives full

This proof shows: correctness, invariant, termination.

Write BtB_t for the bit array after the first tt operations, and StS_t for the set of keys inserted by them. The hash functions h1,…,hkh_1, \ldots, h_k are fixed by line L2 and do not change. Let P(x)={h1(x),…,hk(x)}P(x) = \{h_1(x), \ldots, h_k(x)\} be the set of positions of a key xx.

Lemma 1 (bits only rise). For each tt and each position jj, if Bt[j]=1B_t[j] = 1 then Bt+1[j]=1B_{t+1}[j] = 1.

Proof. A QUERY writes nothing. An INSERT writes only in line L4, and it writes the value 1. So no operation changes a 1 to a 0. □\square

Lemma 2 (invariant (B)). For each tt, each x∈Stx \in S_t and each j∈P(x)j \in P(x), Bt[j]=1B_t[j] = 1.

Proof. By induction on tt. For t=0t = 0, line L1 makes all bits 0, and S0=∅S_0 = \emptyset, so the claim holds with nothing to check. Assume the claim for tt. If operation t+1t + 1 is a QUERY, then St+1=StS_{t+1} = S_t and Bt+1=BtB_{t+1} = B_t, so the claim holds. If it is INSERT(y)(y), then St+1=St∪{y}S_{t+1} = S_t \cup \{y\}. For x∈Stx \in S_t and j∈P(x)j \in P(x) we have Bt[j]=1B_t[j] = 1 by the hypothesis, so Bt+1[j]=1B_{t+1}[j] = 1 by Lemma 1. For x=yx = y, the loop of lines L3 and L4 runs for i=1,…,ki = 1, \ldots, k and sets B[hi(y)]=1B[h_i(y)] = 1 for each ii, so each j∈P(y)j \in P(y) is 1 after the loop. A later round of the same loop writes only the value 1, so it does not undo an earlier round. □\square

Theorem. After any sequence of operations, QUERY(x)(x) returns true for each inserted key xx. If QUERY(x)(x) returns false, then xx was never inserted. QUERY(x)(x) returns true exactly when B[j]=1B[j] = 1 for each j∈P(x)j \in P(x).

Proof. The loop of lines L5 and L6 reads B[hi(x)]B[h_i(x)] for i=1,2,…i = 1, 2, \ldots and returns false at the first ii with B[hi(x)]=0B[h_i(x)] = 0. If there is no such ii, it ends after kk rounds and line L7 returns true. So QUERY returns true exactly when each position of P(x)P(x) holds a 1. If x∈Stx \in S_t, Lemma 2 says that each position of P(x)P(x) holds a 1, so the query returns true. The second claim is the contrapositive of the first. □\square

Union. Let BB and B′B' be the arrays for the sets SS and S′S', built with the same mm and the same hash functions from an all-zero array. By Lemma 1 and line L4, position jj is 1 in BB exactly when j∈⋃x∈SP(x)j \in \bigcup_{x \in S} P(x), because no other position is ever written. So B[j]∨B′[j]=1B[j] \lor B'[j] = 1 exactly when j∈⋃x∈S∪S′P(x)j \in \bigcup_{x \in S \cup S'} P(x), which is the array that inserting S∪S′S \cup S' gives.

Termination. Each loop of lines L3 and L5 has at most kk rounds, and each round does one hash evaluation and one access to the array. ■\blacksquare

Back to Bloom filter