Algorithm learning, algorithm Learning Website

Source: Internet
Author: User

Algorithm learning, algorithm Learning Website

  • About Bloom filter
  • Bloom Filter Calculation Method
  • Advantages and disadvantages of Bloom Filter
    • Advantages
    • Disadvantages
  • Illustration

About Bloom filter

Bloom filter is a binary vector data structure proposed by Howard Bloom in 1970. It has good space and time efficiency and is used to detect whether an element is a member of a set. If the check result is yes, the element is not necessarily in the Set; if the check result is no, the element must not be in the set. Therefore, the Bloom filter has a recall rate of 100%. In this way, each detection request returns two conditions: "in the Set (which may be incorrect)" and "not in the Set (definitely not in the Set, it can be seen that the Bloom filter sacrifices the correct rate and time to save space.

The above text is from Baidu encyclopedia

Bloom Filter Calculation Method

If you need to determine whether an element is in a collection, we usually save all the elements, and then compare them to know whether it is in the set. The linked list and tree are based on this idea, when the number of elements in a set increases, the space and time required are linearly increased, and the retrieval speed is getting slower and slower. Bloom filter uses the hash function to map an element to a point on an m-length array. When this point is 1, the element is in the set, otherwise, it is not in the set. The disadvantage of this method is that there may be conflicts when many elements are detected. The solution is to use k hash functions to correspond to k vertices. If all vertices are 1, the element is in the set. If there is 0, the element is not in the set.

Advantages and disadvantages of Bloom Filter
  • The insertion time and query time are both constants.
  • The data is not saved, and the security is good.
Disadvantages
  • The more elements you insert, the greater the error determination.
  • Elements cannot be deleted.
Illustration

For example, we have a simple Bloom Filter structure: (all bits are 0)

      [ 0 0 0 0 0 0 0 0 0 0 ]

First insertaMap1 4Location, changed to 1.

      [ 1 0 0 1 0 0 0 0 0 0 ]

Second insertbMaps the same hash function1 8Location, changed to 1.

      [ 1 0 0 1 0 0 0 1 0 0 ]

This storesa bTwo elements.aWhen it is present or not, two hashes are found1 4Location. It is 1 at the same time. IndicatesaYes.

But if we lookdMap4 8Location, also found at the same time 1. Think there is, this is an error, because now it only storesa bNod.

Below I will write down the code implementation I wrote with C ++, which is relatively simple to implement. I have briefly written the specific Hash algorithm.

First, the header fileHeader.h

//// Header. h // BloomFilter /// Created by Alps on 15/3/19. // Copyright (c) 2015 chen. all rights reserved. // # ifndef BloomFilter_Header_h # define BloomFilter_Header_hclass BitMap {public: BitMap () {bitmap = NULL; size = 0;} BitMap (int size) {bitmap = NULL; bitmap = new char [size]; if (bitmap = NULL) {printf ("ErroR In BitMap Constractor! \ N ") ;}else {memset (bitmap, 0x0, size * sizeof (char); this-> size = size ;}} int initBitMap (int size) {bitmap = NULL; bitmap = new char [size]; if (bitmap = NULL) {printf ("ErroR In BitMap Constractor! \ N "); return 0;} else {memset (bitmap, 0x0, size * sizeof (char); this-> size = size; return this-> size ;}/ ** set the index bit to 1; */int bitmapSet (int index) {int addr = index/8; int addroffset = index % 8; unsigned char temp = 0x1 <addroffset; if (addr> (size + 1) {return 0 ;} else {bitmap [addr] | = temp; return 1 ;}/ ** return if the index in bitmap is 1; */int bitmapGet (int index) {int Addr = index/8; int addroffset = index % 8; unsigned char temp = 0x1 <addroffset; if (addr> (size + 1) {return 0 ;} else {return (bitmap [addr] & temp)> 0? 1: 0;}/** del the index from 1 to 0 */int bitmapDel (int index) {if (bitmapGet (index) = 0) {return 0 ;} int addr = index/8; int addroffset = index % 8; unsigned char temp = 0x1 <addroffset; if (addr> (size + 1) {return 0 ;} else {bitmap [addr] ^ = temp; return 1 ;}} private: char * bitmap; int size ;}; # endif

Save the header file and place it in the project path.
Below isBloomFilter.cppFile.

//// Main. cpp // BloomFilter /// Created by Alps on 15/3/18. // Copyright (c) 2015 chen. all rights reserved. // # include <iostream> # include "Header. h "using namespace std; template <class Type> class BloomFilter {public: BloomFilter (); BloomFilter (int length) {bitmap. initBitMap (length); this-> length = length;} bool Add (const Type & T); bool Contains (const Type & T); int HasH (const Type & T ); private: BitMap bitmap; int length ;}; template <class Type> int BloomFilter <Type >:: HasH (const Type & T) {int temp = (int) T; return temp % length;} template <class Type> bool BloomFilter <Type >:: Add (const Type & T) {int first = HasH (T ); int second = HasH (first); if (bitmap. bitmapSet (first) & bitmap. bitmapSet (second) {return true;} else {return false;} template <class Type> bool BloomFilter <Type >:: Contains (const Type & T) {int first = HasH (T); int second = HasH (first); if (bitmap. bitmapGet (first) & bitmap. bitmapGet (second) {return true;} else {return false;} int main (int argc, const char * argv []) {BloomFilter <int> bloom (10 ); bloom. add (3); if (bloom. contains (3) {printf ("true \ n");} else {printf ("false \ n");} if (bloom. contains (2) {printf ("true \ n");} else {printf ("false \ n");} return 0 ;}

This is my code.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.