- 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
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Writing C/C++ code efficiently in Competitive programming
- Finding the Parity of a number Efficiently
- Do not use sizeof for array parameters
- Can we use function on left side of an expression in C and C++?
- When do we use Initializer List in C++?
- Use of explicit keyword in C++
- Can we use % operator on floating point numbers?
- How to use getline() in C++ when there are blank lines in input?
- Decimal to octal conversion with minimum use of arithmetic operators
- Why strcpy and strncpy are not safe to use?
- Features and Use of Pointers in C/C++
- How can we use Comma operator in place of curly braces?
- Different ways to use Const with Reference to a Pointer in C++
- How to use make utility to build C projects?`
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.