Count-Min Sketch in C++15 May 2025 | 3 min read Introduction:The Count-Min Sketch is a probabilistic data structure used for approximate counting queries in large data streams. It efficiently estimates the frequency of elements in a stream of data while using limited memory space. In essence, the Count-Min Sketch consists of a two-dimensional array of counters. Hash functions are employed to map elements of the stream to positions in this array. Multiple hash functions are typically used, each determining a different column in the array. When an element arrives in the stream, its corresponding counters in each row are incremented. The main idea behind the Count-Min Sketch is that by using multiple hash functions and maintaining multiple counters per element, collisions are inevitable, but the minimum value across all counters for any given element tends to provide a good approximation of its true frequency. By adjusting the width and depth of the array, users can control the trade-off between accuracy and memory usage. Program:Output: Count of item 42: 7 Count of item 55: 3 Explanation: Count-Min Sketch Class: The code defines a C++ class named CountMinSketch. This class represents the Count-Min Sketch data structure.
width and depth: These variables represent the width and depth of the Count-Min Sketch, controlling its memory usage and accuracy. counts: This is a two-dimensional vector representing the array of counters in the sketch. hash_functions: This vector holds instances of the std::hash<int> function, used for hashing elements.
The constructor initializes the width, depth, and counts array. It also populates the hash_functions vector with the depth number of hash functions.
The update method takes an item and its count as input. It hashes the item using each hash function and updates the corresponding counters in the count's array.
The query method takes an item as input and returns an estimate of its count. It hashes the item using each hash function and retrieves the minimum count across all hash functions.
Inside the main function, an instance of CountMinSketch is created with specified width and depth. The sketch is updated with counts for two items: 42 and 55. The query method is called to estimate the counts of these items, and the results are printed. Complexity analysis:Time Complexity: Update Operation: The time complexity of updating the sketch with an item's count is O(d), where ? d is the depth of the sketch. This is because, for each hash function (of which there are d), the update involves a constant-time operation to increment the corresponding counter. Query Operation: The time complexity of querying the sketch for an item's count is also O(d). Similar to the update operation, for each hash function, the query involves accessing the corresponding counter and finding the minimum value. Space Complexity: The space complexity of the Count-Min Sketch is determined by the size of its data structure, which consists of:
|
How to Capture std::vector in Lambda Function in C++
? Programmers can define an inline function anywhere in the code thanks to C++'s lambda functions. They are also capable of capturing objects outside of their definition. We'll look at how to use C++ lambda functions to capture a std::vector object in this post. Capture std::vector in Lambda...
2 min read
Inner Reducing Pattern printing in C++
Introduction: Pattern printing is a fundamental concept in programming that helps improve logical thinking and understanding of nested loops. One specific type of pattern is the inner reducing pattern, where the number of elements in each row decreases progressively as you move downward. In this pattern, you...
11 min read
std::not_fn function in C++
The std::not_fn utility in C++ is a component of the header, and introduced in C++17. By generating function objects that negate the outcomes of other callable objects (such functions, function pointers, lambdas, or functors), it plays a very specialized and practical role in functional programming. We frequently...
4 min read
std::ranges::fold_left_first_with_iter and std::ranges::fold_left_first_with_iter_result in C++
In this article, we will discuss the with their characteristics and examples. Std::ranges::fold_left_first_with_iter: Use the C++ function std::ranges::fold_left_first_with_iter to begin with the first element and perform a left-fold (or reduction) operation on a range. It combines items sequentially from left to right using some pre-specified binary operation....
7 min read
Strassen's algorithm in C++
Introduction: Strassen's algorithm, conceived by Volker Strassen in 1969, revolutionized matrix multiplication by introducing an efficient approach, particularly beneficial for large matrices. Unlike the standard multiplication algorithm, Strassen's method strategically diminishes the number of required multiplications. The core concept involves expressing the matrix product in the form...
13 min read
std::is_destructible in C++
In this article, we will discuss the std::is_destructable in C++ with its syntax and examples. What is the std::is_destructible? In C++, a type trait known as std::is_destructible function. It helps to determine if a certain type can be destroyed with a delete operator. It is defined in <type_traits>...
3 min read
Rule of Three in C++
In C++ programming, effectively managing resources is essential to developing robust and maintainable applications Indeed, C++ programs that involve classes that manage dynamic resources, such as memory, file descriptors, network sockets, or any other system-level handles, are a typical case. However, without proper care, these...
19 min read
std::uniform_real_distribution min() method in C++
This method is mainly used to get the minimum possible values that this uniform_real_distribution can generate. The <random> header has to be included for using this function in the program. The <random> header will be a good source for generating random numbers. One of its components...
4 min read
std::ranlux48_base function in C++
PRNGs are mainly used in simulative, intimidating, cryptographic and statistical studies that require pseudorandom sources. There are many tools for generating random numbers in the C standard library, all of which can be found in the <random> library. These tools are not actually random. They are...
10 min read
How to Initialize Vector in C++?
A vector can store multiple data values like arrays, but they can only store object references and not primitive data types. They store an object's reference means that they point to the objects that contain the data, instead of storing them. Unlike an array, vectors...
4 min read
We request you to subscribe our newsletter for upcoming updates.

We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India