Algorithm Search⌘K

Proofs: Flajolet-Martin sketch

These are the human proofs of Flajolet-Martin sketch, at full length.

The one-bitmap Flajolet-Martin estimate is within a constant factor full

This proof shows: complexity.

Take one bitmap, m=1m = 1, with LL larger than any index that occurs below. Assume ideal hashing: the values h(x)h(x) of distinct items are independent and uniform ww-bit integers, with ww large. Then for each item, Pr⁡[ρ(h(x))=r]=2−(r+1)\Pr[\rho(h(x)) = r] = 2^{-(r+1)}, independently over the items. Let nn distinct items be seen and let RR be the index of the lowest 0 bit.

Lemma 1. Pr⁡[B[r]=1]=1−(1−2−(r+1))n\Pr[B[r] = 1] = 1 - (1 - 2^{-(r+1)})^n.

Proof. By Lemma 1 of the correctness proof, B[r]=0B[r] = 0 exactly when no item has ρ=r\rho = r. Each of the nn independent items avoids rr with probability 1−2−(r+1)1 - 2^{-(r+1)}. □\square

Theorem. For each integer a≥0a \ge 0,

Pr⁡[2R≥2a]≤n2a,Pr⁡[2R<2a]≤∑r=0a−1exp⁡ ⁣(−n2r+1).\Pr[2^R \ge 2^a] \le \frac{n}{2^{a}}, \qquad \Pr[2^R < 2^a] \le \sum_{r=0}^{a-1} \exp\!\left(-\frac{n}{2^{r+1}}\right).

In particular, for cc a power of two with n/cn/c a power of two, Pr⁡[2R≥cn]≤1/c\Pr[2^R \ge cn] \le 1/c and Pr⁡[2R<n/c]≤e−c/(1−e−c)\Pr[2^R < n/c] \le e^{-c}/(1 - e^{-c}).

Proof. Upper tail. If R≥aR \ge a with a≥1a \ge 1, then B[a−1]=1B[a-1] = 1, which by Lemma 1 and Bernoulli's inequality 1−(1−p)n≤np1 - (1 - p)^n \le np has probability at most n2−an 2^{-a}. For a=0a = 0 the bound n≥1n \ge 1 is trivial.

Lower tail. If R<aR < a, then B[r]=0B[r] = 0 for some r<ar < a. By Lemma 1, 1−p≤e−p1 - p \le e^{-p} and the union bound, this has probability at most ∑r<ae−n2−(r+1)\sum_{r < a} e^{-n 2^{-(r+1)}}.

The special case. Take 2a=cn2^a = cn in the upper tail. For the lower tail take 2a=n/c2^a = n/c. The term r=a−1−lr = a - 1 - l is exp⁡(−c 2l)\exp(-c \, 2^{l}), and 2l≥1+l2^l \ge 1 + l, so the sum is at most ∑l≥0e−ce−cl=e−c/(1−e−c)\sum_{l \ge 0} e^{-c} e^{-cl} = e^{-c}/(1 - e^{-c}). □\square

So for c=4c = 4 the estimate 2R2^R lies in [n/4,4n)[n/4, 4n) with probability at least 1−1/4−0.019>0.731 - 1/4 - 0.019 > 0.73. The constant factor 1/φ1/\varphi and the averaging over mm bitmaps make this a relative error of O(1/m)O(1/\sqrt{m}); Flajolet and Martin (1985) proved the exact constants φ=0.77351\varphi = 0.77351 and 0.78/m0.78/\sqrt{m}.

Cost. ADD computes one hash, one division by mm, one count of trailing zeros and one bit write, which is Θ(1)\Theta(1) word operations on the word RAM. ESTIMATE scans at most L+1L + 1 bits of each bitmap, O(mL)O(mL) steps. The space is mLmL bits. ■\blacksquare

The Flajolet-Martin sketch is a function of the set of distinct items full

This proof shows: correctness, invariant, termination.

For an item xx let j(x)=h(x) mod mj(x) = h(x) \bmod m, q(x)=⌊h(x)/m⌋q(x) = \lfloor h(x)/m \rfloor, and call xx recorded if q(x)>0q(x) > 0 and ρ(q(x))<L\rho(q(x)) < L; then let r(x)=ρ(q(x))r(x) = \rho(q(x)). For a set AA of items define the bitmaps Σ(A)\Sigma(A) by

Σ(A)j[r]=1  ⟺  some recorded x∈A has j(x)=j and r(x)=r.\Sigma(A)_j[r] = 1 \iff \text{some recorded } x \in A \text{ has } j(x) = j \text{ and } r(x) = r.

Lemma 1 (invariant). After the items a1,…,ata_1, \ldots, a_t have been added, the bitmaps equal Σ({a1,…,at})\Sigma(\{a_1, \ldots, a_t\}).

Proof. By induction on tt. For t=0t = 0 line L1 gives all zeros, which is Σ(∅)\Sigma(\emptyset). Let the claim hold for tt and let A={a1,…,at}A = \{a_1, \ldots, a_t\} and x=at+1x = a_{t+1}. Line L3 computes j(x)j(x) and q(x)q(x), and line L4 sets bit r(x)r(x) of Bj(x)B_{j(x)} if xx is recorded, and does nothing otherwise. No bit is cleared. So the new bitmaps have a 1 exactly at the 1s of Σ(A)\Sigma(A) and, if xx is recorded, at (j(x),r(x))(j(x), r(x)). This is Σ(A∪{x})\Sigma(A \cup \{x\}). □\square

Theorem.

  1. The state, and so the output of ESTIMATE, depends only on the set of distinct items seen, not on their order or their number of repeats.
  2. The bitwise OR of the sketches of two streams with the same mm, LL and hh equals the sketch of the concatenated stream.
  3. RjR_j is the least r∈{0,…,L}r \in \{0, \ldots, L\} such that r=Lr = L or Bj[r]=0B_j[r] = 0, and ESTIMATE returns (m/φ)2S/m(m/\varphi) 2^{S/m} with S=∑jRjS = \sum_j R_j.
  4. Each operation ends: ADD takes a constant number of steps, and ESTIMATE scans at most L+1L + 1 positions of each of the mm bitmaps.

Proof. Claim 1 is Lemma 1, since Σ(A)\Sigma(A) depends only on AA. For claim 2, the definition gives Σ(A∪A′)=Σ(A)∨Σ(A′)\Sigma(A \cup A') = \Sigma(A) \lor \Sigma(A') bit by bit, because "some recorded x∈A∪A′x \in A \cup A'" is "some recorded x∈Ax \in A or some recorded x∈A′x \in A'". Claims 3 and 4 follow from lines L5 to L7 and the definition of RjR_j. ■\blacksquare

Back to Flajolet-Martin sketch