Problem with Hashing

If we consider the above example, the hash function we used is the sum of the letters, but if we examined the hash function closely then the problem can be easily visualized that for different strings same hash value is begin generated by the hash function. 

For example: {“ab”, “ba”} both have the same hash value, and string {“cd”,”be”} also generate the same hash value, etc. This is known as collision and it creates problem in searching, insertion, deletion, and updating of value. 

Hashing Notes for GATE Exam [2024]

Hashing is a fundamental concept in computer science and plays a pivotal role in various algorithms and data structures. Aspiring candidates preparing for the GATE Exam 2024 must grasp the intricacies of hashing to tackle complex problem-solving scenarios efficiently. These notes aim to provide a concise yet comprehensive overview of hashing, covering essential concepts that are likely to be tested in the GATE examination.

Table of Content

  • Introduction to Hashing
  • Need for Hash data structure
  • Components of Hashing
  • How does Hashing work?
  • What is a Hash function?
  • Problem with Hashing
  • What is collision?
  • How to handle Collisions?
  • Separate Chaining
  • Open Addressing
  • What is meant by Load Factor in Hashing?
  • What is Rehashing?
  • Applications of Hash Data structure
  • Real-Time Applications of Hash Data structure
  • Advantages of Hash Data structure
  • Disadvantages of Hash Data structure
  • MCQ of Hashing

Similar Reads

Introduction to Hashing

Hashing refers to the process of generating a fixed-size output from an input of variable size using the mathematical formulas known as hash functions. This technique determines an index or location for the storage of an item in a data structure....

Need for Hash data structure

Every day, the data on the internet is increasing multifold and it is always a struggle to store this data efficiently. In day-to-day programming, this amount of data might not be that big, but still, it needs to be stored, accessed, and processed easily and efficiently. A very common data structure that is used for such a purpose is the Array data structure. Now the question arises if Array was already there, what was the need for a new data structure! The answer to this is in the word “efficiency“. Though storing in Array takes O(1) time, searching in it takes at least O(log n) time. This time appears to be small, but for a large data set, it can cause a lot of problems and this, in turn, makes the Array data structure inefficient....

Components of Hashing

There are majorly three components of hashing:...

How does Hashing work?

Suppose we have a set of strings {“ab”, “cd”, “efg”} and we would like to store it in a table....

What is a Hash function?

The hash function creates a mapping between key and value, this is done through the use of mathematical formulas known as hash functions. The result of the hash function is referred to as a hash value or hash. The hash value is a representation of the original string of characters but usually smaller than the original. For example: Consider an array as a Map where the key is the index and the value is the value at that index. So for an array A if we have index i which will be treated as the key then we can find the value by simply looking at the value at A[i].simply looking up A[i]....

Problem with Hashing

If we consider the above example, the hash function we used is the sum of the letters, but if we examined the hash function closely then the problem can be easily visualized that for different strings same hash value is begin generated by the hash function....

What is collision?

The hashing process generates a small number for a big key, so there is a possibility that two keys could produce the same value. The situation where the newly inserted key maps to an already occupied, and it must be handled using some collision handling technology....

How to handle Collisions?

There are mainly two methods to handle collision:...

Separate Chaining

The idea is to make each cell of the hash table point to a linked list of records that have the same hash function value. Chaining is simple but requires additional memory outside the table....

Open Addressing

In open addressing, all elements are stored in the hash table itself. Each table entry contains either a record or NIL. When searching for an element, we examine the table slots one by one until the desired element is found or it is clear that the element is not in the table....

What is meant by Load Factor in Hashing?

The load factor of the hash table can be defined as the number of items the hash table contains divided by the size of the hash table. Load factor is the decisive parameter that is used when we want to rehash the previous hash function or want to add more elements to the existing hash table. It helps us in determining the efficiency of the hash function i.e. it tells whether the hash function which we are using is distributing the keys uniformly or not in the hash table. Load Factor = Total elements in hash table/ Size of hash table...

What is Rehashing?

As the name suggests, rehashing means hashing again. Basically, when the load factor increases to more than its predefined value (the default value of the load factor is 0.75), the complexity increases. So to overcome this, the size of the array is increased (doubled) and all the values are hashed again and stored in the new double-sized array to maintain a low load factor and low complexity....

Applications of Hash Data structure

Hash is used in databases for indexing. Hash is used in disk-based data structures. In some programming languages like Python, JavaScript hash is used to implement objects....

Real-Time Applications of Hash Data structure

Hash is used for cache mapping for fast access to the data. Hash can be used for password verification. Hash is used in cryptography as a message digest. Rabin-Karp algorithm for pattern matching in a string.  Calculating the number of different substrings of a string....

Advantages of Hash Data structure

Hash provides better synchronization than other data structures. Hash tables are more efficient than search trees or other data structures Hash provides constant time for searching, insertion, and deletion operations on average....

Disadvantages of Hash Data structure

Hash is inefficient when there are many collisions. Hash collisions are practically not avoided for a large set of possible keys. Hash does not allow null values....

MCQ of Hashing

Question 1: What is the probability of a collision when hashing n keys into a hash table of size m, assuming that the hash function produces a uniform random distribution? (A) O(1/n)(B) O(n/m)(C) O(log n)(D) O(m/n) Correct Answer: (C)Explanation: The probability of a collision occurring is dependent on the number of items hashed (n) and the size of the hash table (m). As the number of items increases, the probability of a collision also increases. However, as the size of the hash table increases, the probability decreases. Therefore, the probability of a collision can be estimated as O(n/m)....

Contact Us