Zum Inhalt springen
uencoder

Verwenden Bloom filters für setzen membership

Bloom filters probabilistically testen setzen membership mit kein falsch negatives aber possible falsch positives. Space-efficient für large sets.

  1. 1

    A Bloom filter is a bit array of m bits and k hash functions. To add an element: set bits at positions h1(x), h2(x), ..., hk(x) to 1. To test membership: check if all k positions are 1. If any is 0, the element is definitely not in the set. If all are 1, it is probably in the set (false positive rate p ≈ (1 - e^(-kn/m))^k).

  2. 2
    Hash-Rechner →

    Use multiple hash functions (or one hash with different seeds) to compute the k positions. For k=7, m=10000 bits, n=1000 elements: false positive rate ≈ 0.8%. Increase m or k to reduce false positives. Use SHA-256 with different salts for each hash function.

  3. 3
    Murmur3-Hash →

    Use MurmurHash3 (non-cryptographic, fast) for Bloom filter hashing. Bloom filters do not need cryptographic hash functions — they need speed and uniform distribution. MurmurHash3, FNV-1a, and xxHash are good choices. Use k independent hash functions (or double hashing: h1(x) + i*h2(x) for i = 0..k-1).

Häufige Fragen

How do I choose m and k?
Given n elements and desired false positive rate p: m = -n*ln(p)/(ln(2))^2 bits, k = (m/n)*ln(2) hash functions. For n=1M, p=1%: m ≈ 9.6M bits (1.2MB), k ≈ 7. The filter uses 1.2MB regardless of element size — far less than storing 1M strings.
Can I remove elements from a Bloom filter?
Not from a standard Bloom filter — setting bits to 0 might affect other elements. Use a counting Bloom filter (counter instead of bits) to support deletion, or a Cuckoo filter (supports deletion, better space efficiency). For most use cases (caching, deduplication), not supporting deletion is acceptable.

Workflows, die diese Aufgabe lösen

Verwandte Tools