当前位置: 面试刷题>> 计数型布隆过滤器 (经典算法题500道)


题目描述补充

题目:实现一个计数型布隆过滤器(Counting Bloom Filter)

布隆过滤器(Bloom Filter)是一种空间效率很高的概率型数据结构,用于判断一个元素是否在一个集合中。传统的布隆过滤器通过多个哈希函数将元素映射到位数组的不同位置,并将这些位置置为1。然而,布隆过滤器存在误判率,即它可能错误地判断一个不在集合中的元素为在集合中。

计数型布隆过滤器(Counting Bloom Filter)是对传统布隆过滤器的改进,它使用整数(而不是位)数组来记录元素的存在性,允许删除元素,并能在一定程度上减少误判率。在计数型布隆过滤器中,每个位置不再仅仅是一个位,而是一个计数器,每当一个元素被插入时,对应的计数器增加;当一个元素被删除时,对应的计数器减少(但计数器不应小于0,以避免负值)。

任务

  1. 设计并实现一个计数型布隆过滤器。
  2. 提供插入、查询和删除元素的接口。
  3. 分析并讨论计数型布隆过滤器的优缺点。

示例代码

以下是使用PHP、Python和JavaScript编写的计数型布隆过滤器示例代码。

PHP 示例

class CountingBloomFilter {
    private $bits;
    private $hashFunctions;

    public function __construct($size, $hashCount, $maxItems) {
        $this->bits = array_fill(0, $size, 0);
        $this->hashFunctions = array();

        for ($i = 0; $i < $hashCount; $i++) {
            $seed = rand(1, PHP_INT_MAX);
            $this->hashFunctions[] = function ($item) use ($seed, $size) {
                $hash = crc32($item . $seed);
                return ($hash & PHP_INT_MAX) % $size;
            };
        }
    }

    public function add($item) {
        foreach ($this->hashFunctions as $hashFunction) {
            $index = $hashFunction($item);
            $this->bits[$index]++;
        }
    }

    public function contains($item) {
        foreach ($this->hashFunctions as $hashFunction) {
            $index = $hashFunction($item);
            if ($this->bits[$index] === 0) {
                return false;
            }
        }
        return true;
    }

    public function remove($item) {
        foreach ($this->hashFunctions as $hashFunction) {
            $index = $hashFunction($item);
            if ($this->bits[$index] > 0) {
                $this->bits[$index]--;
            }
        }
    }
}

Python 示例

class CountingBloomFilter:
    def __init__(self, size, hash_count, max_items):
        self.size = size
        self.hash_count = hash_count
        self.bits = [0] * size
        self.hash_functions = [
            lambda x, i=i: hash(str(x) + str(i)) % size
            for i in range(hash_count)
        ]

    def add(self, item):
        for f in self.hash_functions:
            index = f(item)
            self.bits[index] += 1

    def contains(self, item):
        return all(self.bits[f(item)] > 0 for f in self.hash_functions)

    def remove(self, item):
        for f in self.hash_functions:
            index = f(item)
            if self.bits[index] > 0:
                self.bits[index] -= 1

JavaScript 示例

class CountingBloomFilter {
    constructor(size, hashCount) {
        this.bits = new Array(size).fill(0);
        this.hashFunctions = [];

        for (let i = 0; i < hashCount; i++) {
            this.hashFunctions.push(item => {
                let hash = BigInt(item) + BigInt(i);
                let hashResult = BigInt.asUintN(32)(hash) % BigInt(size);
                return Number(hashResult);
            });
        }
    }

    add(item) {
        this.hashFunctions.forEach(hashFunc => {
            let index = hashFunc(item);
            this.bits[index]++;
        });
    }

    contains(item) {
        return this.hashFunctions.every(hashFunc => this.bits[hashFunc(item)] > 0);
    }

    remove(item) {
        this.hashFunctions.forEach(hashFunc => {
            let index = hashFunc(item);
            if (this.bits[index] > 0) {
                this.bits[index]--;
            }
        });
    }
}

注意事项

  • 在实际应用中,需要选择合适的哈希函数和参数(如哈希函数的数量、位数组的大小等),以平衡误判率和空间消耗。
  • 计数型布隆过滤器允许删除操作,但多次删除不存在的元素可能会导致计数器变为负数,这在实际应用中需要特别注意。
  • 码小课网站中有更多关于数据结构和算法的相关内容,欢迎大家前去学习交流。
推荐面试题