×

Introduction and Implementation of Bloom Filter

It often happens with many of us that when we create an account on some applications like Github, it shows us that the username already exists. You can add some unique things to your username; only then will it be registered. It happens to all of us.

Now it is time to discuss the logic behind this thing. It is logical that if there is more than one person with the same user name, it will be a very problematic situation for the software to manage things. So, we have to use different usernames, but how does the software find out so fast whether the same username exists or not? Maybe you can think of searching algorithms. If you are familiar with the searching algorithm, then you know that it will be very problematic if we use linear search in this case. We can think of binary search but just think of some applications which have millions of users. It will be very bad for them to take 10 or 20 minutes to warn one user about his user name. So here comes the concept of the bloom filter. Hope you are able to understand the background scenario. Now we will discuss in detail about the bloom filter in the rest of this article. Keep reading to learn more about this topic.

What is a Bloom Filter?

To understand bloom filters, you must first understand hashing. A hash function takes input and returns a unique identifier of a fixed length that can be used to identify the input. An element's membership in a set can be determined using a space-effective probabilistic data structure called a Bloom filter.  Efficiency comes at a price because it is probabilistic in nature, which implies that some False Positive outcomes could occur. False positive refers to the possibility of showing that a specific username is already taken when it is not.

Properties of Bloom Filter

  1. Unlike a traditional hash table, a Bloom filter with a fixed size can represent a set with an arbitrary high number of items.
  2.  Bloom filters never give erroneous negative results, such as claiming a username doesn't exist when it actually does.
  3.  Adding a component is always a smart move. However, the false positive rate increases progressively when more elements are added, and when all of the filter's bits are set to 1, all queries get positive results.
  4. Since deleting a single element by clearing bits at indices created by k hash functions may result in the deletion of a few additional items, it is not possible to delete elements from the filter. For instance, if we remove "world" by erasing bits at positions 1, 4, and 7, we might also remove "hero" because the bloom filter asserts that "hero" is not present when bit at position 4 is set to 0.

Implementation of Bloom Filter

#include <bits/stdc++.h>
using namespace std;
int hashfun1(string s, int arrSize)
{
	long long int hashvar = 0;
	for (int i = 0; i < s.size(); i++)
	{
		hashvar = (hashvar + ((int)s[i]));
		hashvar = hashvar % arrSize;
	}
	return hashvar;
}
int hashfun2(string s, int arrSize)
{
	long long int hashvar = 1;
	for (int i = 0; i < s.size(); i++)
	{
		hashvar = hashvar + pow(19, i) * s[i];
		hashvar = hashvar % arrSize;
	}
	return hashvar % arrSize;
}
int hashfun3(string s, int arrSize)
{
	long long int hashvar = 7;
	for (int i = 0; i < s.size(); i++)
	{
		hashvar = (hashvar * 31 + s[i]) % arrSize;
	}
	return hashvar % arrSize;
}
int hashfun4(string s, int arrSize)
{
	long long int hashvar = 3;
	int p = 7;
	for (int i = 0; i < s.size(); i++) {
		hashvar += hashvar * 7 + s[0] * pow(p, i);
		hashvar = hashvar % arrSize;
	}
	return hashvar;
}


bool lookup(bool* arraywithbit, int arrSize, string s)
{
	int a = hashfun1(s, arrSize);
	int b = hashfun2(s, arrSize);
	int c = hashfun3(s, arrSize);
	int d = hashfun4(s, arrSize);


	if (arraywithbit[a] && arraywithbit[b] && arraywithbit[c]
		&& arraywithbit[d])
		return true;
	else
		return false;
}
void insert(bool* arraywithbit, int arrSize, string s)
{
	if (lookup(arraywithbit, arrSize, s))
		cout << s << " is Probably already present" << endl;
	else
	{
		int a = hashfun1(s, arrSize);
		int b = hashfun2(s, arrSize);
		int c = hashfun3(s, arrSize);
		int d = hashfun4(s, arrSize);


		arraywithbit[a] = true;
		arraywithbit[b] = true;
		arraywithbit[c] = true;
		arraywithbit[d] = true;


		cout << s << " inserted" << endl;
	}
}
int main()
{
	bool arraywithbit[100] = { false };
	int arrSize = 20;
	string usname[8]
		= { "eat","tea","tan","ate","nat","tea","tan","bat"
};
	for (int i = 0; i < 8; i++) {
		insert(arraywithbit, arrSize, usname[i]);
	}
	return 0;
}

