How to use unordered_map efficiently in C++
Pre-requisite: unordered_set, unordered_map
C++ provides std::unordered_set and std::unordered_map to be used as a hash set and hash map respectively. They perform insertion/deletion/access in constant average time.
- However, the worst-case complexity is O(n2).
- The reason is that the unordered_map store’s key-value pair by taking the modulo of input value by a prime number and then stores it in a hash table.
- When the input data is big and input values are multiples of this prime number a lot of collisions take place and may cause the complexity of O(n2).
- Depending on the compiler the prime number maybe 107897 or 126271.
Example 1: If we insert multiples of the above two prime numbers and compute execution time. One of the prime numbers takes a much longer time than the other.
for 107897 : 2.261 seconds for 126271 : 0.024 seconds
Clearly, for one of the prime numbers, the time complexity is O(n2).
The standard inbuilt hash function on which unordered_map works is similar to this:
The above function can produce numerous collisions. The keys inserted in HashMap are not evenly distributed, and after inserting numerous prime multiples, further insertion causes the hash function to reallocate all previous keys to new slots hence making it slow. So, the idea is that we have to randomize the hash function.
The idea is to use a method so that the keys in our hashmap are evenly distributed. This will prevent collisions to take place. For this, we use Fibonacci numbers. The golden ratio related to the Fibonacci sequence (Phi = 1.618) has a property that it can subdivide any range evenly without looping back to the starting position.
We can create our own simple hash function. Below is the hash function:
Basically, the above hashing function generates random hash values to store keys. To know more about this please refer to this article Fibonacci hashing.
Example 2: Using the above hashing function, the program runs very quickly.
for 107897 : 0.025 seconds for 126271 : 0.024 seconds
Reserving space before hand
By default, the capacity of unordered_map is 16 and a hash table is created for this. But every time, when threshold is reached, the capacity of the unordered_map is doubled and all the values are rehashed according to new hash table.
So, we can reserve the capacity beforehand according to our input size by using .reserve() method.
1024 can be replaced by any int value according to input size. This prevents rehashing and dynamic allocation which makes program more efficient.
max_load_factor of unordered_map determines the probability of collision. Default value is set to 1.
By setting it to a lower value like 0.25 can decrease the probability of collisions by great extent.
Example : Using above two method can make umap faster :
for 107897 : 0.029 seconds for 126271 : 0.026 seconds
Please Login to comment...