Hashing Techniques
Hashing Techniques
Hashing Techniques
This technique is used for storing and retrieving the data from the database.
It is also known as mapping technique because, we try to save larger value at smaller values of index.
Hash Table:
Hash Function:
Hash function is the function by which we calculate the index number, where we have to store a particular
data.
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.
This is how Hashing data structure came into play. With the introduction of the Hash data
structure, it is now possible to easily store data in constant time and retrieve them in
constant time as well.
Components of Hashing
There are majorly three components of hashing:
1. Key: A Key can be anything string or integer which is fed as input in the
hash function.
1. Division Method:
This is the most simple and easiest method to generate a hash value. The hash
function divides the value k by M and then uses the remainder obtained.
Formula:
2. Mid Square Method:
The mid-square method is a very good hashing method. It involves two steps
to compute the hash value-
1. Square the value of the key k i.e. k 2
2. Extract the middle r digits as the hash value.
Formula:
3. Folding Method:
It breaks up a key value into precise segments that are added to form a hash
value.
Explanation:
Example 1: The task is to fold the key 123456789 into a Hash Table of ten spaces (0
through 9).
It is given that the key, say X is 123456789 and the table size (i.e., M =
10).
Since it can break X into three parts in any order. Let’s divide it evenly.
Therefore, a = 123, b = 456, c = 789.
Now, H(x) = (a + b + c) mod M i.e., H(123456789) =(123 + 456 + 789)
mod 10 = 1368 mod 10 = 8.
Hence, 123456789 is inserted into the table at address 8.
Collision Resolution:
When the two different values have the same value, then the problem occurs
between the two values, known as a collision. In the above example, the value is
stored at index 6. If the key value is 26, then the index would be:
h(26) = 26%10 = 6
Therefore, two values are stored at the same index, i.e., 6, and this leads to the
collision problem. To resolve these collisions, we have some techniques known as
collision resolution techniques.
These are:
1. Open Hashing
2. Closed Hashing
Open Hashing
In Open Hashing, one of the methods used to resolve the collision is known as a
chaining method.
In open hashing, each hash table slot, also known as a bucket, contains a linked list
of elements that hash to the same slot.
The term chaining is used to describe the process because the linked list is used,
which is like a chain of elements.
Closed Hashing:
Like open hashing, closed hashing is also a technique used for collision resolution in
hash tables. Unlike open hashing, where collisions are resolved by chaining elements
in separate chains, closed hashing aims to store all elements directly within the hash
table itself without the use of additional data structures like linked lists.
In closed hashing, when a collision occurs, and a hash slot is already occupied, the
algorithm probes for the next available slot in the hash table until an empty slot is
found.
Linear Probing
Linear probing is one of the forms of open addressing. As we know that each cell in
the hash table contains a key-value pair, so when the collision occurs by mapping a
new key to the cell already occupied by another key, then linear probing technique
searches for the closest free locations and adds a new key to that empty cell. In this
case, searching is performed sequentially, starting from the position where the
collision occurs till the empty cell is not found.
The next key value is 13. The index value associated with this key value is 9 when
hash function is applied. The cell is already filled at index 9. When linear probing is
applied, the nearest empty cell to the index 9 is 0; therefore, the value 13 will be
added at the index 0.
Quadratic Probing:
Let hash(x) be the slot index computed using the hash function.
For example: Let us consider a simple hash function as “key mod 7” and sequence
of keys as 50, 700, 76, 85, 92, 73, 101
Double Hashing: