当前位置:网站首页>布隆过滤器
布隆过滤器
2022-07-02 22:58:00 【JunesFour】
布隆过滤器
1. 使用场景
- 在使⽤word⽂档时,word如何判断某个单词是否拼写正确.
- ⽹络爬⾍程序,怎么让它不去爬相同的url⻚⾯?允许有误差.
- 垃圾邮件(短信)过滤算法如何设计?允许有误差.
- 公安办案时,如何判断某嫌疑⼈是否在⽹逃名单中?控制误差 假阳率.
- 缓存穿透问题如何解决?允许有误差.
2. 基本思想
可以通过多个Hash函数将元素映射成位图中的数个点,将这些点标记为true,在查询的时候,通过查询这些点是否为true,就可以判断布隆过滤器中是否存在这个元素.所以布隆过滤器相⽐传统的查询结构(例如:hash,set,map等数据结构)更加⾼效,占⽤空间更
⼩.
注意:
- 布隆过滤器无法删除元素,因为它不存储具体的元素,只存储元素映射成的数个点,而且每个点可能被多个元素映射的结果所覆盖.
- 布隆过滤器只能判断一个元素不存在或者可能存在,当使用hash函数映射出的所有点都为true时,该元素可能存在,只要有一个不为true,就必定不存在.
3. 布隆过滤器的实现
组成
位图(bit数组)+ n个hash函数.

原理
当⼀个元素加⼊位图时,通过k个hash函数将这个元素映射到位图的k个点,并把它们置为1;当检索时,再通过k个hash函数运算检测位图的k个点是否都为1;如果有不为1的点,那么认为不存在;如果全部为1,则可能存在(存在误差).
在位图中每个槽位只有两种状态(0或者1),⼀个槽位被设置为1状态,但不明确它被设置了多少次;也就是不知道被多少个str1哈希映射以及是被哪个hash函数映射过来的;所以不⽀持删除操作.

4. 布隆过滤器的设计
在实际应⽤过程中,布隆过滤器该如何使⽤?要选择多少个hash函数,要分配多少空间的位图,存
储多少元素?另外如何控制假阳率(布隆过滤器能明确⼀定不存在,不能明确⼀定存在,那么存在
的判断是有误差的,假阳率就是错误判断存在的概率)?
我们一般使用以下四个参数来解决上面的问题:
- n – 布隆过滤器中元素的个数,如上图 只有str1和str2 两个元素 那么 n=2.
- p – 假阳率,在0-1之间 0.000000.
- m – 位图所占空间.
- k – hash函数的个数.
公式如下:
n = ceil(m / (-k / log(1 - exp(log(p) / k))))
p = pow(1 - exp(-k / (m / n)), k)
m = ceil((n * log(p)) / log(1 / pow(2, log(2))))
k = round((m / n) * log(2))
其中下面的两个参数m和k由上面两个参数n和p计算得到,可以自己计算,也可以在相关的网站上输入前两个值然后计算后两个值:

k个hash函数
上述计算结果中,k=23 ,在实际中,我们不会真的去选择23个hash函数,而是采用双重hash的方式去模拟出23个hash函数:
// 采⽤⼀个hash函数,给hash传不同的种⼦偏移值
// #define MIX_UINT64(v) ((uint32_t)((v>>32)^(v)))
uint64_t hash1 = MurmurHash2_x64(key, len, Seed);
uint64_t hash2 = MurmurHash2_x64(key, len, MIX_UINT64(hash1));
for (i = 0; i < k; i++) // k 是hash函数的个数
{
Pos[i] = (hash1 + i*hash2) % m; // m 是位图的⼤⼩
}
边栏推荐
- Codeforces Round #771 (Div. 2)---A-D
- Interpretation of new plug-ins | how to enhance authentication capability with forward auth
- JDBC tutorial
- JDBC practice cases
- What is the official website address of e-mail? Explanation of the login entry of the official website address of enterprise e-mail
- What is the standard format of a 2000-3000 word essay for college students' classroom homework?
- 教育学大佬是怎么找外文参考文献的?
- SQL query statement parameters are written successfully
- Difference between NVIDIA n card and amda card
- 67 page overall planning and construction plan for a new smart city (download attached)
猜你喜欢

请问大家在什么网站上能查到英文文献?

Explain in detail the process of realizing Chinese text classification by CNN

35页危化品安全管理平台解决方案2022版

开源了 | 文心大模型ERNIE-Tiny轻量化技术,又准又快,效果全开

有哪些比较推荐的论文翻译软件?

The privatization deployment of SaaS services is the most efficient | cloud efficiency engineer points north

Interpretation of new plug-ins | how to enhance authentication capability with forward auth

Which software can translate an English paper in its entirety?

The privatization deployment of SaaS services is the most efficient | cloud efficiency engineer points north

35 pages dangerous chemicals safety management platform solution 2022 Edition
随机推荐
Is there a specific format for English papers?
JDBC練習案例
Installing redis under Linux
MySQL advanced learning notes (III)
leetcode 650. 2 keys keyboard with only two keys (medium)
开发知识点
经济学外文文献在哪查?
JDBC tutorial
QT 如何将数据导出成PDF文件(QPdfWriter 使用指南)
S12. Verify multi host SSH mutual access script based on key
Unique line of "Gelu"
Judge whether the binary tree is full binary tree
Xcode real machine debugging
[array] binary search
How to specify const array in the global scope of rust- How to specify const array in global scope in Rust?
Returns the root node of the largest binary search subtree in a binary tree
Digital twin smart factory develops digital twin factory solutions
Open Source | Wenxin Big Model Ernie Tiny Lightweight Technology, Accurate and Fast, full Open Effect
[shutter] shutter open source project reference
How to write the design scheme of the thesis?