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, , with larger than any index that occurs below. Assume ideal hashing: the values of distinct items are independent and uniform -bit integers, with large. Then for each item, , independently over the items. Let distinct items be seen and let be the index of the lowest 0 bit.
Lemma 1. .
Proof. By Lemma 1 of the correctness proof, exactly when no item has . Each of the independent items avoids with probability .
Theorem. For each integer ,
In particular, for a power of two with a power of two, and .
Proof. Upper tail. If with , then , which by Lemma 1 and Bernoulli's inequality has probability at most . For the bound is trivial.
Lower tail. If , then for some . By Lemma 1, and the union bound, this has probability at most .
The special case. Take in the upper tail. For the lower tail take . The term is , and , so the sum is at most .
So for the estimate lies in with probability at least . The constant factor and the averaging over bitmaps make this a relative error of ; Flajolet and Martin (1985) proved the exact constants and .
Cost. ADD computes one hash, one division by , one count of trailing zeros and one bit write, which is word operations on the word RAM. ESTIMATE scans at most bits of each bitmap, steps. The space is bits.
The Flajolet-Martin sketch is a function of the set of distinct items full
This proof shows: correctness, invariant, termination.
For an item let , , and call recorded if and ; then let . For a set of items define the bitmaps by
Lemma 1 (invariant). After the items have been added, the bitmaps equal .
Proof. By induction on . For line L1 gives all zeros, which is . Let the claim hold for and let and . Line L3 computes and , and line L4 sets bit of if is recorded, and does nothing otherwise. No bit is cleared. So the new bitmaps have a 1 exactly at the 1s of and, if is recorded, at . This is .
Theorem.
- 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.
- The bitwise OR of the sketches of two streams with the same , and equals the sketch of the concatenated stream.
- is the least such that or , and ESTIMATE returns with .
- Each operation ends: ADD takes a constant number of steps, and ESTIMATE scans at most positions of each of the bitmaps.
Proof. Claim 1 is Lemma 1, since depends only on . For claim 2, the definition gives bit by bit, because "some recorded " is "some recorded or some recorded ". Claims 3 and 4 follow from lines L5 to L7 and the definition of .