Bloom filters
لم يُترجم بعد؛ يُعرض باللغة الإنجليزية
عن هذا الدرس
A Bloom filter answers "have I seen this before?" using a few bits per item and never stores the items themselves. The price: it can say "maybe" to something it never saw. Watch a 12-bit filter with 3 hash functions add cat and dog, then answer for cat, fox and dove, and see why it cannot delete. It builds on the Software lesson on hash maps and the lesson "Chaining vs probing: expected cost and clustering", which explain what a hash function does.
Twelve bits, three hashes
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.
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اقرأ الدرس خطوة بخطوة
Twelve bits, three hashes
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.
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 bitsA word becomes three spots
spots("cat") runs the word through the 3 hash functions. Each gives a number from 0 to 11: a spot in the array. For cat they are 1, 5, 6. The same word always gives the same spots.
spots(w): hash i = sha256("i:w") mod 12 sha256("0:cat") mod 12 = 1 sha256("1:cat") mod 12 = 5 sha256("2:cat") mod 12 = 6 spots("cat") = [1, 5, 6] sha256 is the same on every run; Python's built-in hash() of a str is notAdd cat: set its bits
add() sets the bit at each spot to 1: bits 1, 5, 6. That is all an insert does. The word itself is not stored anywhere, so the filter stays the same size however long the words are.
add(bits, "cat") bits[1] = 1 bits[5] = 1 bits[6] = 1 bits = [0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0] cost: k = 3 hashes, k bit writesAdd dog: bits are shared
dog's spots are 0, 5, 7. Bit 5 is already 1 from cat, so it just stays 1. Words share bits. Now 5 of the 12 bits are 1, and nothing records which word set which bit.
add(bits, "dog") bits[0] = 1 bits[5] = 1 (already 1) bits[7] = 1 bits = [1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0] 5 ones; bit 5 belongs to cat and dogAsk for cat: all ones
has() looks at the same spots: 1, 5, 6. All three are 1, so it returns True. That means "maybe": the word was probably added. An added word always finds its own ones, so a Bloom filter never wrongly says no.
has(bits, "cat") bits[1] == 1 bits[5] == 1 bits[6] == 1 no zero found: return True ("maybe yes")Ask for fox: one zero
fox's spots are 6, 11, 9. Bit 6 is 1, but bit 11 is 0, so has() returns False at once and never checks bit 9. A single 0 proves fox was never added: if it had been, that bit would be 1.
has(bits, "fox") bits[6] == 1 bits[11] == 0 -> return False ("definitely not") bits[9] never looked atAsk for dove: a false positive
dove was never added, but its spots 1, 6, 7 are all 1: bits 1 and 6 came from cat, bit 7 from dog. has() returns True. This wrong "maybe" is a false positive, the price paid for storing only bits.
has(bits, "dove") bits[1] == 1 set by cat bits[6] == 1 set by cat bits[7] == 1 set by dog return True, but "dove" was never addedHow often it is wrong
With 5 of 12 bits set, a new word lands on three ones about (5/12)³ ≈ 7% of the time. In general p ≈ (1 − e^(−kn/m))^k for n words. With 10 bits per word and k = 7, p is under 1%.
this filter: 5 of 12 bits are 1 (5/12)^3 = 0.072 about 7% formula, n = 2: (1 - e^(-3*2/12))^3 = 0.061 m/n = 10 bits per word, k = 7: p = 0.0082 best k = (m/n) ln 2 = 6.93 for m/n = 10Why you cannot delete
Try to remove cat by clearing bits 1, 5, 6. Bit 5 was dog's too, so has("dog") now finds a 0 and says no: a false negative. That breaks the filter's one promise, so a plain Bloom filter cannot delete.
pretend delete: bits[1] = bits[5] = bits[6] = 0 bits = [1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0] has(bits, "dog") bits[0] == 1 bits[5] == 0 -> return False, but dog WAS added
أسئلة سريعة: 5
اختر إجابة واحدة لكل سؤال. يمكنك تغيير اختياراتك قبل التحقق منها.
01In the lesson's filter, after adding cat and dog, has(bits, "fox") returns False. What does that tell you?
If fox had been added, all its bits would be 1. Bit 11 is 0, so fox was certainly never added. A Bloom filter has no false negatives.
02has(bits, "dove") returns True, but dove was never added. Why?
Bits 1 and 6 came from cat and bit 7 from dog. The filter cannot tell who set a bit, so it answers maybe. That wrong maybe is a false positive.
03Why can you not delete cat by setting its bits (1, 5, 6) back to 0?
Clearing a shared bit erases part of another word. Then dog, which was added, reads as not added: a false negative, which a Bloom filter must never give.
04A filter has m = 1000 bits, n = 100 words and k = 7 hash functions. Using p ≈ (1 − e^(−kn/m))^k, about how often is it wrong for a word never added?
kn/m = 0.7, e^(−0.7) ≈ 0.497, so 1 − 0.497 ≈ 0.503, and 0.503^7 ≈ 0.0082. That is 10 bits per word and close to the best k = (m/n) ln 2 ≈ 6.9.
05The code uses hashlib.sha256 rather than Python's built-in hash() for strings. Why?
Python salts str and bytes hashes with a random value each time it starts, so a filter saved to disk would not match the next run. Any fixed, well-spread hash works; sha256 is just one that ships with Python.
المسرد
- Bloom filter
- A bit array plus k hash functions that answers whether an item was added: "definitely not" or "maybe".
- Bit array
- A row of m bits, each 0 or 1. The filter's only storage.
- Hash function
- A function that turns an item into a number. Here each hash gives a spot from 0 to m − 1.
- Spots
- The k positions a word's hashes point at. add() sets them to 1; has() checks them.
- False positive
- The filter says maybe for an item that was never added, because other items set all its bits.
- False negative
- Saying no for an item that was added. A Bloom filter never does this, as long as nothing is deleted.
- False positive rate
- How often a never-added item gets a maybe. About (1 − e^(−kn/m))^k for n items.
الملاحظات والمصادر
- The three hash functions are SHA-256 of "0:word", "1:word" and "2:word", taken mod 12. Real filters use faster hashes, but any hash works if it spreads words evenly over the m spots (the uniform hashing assumption, Sedgewick and Wayne Assumption J).
- 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.
- Variants exist for deletion. A counting Bloom filter keeps a small counter instead of a single bit in each spot, at the cost of more memory.
- The code is real Python 3. After adding cat and dog, bits is [1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0], and has() returns True for cat, False for fox and True for dove.
- Broder and Mitzenmacher, Network Applications of Bloom Filters: A Survey (Internet Mathematics, 2004)
- Bloom filter (with a reference to Bloom 1970, Communications of the ACM 13(7))
- Sedgewick and Wayne, Algorithms 4th ed., §3.4 Hash Tables
- MIT 6.006 Introduction to Algorithms (Spring 2020), Lecture 4: Hashing
- Cormen, Leiserson, Rivest and Stein, Introduction to Algorithms, 4th ed. (MIT Press, 2022), chapter 11
- Python docs: hashlib, secure hashes and message digests
- Python docs: object.__hash__ and hash randomization
اختبار يتاح بعد الخطوة الأخيرة.