A Bloom Filter keeps an array of bits. To insert an element , set with hash functions. To test membership of :
- if any → is definitely not in the set
- if all → is probably in the set (but false positives are possible)
There are no false negatives, so if an element was inserted all its bits are set.
The probability that is erroneously claimed to be in is:
where is the number of inserted elements, is the array size, is the number of hash functions.
Typically:
- is a small constant which depends on the desired false error rate
- is proportional to
Insertion:
- feed the element to the hash functions to get array positions
- set positions bits to 1
Testing: - feed the element to the hash functions to get array positions
- two cases
- any of the position is at 0 = element def not in the set
- otherwise = either:
- the element is in the set
- bits have by chance been set to 1 during insertion of other elements (no way to tell the difference)
See also
- A Hash Function Must Be Deterministic, Uniform, and Fast — FNV-1a Is One Example — each of the k hash functions must be uniform and independent; poor hash functions increase false positive rate by clustering bits
- Collisions are unavoidable by Pigeonhole, Chaining and Open Addressing resolve them — false positives in Bloom Filters are the same phenomenon as hash collisions: different elements mapping to the same bit position
- Prefer sequential memory access, CPUs predict and prefetch based on locality — the bit array is compact and cache-friendly for sequential scans; the k random bit positions accessed per query are not sequential, so membership tests cause cache misses