Lesson
The theory — Bloom Filter: Definitely Not, or Probably Yes
A Bloom filter answers whether an item belongs to a set, using a row of m bits and k hash functions. Inserting an item sets the k bits its hashes point to. A lookup checks the same k bits: if any is 0 the item is definitely not in the set; if all are 1 it is probably in it. A wrong “probably yes” is a false positive. With n items inserted, the standard estimate of the false-positive rate, an exponential approximation of Bloom's own 1970 formula, is (1 − e−kn/m)k, smallest at k = (m/n) ln 2.
What each symbol means
m- the number of bits in the filter
k- the number of hash functions, so the number of bits each word sets and each lookup tests
n- the number of words inserted
X- the number of bits set to 1 once all n words are in
- Assumes
- The k positions of a word come from two hashes by double hashing: position i is (h₁ + i·h₂) mod m, with h₁ the FNV-1a hash of the word, h₂ a stirred djb2 hash made odd, and m a power of two, so a word's k positions are always different. The standard estimate treats every one of the kn positions as an independent random choice; double hashing and a small m are where it parts company with a real filter. The false-positive rate is measured over the 10,000 strings 0 to 9999 and is exact for those strings, not for every possible word.
Problem solved in full
-
How small could Bloom's 1970 hyphenation filter be? 4 steps
Burton Bloom's example in 1970 was a hyphenation program for 500,000 words. A few simple rules hyphenate 450,000 of them; the other 50,000 must be looked up in a dictionary on disk. He proposed keeping a filter of those 50,000 words in core memory and accepting that 1 in 16 of the easy words would send the program to the disk for nothing. How many bits does the filter need, with how many hash functions, and how many disk reads are left? Then set this page to the same number of bits per word and measure.
-
At the best k, the standard estimate needs log₂(1/p) / ln 2 bits per word. For p = 1/16 that is 4 / 0.6931 = 5.77 bits, so the filter is 50,000 × 5.77 = 288,539 bits: Bloom's own figure was “less than 300,000 bits”. The best k is 5.77 × ln 2 = 4, and with four hash functions half the bits are expected to be 1.
-
Every one of the 50,000 hard words still goes to the disk, and so does 1 easy word in 16, which is 450,000 / 16 = 28,125 more. That is 78,125 disk reads out of 500,000, or 15.6%. Bloom rounded it to 78,000, a saving of 84% against going to the disk for every word.
-
The length of a word appears nowhere in this. The filter costs 5.77 bits per word whether the word is “cat” or “incomprehensibilities”, where Bloom reckoned that a conventional hash table needed at least 19 bits on average to represent each word. Each halving of p adds 1 / ln 2 = 1.44 bits per word: for 1 in 64 it would be 6 / 0.6931 = 8.66.
-
This page holds at most 64 words, but the estimate depends only on m/n and k. Set m = 256, n = 44 (5.82 bits per word) and k = 4. 122 of the 256 bits are 1, 47.7%, and 535 of the 10,000 never-inserted strings answer “probably yes”: 5.35%, under the 1/16 = 6.25% the filter was sized for. The standard estimate for this filter is 6.11%.
Answer
288,539 bits and 4 hash functions, leaving 78,125 disk reads of 500,000. The page's 44 words in 256 bits measure 5.35%, inside the target. That is one filter of 44 words. Whether 50,000 words in 288,539 bits land as close to 1/16 the page cannot test: it stops at 64 words.
-
Learning path
What a data structure makes cheap
References (1)
- The method itself, the turning point at half the bits set in the first insight, and the hyphenation example the worked problem recomputes: Burton H. Bloom, "Space/time trade-offs in hash coding with allowable errors," Communications of the ACM 13(7), July 1970, pp. 422–426.