【数据结构】位图BitMap与布隆过滤器BloomFilter

前端之家收集整理的这篇文章主要介绍了【数据结构】位图BitMap与布隆过滤器BloomFilter前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

首先先看一下下面这个腾讯的面试题:

  • 给40亿个不重复的无符号整数,没排过序。给一个无符号整数,如何快速判断一个数是否在这40亿个数中。【腾讯】


思路一:

最容易想到的解法就是遍历所有的40多亿个整数,然后一个一个判断。但是这个需要花费的内存是多大呢?

大家可以去算一下,这里我就直接给出结果为16G,是不是需要的空间很大啊。如果面试官给出限制条件,要你使用的空间少于多少,遍历的方法就行不通啦。


思路二:

我们可以把一个整形再细分一下,一个int类型就可以编程32个位,每一位用0,1表示当前这个位置上是否存有值,同样是利用哈希存储的方法。只是这样存储的话就可以减少很多的空间了,例如上题使用的内存就可以从16G降到500M的内存。空间的使用率减少了不止一点。


位图的实现

  1. #ifndef__BITMAP_H__
  2. #define__BITMAP_H__
  3. #include<vector>
  4. #include"Common.h"
  5.  
  6. classBitMap
  7. {
  8. public:
  9. BitMap(size_tsize=0)
  10. :_size(0)
  11. {
  12. _a.resize((size>>5)+1);
  13. }
  14.  
  15. //插入数据
  16. voidSet(size_tx)
  17. {
  18. size_tindex=x>>5;
  19. size_tnum=x%32;
  20. //如果当前位置不存在值,直接插入
  21. if(!(_a[index]&(1<<num)))//判断这个位上是不是等0的
  22. {
  23. ++_size;
  24. _a[index]|=(1<<num);//将当前位上的值置成1
  25. }
  26. }
  27.  
  28. voidReset(size_tx)
  29. {
  30. size_tindex=x>>5;
  31. size_tnum=x%32;
  32.  
  33. //判断当前位上的值是不是等于1,等于1删除
  34. if(_a[index]&(1<<num))
  35. {
  36. --_size;
  37. _a[index]&=~(1<<num);//将当前位置成0
  38. }
  39. }
  40.  
  41. boolTest(size_tx)
  42. {
  43. size_tindex=x>>5;
  44. size_tnum=x%32;
  45.  
  46. //如果当前位等于1,那么存在
  47. if(_a[index]&(1<<num))
  48. {
  49. returntrue;
  50. }
  51.  
  52. returnfalse;
  53. }
  54.  
  55. voidResize(size_tsize)
  56. {
  57. _a.resize((size>>5)+1);
  58. }
  59.  
  60. size_tSize()
  61. {
  62. return_size;
  63. }
  64.  
  65. private:
  66. vector<size_t>_a;
  67. size_t_size;//位图上插入了多少值
  68. };
  69.  
  70.  
  71. voidTest()
  72. {
  73. BitMapbm(35);
  74. bm.Set(4);
  75. bm.Set(5);
  76. bm.Set(6);
  77. cout<<"is4Exist?->"<<bm.Test(4)<<endl;
  78. cout<<"is5Exist?->"<<bm.Test(5)<<endl;
  79.  
  80. bm.Reset(5);
  81. cout<<"is4Exist?->"<<bm.Test(4)<<endl;
  82. cout<<"is5Exist?->"<<bm.Test(5)<<endl;
  83. }
  84.  
  85. #endif//__BITMAP_H__


布隆过滤器 Bloom Filter

原理

如果想判断一个元素是不是在一个集合里,一般想到的是将集合中所有元素保存起来,然后通过比较确定。链表、树、散列表(又叫哈希表,Hash table)等等数据结构都是这种思路。但是随着集合中元素的增加,我们需要的存储空间越来越大。同时检索速度也越来越慢。

Bloom Filter 是一种空间效率很高的随机数据结构,Bloom filter 可以看做是对 bit-map 的扩展,它的原理是:

当一个元素被加入集合时,通过 KHash函数将这个元素映射成一个位阵列(Bit array)中的 K 个点,把它们置为 1。检索时,我们只要看看这些点是不是都是 1 就(大约)知道集合中有没有它了:

  • 如果这些点有任何一个 0,则被检索元素一定不在

  • 如果都是 1,则被检索元素可能在


如果只是空洞的说这些原理的话,肯定大家都不知道布隆过滤器有什么用处。布隆过滤器对于单机来说可能用处不是很大,但对于分布式来说就比较有用了。

如主从分布:一个数组过来,我想要知道他是不是在内存中,我们是不是需要一个一个去访问磁盘,判断数据是否存在。但是问题来了访问磁盘的速度是很慢的,所以效率会很低,如果使用布隆过滤器,我们就可以先去过滤器这个集合里面找一下对应的位置的数据是否存在。虽然布隆过滤器有他的缺陷,但是我们能够知道的是当前位置为0是肯定不存在的,如果都不存在,就不需要去访问了。


下面来讲一下布隆过滤器的缺陷

缺陷一:误算率(False Positive)是其中之一。随着存入的元素数量增加,误算率随之增加。但是如果元素数量太少,则使用散列表足矣。所以我们用多个哈希表去 存储一个数据。那么问题又来了,我们是多用一些呢,还是少用一些。如果多用哈希表的话,如上面的题,一个哈希就需要500M,那么放的越多是不是越占内存啊。如果太少的话是不是误算率就高啊,所以取个适中的。下面我的实现是取了五个哈希表(没有什么根据,只是把思路展现出来一下,能够分析出取多少个,那都是大牛们弄出来的算法,我当前水平不够~)


缺陷二:如果当前位置为0肯定不存在,但是为1不一定存在


布隆过滤器的实现:(用了素数表和5个哈希算法)

一、5个哈希算法的实现

  1. #ifndef__COMMON_H__
  2. #define__COMMON_H__
  3.  
  4. size_tGetPrimeSize(size_tsize)
  5. {
  6. staticconstint_PrimeSize=28;
  7. staticconstunsignedlong_PrimeList[_PrimeSize]=
  8. {
  9. 53ul,97ul,193ul,389ul,769ul,1543ul,3079ul,6151ul,12289ul,24593ul,49157ul,98317ul,196613ul,393241ul,786433ul,1572869ul,3145739ul,6291469ul,12582917ul,25165843ul,50331653ul,100663319ul,201326611ul,402653189ul,805306457ul,1610612741ul,3221225473ul,4294967291ul
  10. };
  11.  
  12. for(size_ti=0;i<_PrimeSize;i++)
  13. {
  14. if(_PrimeList[i]>size)
  15. {
  16. return_PrimeList[i];
  17. }
  18. if(_PrimeList[_PrimeSize]==size)
  19. return_PrimeList[_PrimeSize-1];
  20. }
  21. return_PrimeList[_PrimeSize-1];
  22. }
  23.  
  24.  
  25. template<classT>
  26. struct__HashFunc1
  27. {
  28. size_tBKDRHash(constchar*str)
  29. {
  30. registersize_thash=0;
  31. while(size_tch=(size_t)*str++)
  32. {
  33. hash=hash*131+ch;//也可以乘以31、131、1313、13131、131313..
  34. }
  35. returnhash;
  36. }
  37.  
  38. size_toperator()(constT&str)
  39. {
  40. returnBKDRHash(str.c_str());
  41. }
  42.  
  43. };
  44.  
  45.  
  46. template<classT>
  47. struct__HashFunc2
  48. {
  49. size_tSDBMHash(constchar*str)
  50. {
  51. registersize_thash=0;
  52. while(size_tch=(size_t)*str++)
  53. {
  54. hash=65599*hash+ch;
  55. //hash=(size_t)ch+(hash<<6)+(hash<<16)-hash;
  56. }
  57. returnhash;
  58. }
  59.  
  60. size_toperator()(constT&str)
  61. {
  62. returnSDBMHash(str.c_str());
  63. }
  64.  
  65. };
  66.  
  67.  
  68. template<classT>
  69. struct__HashFunc3
  70. {
  71. size_tRSHash(constchar*str)
  72. {
  73. registersize_thash=0;
  74. size_tmagic=63689;
  75. while(size_tch=(size_t)*str++)
  76. {
  77. hash=hash*magic+ch;
  78. magic*=378551;
  79. }
  80. returnhash;
  81. }
  82.  
  83. size_toperator()(constT&str)
  84. {
  85. returnRSHash(str.c_str());
  86. }
  87.  
  88. };
  89.  
  90.  
  91. template<classT>
  92. struct__HashFunc4
  93. {
  94. size_tAPHash(constchar*str)
  95. {
  96. registersize_thash=0;
  97. size_tch;
  98. for(longi=0;ch=(size_t)*str++;i++)
  99. {
  100. if((i&1)==0)
  101. {
  102. hash^=((hash<<7)^ch^(hash>>3));
  103. }
  104. else
  105. {
  106. hash^=(~((hash<<11)^ch^(hash>>5)));
  107. }
  108. }
  109. returnhash;
  110. }
  111.  
  112. size_toperator()(constT&str)
  113. {
  114. returnAPHash(str.c_str());
  115. }
  116.  
  117. };
  118.  
  119.  
  120. template<classT>
  121. struct__HashFunc5
  122. {
  123. size_tJSHash(constchar*str)
  124. {
  125. if(!*str)//这是由本人添加,以保证空字符串返回哈希值0
  126. return0;
  127. registersize_thash=1315423911;
  128. while(size_tch=(size_t)*str++)
  129. {
  130. hash^=((hash<<5)+ch+(hash>>2));
  131. }
  132. returnhash;
  133. }
  134.  
  135. size_toperator()(constT&str)
  136. {
  137. returnJSHash(str.c_str());
  138. }
  139.  
  140. };
  141.  
  142.  
  143.  
  144.  
  145.  
  146.  
  147. #endif//__COMMON_H__

