Hashing
First of all, we'll figure out what hashing is. Hashing comes out of a word i.e HASH. which means in English chop up, we can imply that change in state
That's exactly what a hash function do. It takes some value and converts into some other value.
In more specific terms "A hash function is a function which when given a key, generates an address in the table or the map".
To generate an address from a key we need a function i.e Hash Function.
A hash function is used to convert a key into an index. Ideally, a hash function should map each key to a specific and unique index. But in reality that is not possible.
So, we need to be selective in terms of hash function. A hash function should :
- distribute the index values of inserted objects uniquely.
- calculate the index quickly.
- have minimum collisions.
Collisions
What collision is ?
In simple terms, a collision is when two things strike together on the same target.
Similarly in programming it means the same with including some technical terms.
In hashing collision means when at particular index we are saving more than one value.
How to deal with that so their are two ways of solving every problem either stop the problem to occur or resolve the problem after it comes
First we try to avoid collisions. This is known as Collision resolution technique
It is the process to find a new location for the same value that a hash function generates
Here are the two most powerful Collision resolution technique:
if key is 1 hash(<key>%2) will return 1
if key is 3 hash(<key>%2) will return 1
In hashing collision means when at particular index we are saving more than one value.
How to deal with that so their are two ways of solving every problem either stop the problem to occur or resolve the problem after it comes
First we try to avoid collisions. This is known as Collision resolution technique
It is the process to find a new location for the same value that a hash function generates
Here are the two most powerful Collision resolution technique:
- Direct Chaining
- Open addressing
Direct Chaining
If a hash function returns same indexs for two keys than we combine the concept of link list with hash.
We create a lilnk list for every index where hash function retruns the same value for two keys.
for ex:
hash function is hash(<key>%2)
Open Addressing
It is also known as closed hashing. Open Addressing is based on probing
It can be of two types
Linear Probing
Suppose using 'x' key we will get 'p' as the result when 'x' is processed by the hash function. And also after processing 'y' also we will get 'p'. Then for the second key that produces 'p' we will find new index by using Linear Probing.
If we encounter a any value at the same index than we rehash the key to find new index and save the value to the new location.
In linear probing we can use the rehash function to be increased by 1 or 2 any constant. Like if some value exists on index '2' and when we hash another key it will produce as index '2' than we increase the index by 1 and save the value to index 3.
*We can also use a function like if index is occupied we will save at n*n (where n is the index).This phenomenon is known as quadratic probing.
That finishes up this time next time we will catch up with the Almighty Hash Table.
See yaa pals..
If we encounter a any value at the same index than we rehash the key to find new index and save the value to the new location.
In linear probing we can use the rehash function to be increased by 1 or 2 any constant. Like if some value exists on index '2' and when we hash another key it will produce as index '2' than we increase the index by 1 and save the value to index 3.
*We can also use a function like if index is occupied we will save at n*n (where n is the index).This phenomenon is known as quadratic probing.
That finishes up this time next time we will catch up with the Almighty Hash Table.
See yaa pals..