012345678910110101010101010101010101011def add(bits, w):2 for i in spots(w):3 bits[i] = 14def has(bits, w):5 for i in spots(w):6 if bits[i] == 0:7 return False8 return Truespots(w)wordanswer
Lamps: the bit array; a lit lamp is a 1Chip: spots(w), three hash functionsA hopping token: one hash pointing at one bitA Bloom filter with m = 12 bits and k = 3 hash functions. add() sets the bits a word's hashes point at; has() checks them and stops at the first 0. A 0 means definitely not added; all ones means maybe, and dove shows that maybe can be wrong.

Adding words

  1. A Bloom filter remembers which words it has seen, using very little memory. It is an array of m bits, all 0 to start (here m = 12), plus k hash functions (here k = 3). A hash function turns a word into a number.

Asking questions

The limits

What happens

;; step 1/9 · bit-array
bits = [0] * M        M = 12
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
K = 3 hash functions, numbered 0, 1, 2
it stores no words, only bits

The formula p ≈ (1 − e^(−kn/m))^k assumes uniform, independent hashes (Broder and Mitzenmacher, 2004). It is lowest at k = (m/n) ln 2 hash functions, so about 10 bits per word give under 1% false positives, however long the words are. A Bloom filter has false positives but no false negatives: if has(w) returns False, w was certainly never added. Bloom introduced the idea in 1970 to save memory when a few wrong "maybe" answers are acceptable.