赞
踩
一道面试题:
给定40亿个无序不重复的无符号整数。给一个无符号整数,如何快速判断一个数是否在这40亿个数中
所谓位图,就是用每一位来存放某种状态,适用于海量数据,数据无重复的场景。通常是用来判断某个数据存不存在的
bitset<32> b1;// 0
bitset<32> b2(-1);// 4294967295
bitset<32> b3("1010");// 10
cout << b1.to_ulong() << endl;// 转换成无符号长整型
cout << b2.to_ulong() << endl;
cout << b2 << endl;// 直接输出二进制表示
cout << b3.to_ulong() << endl;
cout << b3.to_string() << endl;// 转换成字符串
函数 | 说明 |
---|---|
operator[](size_t pos) | 返回pos位置的bit(1或0) |
count | 返回1的个数 |
size | 返回位图大小 |
test(size_t pos) | pos位置是1返回true,0返回false |
any | 全0返回false,否则返回true |
all | 全1返回true,否则返回false |
none | 全0返回true,否则返回false |
函数 | 说明 |
---|---|
set(size_t pos, bool val = true) | 将pos位置设置为val |
reset(size_t pos) | 将pos位置设置为0 |
flip(size_t pos) | pos位置取反 |
// 非类型模板参数 template<size_t N> class bitset { public: bitset() { // 加一个字节避免 20 / 8 = 2访问20时越界 //_bits.resize(N / 8+1, 0);// 1个字节8个比特位 _bits.resize((N >> 3) + 1, 0);// 注意运算符优先级 } void set(size_t pos) { //size_t i = pos / 8; size_t i = pos >> 3;// 在第i个char size_t j = pos % 8;// 第i个char的第j个比特 _bits[i] |= (1 << j); } void reset(size_t pos) { size_t i = pos >> 3; size_t j = pos % 8; _bits[i] &= (~(1 << j)); } bool test(size_t pos) { size_t i = pos >> 3; size_t j = pos % 8; //return (_bits[i] >> j) & 1; return _bits[i] & (1 << j); } private: std::vector<char> _bits; };
我们在使用新闻客户端看新闻时,它会给我们不停地推荐新的内容,它每次推荐时要去重,去掉那些已经看过的内容。问题来了,新闻客户端推荐系统如何实现推送去重的?
用服务器记录了用户看过的所有历史记录,当推荐系统推荐新闻时会从每个用户的历史记录里进行筛选,过滤掉那些已经存在的记录。 如何快速查找呢?
布隆过滤器是由布隆(Burton Howard Bloom)在1970年提出的 一种紧凑型的、比较巧妙的概率型数据结构,特点是高效地插入和查询,可以用来告诉你 “某样东西一定不存在或者可能存在” ,它是用多个哈希函数,将一个数据映射到位图结构中。此种方式不仅可以提升查询效率,也可以节省大量的内存空间
布隆过滤器的思想是将一个元素用多个哈希函数映射到一个位图中,因此被映射到的位置的比特位一定为1。所以可以按照以下方式进行查找:分别计算每个哈希值对应的比特位置存储的是否为零,只要有一个为零,代表该元素一定不在哈希表中,否则可能在哈希表中。
注意:布隆过滤器如果说某个元素不存在时,该元素一定不存在,如果该元素存在时,该元素可能存在,因为有些哈希函数存在一定的误判。
比如:在布隆过滤器中查找"Tencent"时,假设3个哈希函数计算的哈希值为:2、4、6,刚好和其他元素的比特位重叠,此时布隆过滤器告诉该元素存在,但实际该元素是不存在的。
布隆过滤器不能直接支持删除工作,因为在删除一个元素时,可能会影响其他元素。
比如:删除"tencent"元素,如果直接将该元素所对应的二进制比特位置0, “baidu”元素也被删除了,因为这两个元素在多个哈希函数计算出的比特位上刚好有重叠。
一种支持删除的方法:将布隆过滤器中的每个比特位扩展成一个小的计数器,插入元素时给k个计数器(k个哈希函数计算出的哈希地址)加一,删除元素时,给k个计数器减一,通过多占用几倍存储 空间的代价来增加删除操作。
缺陷:
#pragma once #include <iostream> #include <vector> #include <bitset> #include <string> using namespace std; namespace nb { struct BKDRHash { size_t operator()(const string& key) { size_t hash = 0; for (auto ch : key) { hash *= 131; hash += ch; } return hash; } }; struct APHash { size_t operator()(const string& key) { unsigned int hash = 0; int i = 0; for (auto ch : key) { if ((i & 1) == 0) { hash ^= ((hash << 7) ^ (ch) ^ (hash >> 3)); } else { hash ^= (~((hash << 11) ^ (ch) ^ (hash >> 5))); } ++i; } return hash; } }; struct DJBHash { size_t operator()(const string& key) { unsigned int hash = 5381; for (auto ch : key) { hash += (hash << 5) + ch; } return hash; } }; struct JSHash { size_t operator()(const string& s) { size_t hash = 1315423911; for (auto ch : s) { hash ^= ((hash << 5) + ch + (hash >> 2)); } return hash; } }; // 假设N是最多存储的数据个数 // 平均存储一个值,开辟X个位 template<size_t N, size_t X = 6, class K = string, class HashFunc1 = BKDRHash, class HashFunc2 = APHash, class HashFunc3 = DJBHash, class HashFunc4 = JSHash> class BloomFilter { public: void set(const K& key) { size_t hash1 = HashFunc1()(key) % (N * X); size_t hash2 = HashFunc2()(key) % (N * X); size_t hash3 = HashFunc3()(key) % (N * X); size_t hash4 = HashFunc4()(key) % (N * X); _bs.set(hash1); _bs.set(hash2); _bs.set(hash3); _bs.set(hash4); } bool test(const K& key) { size_t hash1 = HashFunc1()(key) % (N * X); size_t hash2 = HashFunc2()(key) % (N * X); size_t hash3 = HashFunc3()(key) % (N * X); size_t hash4 = HashFunc4()(key) % (N * X); // 有一个为0则一定不存在 if (!_bs.test(hash1) || !_bs.test(hash2) || !_bs.test(hash3) || !_bs.test(hash4)) { return false; } // 前面判断不在都是准确,不存在误判 return true; // 可能存在误判,映射几个位置都冲突,就会误判 } private: std::bitset<N* X> _bs; }; void test_bloomfilter1() { // 10:46继续 string str[] = { "apple", "banana", "cherry", "fruit", "peach1","1peach","p1each","p11each","1peach1" }; BloomFilter<10> bf; for (auto& str : str) { bf.set(str); } for (auto& s : str) { cout << bf.test(s) << endl; } cout << endl; srand(time(0)); for (const auto& s : str) { cout << bf.test(s + to_string(rand())) << endl; } } void test_bloomfilter2() { srand(time(0)); const size_t N = 100000; BloomFilter<N> bf; std::vector<std::string> v1; std::string url = "https://www.cnblogs.com/-clq/archive/2012/05/31/2528153.html"; for (size_t i = 0; i < N; ++i) { v1.push_back(url + std::to_string(i)); } for (auto& str : v1) { bf.set(str); } // v2跟v1是相似字符串集,但是不一样 std::vector<std::string> v2; for (size_t i = 0; i < N; ++i) { std::string url = "https://www.cnblogs.com/-clq/archive/2012/05/31/2528153.html"; url += std::to_string(999999 + i); v2.push_back(url); } size_t n2 = 0; for (auto& str : v2) { if (bf.test(str)) { ++n2; } } cout << "相似字符串误判率:" << (double)n2 / (double)N << endl; // 不相似字符串集 std::vector<std::string> v3; for (size_t i = 0; i < N; ++i) { string url = "zhihu.com"; url += std::to_string(i + rand()); v3.push_back(url); } size_t n3 = 0; for (auto& str : v3) { if (bf.test(str)) { ++n3; } } cout << "不相似字符串误判率:" << (double)n3 / (double)N << endl; } }
数的状态:0次、1次和1次以上
template<size_t N> class twobitset { public: void set(size_t x) { if (!_bs1.test(x) && !_bs2.test(x)) // 00 --> 01 0次变成1次 { _bs2.set(x); // 01 } else if (!_bs1.test(x) && _bs2.test(x)) // 01 --> 1次变成1次以上 { _bs1.set(x); _bs2.reset(x); // 10 } // 10 1次以上不做处理 } void PirntOnce() { for (size_t i = 0; i < N; ++i) { // 01是出现一次 if (!_bs1.test(i) && _bs2.test(i)) { cout << i << endl; } } cout << endl; } private: bitset<N> _bs1; bitset<N> _bs2; }; void test_twobitset() { twobitset<100> tbs; int a[] = { 1, 2, 3, 4, 4, 5, 5, 99, 22 }; for (auto e : a) { tbs.set(e); } tbs.PirntOnce(); }
假设这里的整数范围不超过32位整数范围。分别开两个位图,42亿个比特位表示每个整数是否存在,每个位图约0.5G(1GB约10亿字节80亿比特,1MB约100万字节),两个位图都存在的数则为交集。
A i A_i Ai小文件超过1G怎么办?
情况1: 小文件中冲突的ip很多,都是不同的ip,大多数是不重复的。map统计不下。换个字符串哈希函数,递归再切分。
情况2:小文件中冲突的ip很多,大多都是相同的ip,大多数是重复。map可以统计
如何区分这两种情况?
情况1下map会因内存不足,insert失败,new节点抛异常。通过捕获异常就能区分两种情况
精确算法与上面的哈希切割问题解决办法大致相同:
近似算法:
使用 Bloom Filter 算法找到两个文件的交集的步骤:
参考博客:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。