【问题标题】:Combining Bloom Filters组合布隆过滤器
【发布时间】:2011-05-23 15:43:53
【问题描述】:

我正在使用布隆过滤器来检查集合中的重复数据。但是,需要将两组数据的结果组合到一个过滤器中,以检查两组数据之间的重复。我在伪 Python 中设计了一个函数来执行此任务:

def combine(a : bloom_filter, b : bloom_filter):
    assert a.length == b.length
    assert a.hashes == b.hashes

    c = new bloom_filter(length = a.length, hashes = b.hashes)
    c.attempts = a.attempts + b.attempts
    c.bits = a.bits | b.bits

    # Determining the amount of items
    a_and_b = count(a & b)
    a_not_b = count(a & !b)
    not_a_b = count(!a & b)
    neither = count(!a & !b)
    c.item_count = a_not_b / a.length * a.item_count
                 + not_a_b / b.length * b.item_count
                 + a_and_b / c.length * min(a.item_count, b.item_count)

    return c

这听起来是否正确?我在内部就是否有可能做我想做的事情进行了相当大的争论,因为有关源数据的大部分信息都丢失了(这是布隆过滤器的重点)。

【问题讨论】:

    标签: language-agnostic bloom-filter


    【解决方案1】:

    您可以推导出一个公式来估算布隆过滤器的项目数量:

    c = log(z / N) / ((h * log(1 - 1 / N))
    
    N: Number of bits in the bit vector
    h: Number of hashes
    z: Number of zero bits in the bit vector
    

    这提供了对布隆过滤器中项目数量的相当准确的估计。您可以通过简单的减法来估算贡献。

    【讨论】:

      【解决方案2】:

      有可能......有点......

      假设集合 A 包含苹果和橙子

      假设 B 组包含豌豆和胡萝卜

      构建一个简单的 16 位布隆过滤器作为示例,CRC32 作为哈希

      crc32(apples) = 0x70CCB02F
      
      crc32(oranges) = 0x45CDF3B4
      
      crc32(peas) = 0xB18D0C2B
      
      crc32(carrots) = 0x676A9E28
      

      两个集合(A,B)都使用空的布隆过滤器(BF)(比如 16 位)开始

      BFA = BFB = 0000 0000 0000 0000 
      

      然后,将散列分解为一些位长度,我们将在这里使用 4 我们可以将苹果添加到BF。 例如

      Get Apples BF Index list by splitting up the hash:
      
      0x70CCB02F = 0111 0000 1100 1100 1011 0000 0010 1111
                   7      0    C    C   B     0    2     F     
      ----------------------------------------------------
      
      Add Apples to BFA by setting BF bit indexes [ 7, 0, 12, 12, 11, 0, 2, 15]
      
                                       (set the index bit of an empty BF to 1)
      Apples =     1001 1000 1000 0101 (<- see indexes 0,2,7,11,12,15 are set)
      BF =         0000 0000 0000 0000  (or operation adds that item to the BF)
      ================================
      Updated BFA = 1001 1000 1000 0101 
      

      以同样的方式将橙子添加到 BF:

      0x45CDF3B4 = 0100 0101 1100 1101 1111 0011 1011 0100
                    4    5    12   13   15    3   11   4
      ----------------------------------------------------
      Add oranges to BF by setting BF bit indexes [ 4,5,12,13,15,3,11,4]
      
      Oranges =      1011 1000 0011 1000 
      BFA =          1001 1000 1000 0101  (or operation)
      ================================
      Updated BFA =  1011 1000 1011 1101 
      

      所以现在苹果和橙子被插入到 BF1 最终值为1011 1000 1011 1101

      对 BFB 做同样的事情

      crc32(peas) = 0xB18D0C2B becomes => 
      set [11,2,12,0,13,1,8] in BFB
       0011 1001 0000 0011 = BF(peas)
      
      crc32(carrots) = 0x676A9E28 becomes => 
      set [8,2,14,9,10,6,7] in BFB
      
      0100 0111 1100 0100 = BF(carrots)
      
      so BFB = 
      0011 1001 0000 0011  BF(peas)
      0100 0111 1100 0100  BF(carrots)
      ===================  ('add' them to BFB via locial or op)
      0111 1111 1100 0111
      

      您现在可以在 B 中循环搜索 A 条目,反之亦然:

      B 是否包含“橙子”=>

       1011 1000 0011 1000 (Oranges BF representation)
       0111 1111 1100 0111 (BFB)
      =====================     (and operation)
       0011 1000 0000 0000  
      

      因为这个结果(0011 1000 0000 0000) 不匹配 橙子原厂BF,可以确定B不含橙子

      ... ...(对其余项目执行)

      然后,B 不包含任何 A 项, 就像 B 不包含任何苹果一样。

      我不认为那是你问的,而且看起来你可以用计算机来改变 BF,这更符合你的观点。似乎你可以做一个异或运算,这会给你一个包含两个差异的“单一”数组:

      0111 1111 1100 0111 (BFB)
      1011 1000 1011 1101 (BFA)
      ========================
      1100 0111 0111 1010 (BFA xor BFB) == (items in B not in A, and items in A not in B)
      

      意味着使用这个单一的 BF,您可以 100% 的时间检测到一个项目的不存在, 只是不存在 100% 的项目。

      您将使用它的方式如下(检查豌豆是否从 A 中丢失):

       1100 0111 0111 1010 (BFA xor BFB)
       0011 1001 0000 0011 (Peas)
      ============================== (And operation)
       0000 0001 0000 0010 (non-zero)
      

      因为(BFA xor BFB) &amp;&amp; (Peas) != 0 你知道一组不包含“豌豆”...

      再次,您将逐项测试,也许您可​​以进行聚合但可能不是一个好主意...

      希望这会有所帮助!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-10-12
        • 2011-09-22
        • 2023-04-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多