×

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

Interval Tree

Interval Tree Interval Tree: The concept is to increase a Binary Search Tree self-balancing such as Red Black Tree, and AVL Tree, so that every feature can be completed in time O(Logn). Each Interval...

4 minutes read.

Insertion Sort vs Bubble Sort

In this article, we will see the major differences between Insertion Sort and Bubble Sort. Before that, let’s have a quick overview of what these sorting algorithms are and what’s...

4 minutes read.

Binary Tree to Doubly Linked List

Binary Tree to Doubly Linked List This article will explain how to convert the given binary tree into a Doubly Linked List. The left and right pointers in tree nodes are...

2 minutes read.

Cocktail Sort

C Program executes cocktail sort. Combo sort is a somewhat straightforward arranging calculation initially planned by Wlodzimierz Dobosiewicz and Artur Borowy in 1980, later rediscovered by Stephen Lacey and Richard Box...

5 minutes read.

Priority Queue in Data Structure

Priority Queue A priority queue is a special kind of queue, in priority queue we give some priority to an element and according to this priority an element can be served...

3 minutes read.

Data Structure Prefix to Postfix Conversion

Prefix to Postfix Conversion Prefix: As the name suggests if the operator placed before the operands called the prefix expression.  The form of prefix expression is (operator, operand1, operand2). Example:  *+EF-GH (Infix:...

2 minutes read.

Write Main Difference Between Tree and Graph in Data Structures

Graph: The graph has two sets, which are considered V and E. These vertices are also called nodes, and edges are referred to as arcs connecting any two nodes in a...

4 minutes read.

Spanning Tree

Spanning Tree: The spanning tree is a subset of the graph. It is a non-cyclic graph. If any node in the spanning tree is truncated, the entire graph fails. There are...

10 minutes read.

Insertion Sort in Data Structures

Insertion Sort in C++ Insertion sort is a sorting algorithm that, in each iteration, installs an unsorted element in its proper position Insertion sort operates in a similar way to how we...

3 minutes read.

Binary Tree vs Binary Search Tree: Data Structure

Difference Between Binary Tree and Binary Search Tree What is Binary Tree? A tree which each node can have utmost two children called binary tree. These children are referred as the ‘left...

3 minutes read.

Balanced Binary Tree

A balanced binary tree is just a random nod-based tree with a rule of keeping its height minimum in size to maintain various operations such as insertions, deletions and several...

3 minutes read.

Huffman tree in Data Structures

The Huffman trees in the field of data structures are pretty impressive in their work. They are generally treated as the binary tree, which is linked with the least external...

6 minutes read.

Splay Tree

Splay Tree A splay tree is a self-balanced or self-adjusted binary search tree. We can say, Splay Tree is used in some cases where some elements or data are accessed more...

8 minutes read.

What are the types of Trees in Data Structure

Data structures Data management is called database management. This allows the computer to sort or organize the data for efficient retrieval. A data model is a system used to store, manage,...

6 minutes read.

Perfect Binary Tree

Complete binary trees are an important and general topic in the concept – of tree data structures. Before discussing a complete binary tree, we need to know the concept of...

4 minutes read.

Deletion in Binary Search Tree

Implementation #include <iostream> using namespace std; struct _nod {   int ky;   struct _nod *Lft, *Rt; }; // Creating a node in the binary tree. struct _nod *nw_nod(int Itm) {   struct _nod *temp = (struct _nod *)malloc(sizeof(struct...

4 minutes read.

Equal Sum

Find an element in array such that the sum of left array is equal to the sum of right array You have been given an array of numbers. You have to...

4 minutes read.

Difference between Stack and Queue

In this article, we will learn about the major differences between Stack and Queue data structures. What is a stack? Stack – A stack is an abstract data structure defined as the...

3 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.

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.