Output:

eat inserted
tea inserted
tan inserted
ate inserted
nat inserted
tea is Probably already present
tan is Probably already present
bat inserted

Related Topics

Given Two Binary Trees, Check if it is Symmetric

Implementation // creating a C++ program that will help us check whether the two given trees are mirror images of each other.  #include<bits/stdc++.h> using namespace std; /* A given binary tree has a data...

5 minutes read.

Delete a Node without head pointer from the linked list

Delete a Node without head pointer from the linked list This article will explain how to delete a node without a head pointer from the linked list. We have given a...

2 minutes read.

Linear vs Binary Search: Data Structure

Difference Between Linear and Binary Search What is Linear Search? A linear search also referred as a sequential search. It is a way to find an element within a list and it...

3 minutes read.

What is the Use of Segment Trees in Data Structure?

Segment trees Segment trees are also called statistical trees in computer science. They are a type of tree data structure. Segment trees are used to store information regarding segments and intervals....

6 minutes read.

Convert binary tree to a doubly linked list

Implementation //creating a C++ program for the transition of a binary tree into a linked list. #include <iostream> using namespace std; /* Firstly, let’s create a binary tree that will help us in setting...

4 minutes read.

Find the fractional (n/kth) node in the linked list

Find the fractional (n/kth) node in the linked list In this problem, we have given a singly linked list and a number k. Here we need to find the (n/k)th element...

2 minutes read.

Function to Delete a Leaf Node from a Binary Tree

Implementation // We are writing a C++ code to eliminate all the leaves from the given value.  #include <bits/stdc++.h> using namespace std; // creating a new binary tree node struct __nod { int record; struct __nod *Lft,...

4 minutes read.

Array vs Linked List: Data Structure

Data structure: Difference Between Array and Linked List What is Array? An array is a linear data structure that can store similar data items for further processing. The similar data items...

3 minutes read.

Optimal binary search tree using dynamic programming

Implementation // We are creating a presentation where we will present a recursive method of the optimal binary search tree problem.  #include <bits/stdc++.h> using namespace std; //creating a utility function that will help us...

9 minutes read.

AVL tree in data structure c++

AVL tree is generally known as the self-sustained and most balanced tree in the field of a binary search tree. It was also widely known as the height-balanced binary tree....

6 minutes read.

Comb Sort

Brush sort is a fairly direct orchestrating computation at first arranged by Wlodzimierz Dobosiewicz and Artur Borowy in 1980, later rediscovered (and given the name "Combsort") by Stephen Lacey and...

5 minutes read.

Recaman’s Sequence

Recamán's succession repeat connection in arithmetic and software engineering. Since its components are obviously connected with the past components, they are as often as possible characterized utilizing recursion. It takes its...

4 minutes read.

Bubble Sort vs Merge Sort

In this article, we are going to compare two sorting techniques, Bubble sort and Merge Sort. In starting, we will first discuss the idea of sorting an array using bubble...

7 minutes read.

Find out the area between two concentric circles

You have given two values of the radius of two circles. You have to find out the area between these two circles. Let's take an example - For the above diagram,...

3 minutes read.

How to get Better in Data Structures and Algorithms?

Introduction Data structures and algorithms are fundamental computer science concepts that store, organize, and process data efficiently. By understanding different data structures and algorithms and using them effectively, you can become...

19 minutes read.

Post-order traversal in a binary tree

We all know that postorder is a form of tree traversal to visit the tree's nodes, and it helps us reach out to the tree's nodes. Postorder means visiting the...

4 minutes read.

Rearrange a linked list into alternate fashion first and the last element

Rearrange a linked list into alternate fashion first and the last element This article will explain how to rearrange the linked list into alternate fashion first and the last element. Here,...

3 minutes read.

Complete Binary tree

In this article, we will discuss the complete binary tree. But before start discussing the complete binary tree, we should first see a brief description of a binary tree. What is...

7 minutes read.

Queue Implementation using stacks Data Structure

Queue Implementation using stacks In this problem, we have stack data structure which supports only push() and pop() operations. We are required to implement a queue data structure using the instances...

4 minutes read.

Tree terminology in Data structures

Data structures The storage used to organize and store data is known as a data structure, and it is a method where data can be arranged on a computer to be...

6 minutes read.