数据结构之布隆过滤器

1. 布隆过滤器(Bloom Filter)

如果使用哈希表来判断一个元素是否存在,虽然时间复杂度可以达到**O(1)**级别,但是空间的利用率不高。但同时,如果数据量过于庞大就需要过大的内存空间。

1970年由布隆提出,一个空间效率高的概率型数据结构,用来判断一个元素一定不存在或者可能存在。
本质上是一个很长的二进制向量和一系列Hash函数。
优点:空间效率和查询时间都远远超过一般的算法;
缺点:一定的误判率(可以通过代码尽可能的降低),删除困难。
添加、查询的时间复杂度:O(k)k是哈希函数个数。
空间复杂度:O(m)m是二进制个数。

1.1 原理

  • 假设由 20位二进制(默认都是0)、3个哈希函数组成,每个元素经过哈希函数处理都能生成一个索引位置。
    在这里插入图片描述
  • 添加元素:将每一个哈希函数生成的索引位置都设为1。如图中添加A时将通过三个哈希函数得出的索引位置的值都设置为1。
    在这里插入图片描述
  • 查询元素:通过哈希函数:
    1. 如果有一个哈希函数生成的索引不为1,就不存在(100%准确);
    2. 如果每一个哈希函数生成的索引都为1,就表示存在(有误判率),因为某个索引位置设置为1是由于其它元素设置的。

1.2 误判率

  • 误判率p受三个因素影响:

    1. 二进制个数m
    2. 哈希函数个数k
    3. 数据规模n
  • 误判率计算公式:
    在这里插入图片描述

  • 通过误判率和数据规模推导
    在这里插入图片描述

1.3 代码实现

  • 变量设计:
    // 二进制向量长度
    private int bitSize;

    // 二进制向量 一个long元素 64 位
    private long[] bits;

    // 哈希函数个数
    private int hashSize;
  • 接口设计:
    /**
     * 构造函数
     * @param n 数据规模
     * @param p 误判率
     */
    public BloomFilter(int n, double p) {
        if (n <= 0 || p <= 0 || p >= 1) {
            throw new IllegalArgumentException("parameter error");
        }

        double ln2 = Math.log(2);

        // 计算二进制向量长度
        bitSize = (int) (-(n * Math.log(p)) / (ln2 * ln2));

        // 计算哈希函数个数
        hashSize = (int) (bitSize * ln2 / n);

        // 确定数组长度
        bits = new long[(bitSize + Long.SIZE - 1) / Long.SIZE];
    }


    /**
     * 添加元素
     * @param v
     */
    public boolean put(T v){

    }

    /**
     * 查询是否存在
     * @param v
     * @return
     */
    public boolean contains(T v){

    }
  • 代码实现
    /**
     * 添加元素
     * @param v
     * @return
     */
    public boolean put(T v){
        nullCheck(v);

        // 计算 hash 值
        int h1 = v.hashCode();
        int h2 = hashSize >>> 16;

        boolean result = false;

        for (int i = 0; i < hashSize; i++) {
            // 根据两个 hash 计算当前hash
            int combinedHash = h1 + (i * h2);

            // 小于0 取反
            if (combinedHash < 0){
                combinedHash = ~combinedHash;
            }
            // 如果 hash 值过大,取模
            int index = combinedHash % bitSize;

            // 设置索引位置为1
            if (set(index)) result = true;
        }

        return result;
    }

    /**
     * 查询是否存在
     * @param v
     * @return
     */
    public boolean contains(T v){
        nullCheck(v);

        // 利用value生成2个整数
        int hash1 = v.hashCode();
        int hash2 = hash1 >>> 16;

        for (int i = 1; i <= hashSize; i++) {
            int combinedHash = hash1 + (i * hash2);
            if (combinedHash < 0) {
                combinedHash = ~combinedHash;
            }
            // 生成一个二进位的索引
            int index = combinedHash % bitSize;

            // 查询index位置的二进位是否为0
            if (!get(index)) return false;
        }
        return true;
    }

    /**
     * 参数检测
     * @param v
     */
    private void nullCheck(T v){
        if (v == null) {
            throw new IllegalArgumentException("v must be not null");
        }
    }

    /**
     * 设置index位置的二进位为1
     */
    private boolean set(int index) {

        // 根据当前索引获取数组中的元素
        long value = bits[index / Long.SIZE];

        // 计算当前位置位置的二进制值,除非当前索引位置是1外都是0
        int bitValue = 1 << (index % Long.SIZE);

        // 当前位置取反
        bits[index / Long.SIZE] = value | bitValue;

        // 判断是否已经存在
        return (value & bitValue) == 0;
    }

    /**
     * 查看index位置的二进位的值
     * @return true代表1, false代表0
     */
    private boolean get(int index) {
        long value = bits[index / Long.SIZE];
        return (value & (1 << (index % Long.SIZE))) != 0;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值