二、布隆过滤器

  1. #ifndef__BLOOMFILTER_H__
  2. #define__BLOOMFILTER_H__
  3. #include<iostream>
  4. usingnamespacestd;
  5. #include<string>
  6. #include"BitMap.h"
  7. #include"Common.h"
  8.  
  9.  
  10. template<classK=string,classHashFunc1=__HashFunc1<K>,classHashFunc2=__HashFunc2<K>,classHashFunc3=__HashFunc3<K>,classHashFunc4=__HashFunc4<K>,classHashFunc5=__HashFunc5<K>>
  11. classBloomFilter
  12. {
  13. public:
  14. BloomFilter(size_tsize=0)
  15. {
  16. _capacity=GetPrimeSize(size);
  17. _bitMap.Resize(_capacity);
  18. }
  19.  
  20. voidSet(constK&key)
  21. {
  22. size_tindex1=HashFunc1()(key);
  23. size_tindex2=HashFunc2()(key);
  24. size_tindex3=HashFunc3()(key);
  25. size_tindex4=HashFunc4()(key);
  26. size_tindex5=HashFunc5()(key);
  27.  
  28. _bitMap.Set(index1%_capacity);//设置为第多少位的数,然后调用位图的Set设置成第几个字节的第几位
  29. _bitMap.Set(index2%_capacity);
  30. _bitMap.Set(index3%_capacity);
  31. _bitMap.Set(index4%_capacity);
  32. _bitMap.Set(index5%_capacity);
  33. }
  34.  
  35. boolTest(constK&key)
  36. {
  37. size_tindex1=HashFunc1()(key);
  38. if(!(_bitMap.Test(index1%_capacity)))//为1不一定存在,为0肯定不存在
  39. returnfalse;
  40.  
  41. size_tindex2=HashFunc2()(key);
  42. if(!(_bitMap.Test(index2%_capacity)))
  43. returnfalse;
  44.  
  45. size_tindex3=HashFunc3()(key);
  46. if(!(_bitMap.Test(index3%_capacity)))
  47. returnfalse;
  48.  
  49. size_tindex4=HashFunc4()(key);
  50. if(!(_bitMap.Test(index4%_capacity)))
  51. returnfalse;
  52.  
  53. size_tindex5=HashFunc4()(key);
  54. if(!(_bitMap.Test(index5%_capacity)))
  55. returnfalse;
  56.  
  57. returntrue;
  58. }
  59.  
  60. protected:
  61. BitMap_bitMap;
  62. size_t_capacity;
  63. };
  64.  
  65.  
  66. voidTestBloomFilter()
  67. {
  68. BloomFilter<>bf(50);
  69. bf.Set("臧");
  70. bf.Set("静");
  71. bf.Set("比特");
  72. bf.Set("peter");
  73. bf.Set("徐");
  74. bf.Set("http://www.cnblogs.com/-clq/archive/2012/05/31/2528153.html");
  75. bf.Set("http://www.cnblogs.com/-clq/archive/2012/05/31/2528155.html");
  76.  
  77. cout<<"Exist?->:"<<bf.Test("臧")<<endl;
  78. cout<<"Exist?->:"<<bf.Test("静")<<endl;
  79. cout<<"Exist?->:"<<bf.Test("peter")<<endl;
  80. cout<<"Exist?->:"<<bf.Test("徐航")<<endl;
  81. cout<<"Exist?->:"<<bf.Test("http://www.cnblogs.com/-clq/archive/2012/05/31/2528153.html")<<endl;
  82. cout<<"Exist?->:"<<bf.Test("http://www.cnblogs.com/-clq/archive/2012/05/31/25288153.html")<<endl;
  83.  
  84. }
  85.  
  86. #endif//__BLOOMFILTER_H__

猜你在找的数据结构相关文章