data structures
bloom filter
cuckoo hashing
hash functions
computer science

Bloom filter or cuckoo hashing?

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

Bloom filters and cuckoo hashing are two fascinating data structures used to efficiently handle operations related to membership tests and data retrieval. Both hold prominence in computer science for their unique ways of solving common problems typically faced in databases, networking, and other computational fields.

Bloom Filters

Bloom filters are probabilistic data structures that efficiently test whether an element is part of a set. They are highly space-efficient and allow for speedy insertions and queries. However, false positives are possible, meaning the Bloom filter might indicate that an element is present when it is not. Nevertheless, false negatives, where an element is present but is reported absent, are impossible.

How Bloom Filters Work

  1. Initialization: A Bloom filter is initialized as a bit array of size mm.
  2. Hash Functions: Define kk independent hash functions, h1,h2,,hkh_1, h_2, \ldots, h_k, each of which, when given an input, outputs a number ranging between 1 and mm.
  3. Insertion: To add an element xx to the Bloom filter, compute the kk hash values h1(x),h2(x),,hk(x)h_1(x), h_2(x), \ldots, h_k(x). Set the bits at these positions in the bit array to 1.
  4. Query: To check if an element xx is present, compute the same kk hash values. If all bits at the respective positions are set to 1, xx might be in the set; if any are 0, xx is definitely not in the set.

Example

Consider a Bloom filter with a bit array of size 10 and 3 hash functions. If we wish to insert the element "hello", the hash functions might produce the positions [3, 7, 9], thus setting these bits to 1.

To query "hello", these positions are checked. If all are set to 1, it indicates "hello" might be there.

Use Cases

Bloom filters are broadly used in:

  • Databases: To quickly check if a particular row or item exists in a database.
  • Networking: Efficiently filter packets to check if they should be cached.
  • Blockchain: In Bitcoin, for managing lookups in simplified payment verification.

Parameters

Impact

ParameterDescriptionEffect on Performance
mmSize of the bit arrayLarger mm reduces the chance of false positives.
kkNumber of hash functionsMore hash functions reduce false positives but increase the time to add/check elements.

Cuckoo Hashing

Cuckoo hashing is a collision resolution strategy that ensures O(1) worst-case time complexity for lookups and amortized O(1) insertions. The method is named metaphorically after the cuckoo bird, which lays its eggs in other birds' nests, as elements in cuckoo hashing may be "kicked out" and placed elsewhere in the table.

Mechanism

  1. Multiple Hash Tables: Typically employs two hash tables.
  2. Hash Functions: Requires two hash functions such that each element can be placed in one of two possible positions.
  3. Insertion:
    • If a position is available, the element is inserted directly.
    • If occupied, the existing element is "kicked out" and reinserted into its alternate position as determined by the second hash function.
  4. Rehashing: If cyclic movements occur, indicating the current table configuration is insufficient, rehashing is triggered.

Example

For a two-table setup with hash functions h1h_1 and h2h_2, to insert value xx:

  • Calculate h1(x)h_1(x) and check the position in Table 1. If free, place xx here.
  • If occupied, determine the position in Table 2 using h2(x)h_2(x) and attempt to place the displaced element accordingly, potentially causing a chain reaction of relocations.

Advantages and Use Cases

Cuckoo hashing provides constant time complexity for retrievals, making it suitable for applications where fast lookups are essential, such as:

  • Memory-efficient caches.
  • High-speed networking equipment.

Comparison of Bloom Filters and Cuckoo Hashing

FeatureBloom FilterCuckoo Hashing
PurposeProbabilistic membership testCollision resolution in hash tables
Data StructureBit arrayHash
table with possible relocations
False PositivesPossible, but no false negativesNot applicable
Memory UsageVery space efficientRequires more space due to multiple tables
Use CaseDatabases, networking, blockchainMemory-efficient caches, networking equipment
Time ComplexityO(1) for insertions and queries with potential false positivesO(1) in the worst case for lookups; amortized O(1) for insertions

Both Bloom filters and cuckoo hashing provide innovative solutions to specific challenges in computing today, each with its own strengths and considerations to keep in mind when selecting the most suitable approach for any given application.


Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

All Rights Reserved.