DSA Notes
Master Bloom filters — what they are, how multiple hash functions create a probabilistic set, false positive analysis and optimal parameter calculation, complete implementation, applications in databases and networking, and counting Bloom filters for deletion support.
What Is a Bloom Filter?
A Bloom filter is a space-efficient probabilistic data structure used to test whether an element is a member of a set. It can answer with certainty that an element is definitely not in the set, or with some probability that it might be in the set.
The trade-off: Bloom filters never produce false negatives (if it says "not in set," it's definitely not there), but they can produce false positives (saying "in set" when it actually isn't).
How It Works
A Bloom filter uses:
- A bit array of size m (all bits start as 0)
- k hash functions that each map an element to a position [0, m-1]
Adding an element:
Run the element through all k hash functions. Set those k bit positions to 1.
Checking membership:
Run through all k hash functions. If ALL k positions are 1 → element might be in set. If ANY position is 0 → element is definitely not in set.
| Bit array | [0,0,0,0,0,0,0,0,0,0] (size m=10) |
| Bit array | [0,0,1,0,0,1,0,0,1,0] |
| Bit array | [0,1,1,0,0,1,0,1,1,0] |
| Positions 2,5,8 | all 1 → "Possibly in set" ✓ |
| h2("cherry") = 3 ← 0 ← ZERO found | "Definitely NOT in set" ✓ |
| h3("grape") = 7 ← 1 | all 1 → "Possibly in set" (FALSE POSITIVE!) |
Complete Implementation
If mmh3 is not available, use hashlib:
import hashlib
def _hash_positions_hashlib(self, item: str) -> list:
"""Alternative using hashlib (no external dependency)."""
positions = []
for seed in range(self.k):
h = hashlib.md5(f"{item}{seed}".encode()).hexdigest()
positions.append(int(h, 16) % self.m)
return positionsFalse Positive Analysis
The false positive probability after inserting n elements into a filter with m bits and k hash functions:
Practical table for p=1% false positive rate:
| Elements (n) | Bits per element (m/n) | Memory | Hash functions (k) |
|---|---|---|---|
| 1,000 | 9.59 | ~1.2 KB | 7 |
| 100,000 | 9.59 | ~120 KB | 7 |
| 10,000,000 | 9.59 | ~12 MB | 7 |
| 1,000,000,000 | 9.59 | ~1.2 GB | 7 |
Compare: storing 1 billion 50-byte strings = 50 GB. Bloom filter = 1.2 GB for 1% FP rate, or 2.4 GB for 0.1%.
Testing
Applications
1. Database Query Optimization
| 2. If "definitely not" | skip disk read (avoid expensive I/O) |
| 3. If "possibly yes" | do disk read to confirm |
| Used by | HBase, Cassandra, RocksDB, BigTable |
2. Web Crawler URL Deduplication
class WebCrawler:
def __init__(self):
self.visited = BloomFilter(expected_elements=10**9, false_positive_rate=0.001)
def should_visit(self, url: str) -> bool:
if self.visited.might_contain(url):
return False # Probably already visited (occasional false skip is OK)
self.visited.add(url)
return True3. Chrome Safe Browsing
Google Chrome downloads a Bloom filter of malicious URLs. For each URL visited:
- Check Bloom filter: "Possibly dangerous" → query Google's servers
- "Definitely safe" → no server check needed
This saves bandwidth — the filter handles 99.9%+ of cases locally.
Counting Bloom Filter (Supports Deletion)
Standard Bloom filters cannot support deletion (unsetting a bit might affect other elements). Counting Bloom filters use a counter per position instead of a single bit:
Summary
| Property | Value |
|---|---|
| False negatives | Never — guaranteed |
| False positives | Possible — controlled by m and k |
| Space | O(m) bits — ~10 bits/element for 1% FP rate |
| Add | O(k) — k hash computations |
| Query | O(k) — k hash computations |
| Delete | Not supported (use counting variant) |
| Best used for | Membership testing where false positives are acceptable |
Bloom filters are one of the most practical and widely deployed probabilistic data structures. When you need to answer "have I seen this before?" at massive scale without massive memory, a Bloom filter is the answer.
*Next: Randomized Algorithms*
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Bloom Filter — Space-Efficient Probabilistic Membership Testing.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Data Structures & Algorithms topic.
Search Terms
data-structures-algorithms, data structures & algorithms, data, structures, algorithms, advanced, topics, bloom
Related Data Structures & Algorithms Topics