Algorithm Search⌘K

Proofs: Counting Bloom filter

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

Counter overflow in a counting Bloom filter full

This proof shows: complexity.

Setting. Let nn distinct keys be stored once each. Assume ideal hashing: the knkn values hi(x)h_i(x) are independent and uniform on {0,…,m−1}\{0, \ldots, m-1\}. Let NjN_j be the number of the knkn values equal to jj. By invariant (C) of the correctness proof, counter jj can be stuck only if NjN_j reached cmax⁡c_{\max} at some time, and with inserts only NjN_j never falls, so it suffices to bound NjN_j at the end. With deletes, the bound below applies to each set of nn keys that is present at some time.

Lemma. For each jj and each integer i≥1i \ge 1, Pr⁡[Nj≥i]≤(kni)m−i\Pr[N_j \ge i] \le \binom{kn}{i} m^{-i}.

Proof. If Nj≥iN_j \ge i, then some set of ii of the knkn hash values is all equal to jj. A fixed set of ii values is all equal to jj with probability m−im^{-i} by independence. There are (kni)\binom{kn}{i} sets, and the union bound gives the claim. □\square

Theorem. If k≤(m/n)ln⁡2k \le (m/n) \ln 2, then for each integer i≥1i \ge 1,

Pr⁡[max⁡jNj≥i]≤m(eln⁡2i)i.\Pr[\max_j N_j \ge i] \le m \left(\frac{e \ln 2}{i}\right)^i.

Proof. By the bound (ai)≤(ea/i)i\binom{a}{i} \le (ea/i)^i,

(kni)m−i≤(eknim)i≤(eln⁡2i)i,\binom{kn}{i} m^{-i} \le \left(\frac{e k n}{i m}\right)^i \le \left(\frac{e \ln 2}{i}\right)^i,

using kn/m≤ln⁡2kn/m \le \ln 2. The union bound over the mm positions multiplies by mm. ■\blacksquare

Numbers. For i=16i = 16 the factor is (eln⁡2/16)16≈1.37⋅10−15(e \ln 2/16)^{16} \approx 1.37 \cdot 10^{-15}, and for i=15i = 15, the value at which a 4-bit counter sticks, it is about 3.1⋅10−143.1 \cdot 10^{-14}. The exact law of NjN_j is binomial with knkn trials and success probability 1/m1/m, close to a Poisson law with mean kn/m≤ln⁡2kn/m \le \ln 2, so the true tail is smaller still: the Poisson tail at mean 0.70.7 for 16 or more is about 8⋅10−178 \cdot 10^{-17}.

Time. Each of INSERT, DELETE and QUERY makes at most kk hash evaluations and kk counter accesses, and does constant work for each, so it takes Θ(k)\Theta(k) time in the worst case on the word RAM with c≤wc \le w. The array uses mcmc bits.

A counting Bloom filter has no false negatives full

This proof shows: correctness, invariant, termination.

Write CtC_t for the counter array after the first tt operations and StS_t for the multiset of keys that are stored after them. The hash functions are fixed by line L2. For a key xx and a position jj let aj(x)=∣{i:hi(x)=j}∣a_j(x) = |\{i : h_i(x) = j\}|, the number of times xx uses position jj. Let Nj(t)=∑xmultSt(x) aj(x)N_j(t) = \sum_{x} \mathrm{mult}_{S_t}(x)\, a_j(x). Assume that each DELETE(x)(x) is applied only when xx has a copy in StS_t. Each loop has kk rounds and each round is a constant number of steps, so each operation ends.

Lemma 1 (stuck counters stay stuck). If Ct[j]=cmax⁡C_t[j] = c_{\max}, then Ct′[j]=cmax⁡C_{t'}[j] = c_{\max} for each t′≥tt' \ge t.

Proof. Only lines L4 and L6 write a counter, and both write only when the counter is below cmax⁡c_{\max}. □\square

Lemma 2 (invariant (C)). For each tt and jj, either Ct[j]=Nj(t)C_t[j] = N_j(t) and Cs[j]<cmax⁡C_s[j] < c_{\max} for each s≤ts \le t, or Ct[j]=cmax⁡C_t[j] = c_{\max}.

Proof. By induction on tt. At t=0t = 0, line L1 gives C0[j]=0=Nj(0)C_0[j] = 0 = N_j(0), and 0<cmax⁡0 < c_{\max} since c≥1c \ge 1. Assume the claim at tt and look at operation t+1t + 1 and a fixed position jj.

A QUERY changes neither CC nor SS. If Ct[j]=cmax⁡C_t[j] = c_{\max}, then Ct+1[j]=cmax⁡C_{t+1}[j] = c_{\max} by Lemma 1. So assume that Ct[j]=Nj(t)<cmax⁡C_t[j] = N_j(t) < c_{\max} and that the counter was never at cmax⁡c_{\max}.

INSERT(x)(x). Then Nj(t+1)=Nj(t)+aj(x)N_j(t+1) = N_j(t) + a_j(x). The loop of line L4 visits position jj exactly aj(x)a_j(x) times. Each visit adds 1 while the counter is below cmax⁡c_{\max}. If the counter stays below cmax⁡c_{\max} through all the visits, then Ct+1[j]=Nj(t)+aj(x)=Nj(t+1)C_{t+1}[j] = N_j(t) + a_j(x) = N_j(t+1), and it was never at cmax⁡c_{\max}. Otherwise it reaches cmax⁡c_{\max} at some visit, and by Lemma 1 it ends at cmax⁡c_{\max}.

DELETE(x)(x). Then Nj(t+1)=Nj(t)−aj(x)N_j(t+1) = N_j(t) - a_j(x), and Nj(t+1)≥0N_j(t+1) \ge 0 because St+1S_{t+1} is a multiset. The loop of line L6 visits position jj exactly aj(x)a_j(x) times, and each visit subtracts 1 since the counter is below cmax⁡c_{\max}. So Ct+1[j]=Nj(t)−aj(x)=Nj(t+1)≥0C_{t+1}[j] = N_j(t) - a_j(x) = N_j(t+1) \ge 0, and it never reaches cmax⁡c_{\max} on the way down. □\square

Theorem. After any sequence of operations that respects the precondition of DELETE, QUERY(x)(x) returns true for each xx with a copy in StS_t. QUERY(x)(x) returns true exactly when Ct[hi(x)]>0C_t[h_i(x)] > 0 for each ii.

Proof. Lines L7 and L8 return false at the first ii with C[hi(x)]=0C[h_i(x)] = 0, and line L9 returns true if there is none. This gives the second claim. Let xx have a copy in StS_t and let j=hi(x)j = h_i(x). Then aj(x)≥1a_j(x) \ge 1, so Nj(t)≥1N_j(t) \ge 1. By Lemma 2 either Ct[j]=Nj(t)≥1C_t[j] = N_j(t) \ge 1 or Ct[j]=cmax⁡≥1C_t[j] = c_{\max} \ge 1. So no counter of xx is 0, and the query returns true. □\square

Remark (the precondition). If DELETE(y)(y) is applied to a yy with no copy in SS, the step Nj(t+1)≥0N_j(t+1) \ge 0 of Lemma 2 fails. For example, let xx and yy share position jj with C[j]=1C[j] = 1 from xx alone; then DELETE(y)(y) sets C[j]=0C[j] = 0 and QUERY(x)(x) returns false. So the precondition cannot be removed. ■\blacksquare

Back to Counting Bloom filter