Hashing
Hashing
Introduction
Hashing
Advantages:
• Simple to implement.
• Hash table never fills up, we can always add more elements
to the chain.
• The cache performance of chaining is not good as keys are stored using
• Wastage of Space (Some Parts of the hash table are never used)
• If the chain becomes long, then search time can become O(n) in the
worst case
i. Linear Probing
i = {0, 1, ….}
Example
• Example: Let us consider table Size = 7, hash function as Hash(x) = x % 7
and collision resolution strategy to be f(i) = i2 . Insert = 22, 30, and 50.
at slot 1.
at slot 2.
Example
• Step 3: Inserting 50
– Hash(50) = 50 % 7 = 1
– In our hash table slot 1 is already occupied. So, we will search for slot 1+1 2, i.e.
1+1 = 2,
– Again slot 2 is found occupied, so we will search for cell 1+22, i.e.1+4 = 5,
A good hash function may not prevent the collisions completely however it
Here, we will look into different methods to find a good hash function
• Division Method
• Multiplication Method
1. Division Method
• Formula:
• h(K) = k mod M
Here, k is the key value, and M is the size of the hash table.
Division Method
• Example:
– k = 1276
M = 11
h(1276) = 1276 mod 11
=0
Division Method
• Pros:
– This method is quite good for any value of M.
– The division method is very fast since it requires
only a single division operation.
• Cons:
– This method leads to poor performance since
consecutive keys map to consecutive hash values in
the hash table.
– Sometimes extra care should be taken to choose
the value of M.
Multiplication Method
• This method involves the following steps:
• Multiply the result of the above step by the size of the hash
table i.e. M.
• The resulting hash value is obtained by taking the floor of the
result obtained in step 4.
Multiplication Method
• Formula:
• h(K) = floor (M (kA mod 1))
• Here,
M is the size of the hash table.
k is the key value.
A is a constant value.
Multiplication Method
• Example:
• k = 12345
A = 0.357840
M = 100
• h(12345) = floor[ 100 (12345*0.357840 mod 1)]
= floor[ 100 (4417.5348 mod 1) ]
= floor[ 100 (0.5348) ]
= floor[ 53.48 ]
= 53
Multiplication Method
• Pros:
• although there are some values that tend to give better results than
the rest.
• Cons:
• then the whole process of computing the index by the key using
multiplication hashing is very fast.
Mid Square Method
method.
• Formula:
• h(K) = h(k x k)
• Here,
• This is because all digits in the key contribute to generating the middle digits of the squared result.
• The result is not dominated by the distribution of the top digit or bottom digit of the original key
value.
• Cons:
• The size of the key is one of the limitations of this method, as the key is of big size then its square will
• Another disadvantage is that there will be collisions but we can try to reduce collisions.
Digit Folding Method
• Divide the key-value k into a number of parts i.e. k1, k2, k3,….,kn, where
each part has the same number of digits except for the last part that can
• Add the individual parts. The hash value is obtained by ignoring the last
carry if any.
Digit Folding Method
• Formula:
s = k1+ k2 + k3 + k4 +….+ kn
h(K)= s
• Here,
• Example:
• k = 12345
k1 = 12, k2 = 34, k3 = 5
s = k1 + k2 + k3
= 12 + 34 + 5
= 51
h(K) = 51
Digit Folding Method
• Example:
• k = 12345
k1 = 12, k2 = 34, k3 = 5
s = k1 + k2 + k3
= 12 + 34 + 5
= 51
h(K) = 51
Applications of Hashing
including:
databases efficiently.
multiple servers.
Advantages of Hashing
• Fast Access: Hashing provides constant time access to data, making it
faster than other data structures like linked lists and arrays.
operations.
hash table.
Limitations of Hashing:
• Hash Collisions: Hashing can produce the same hash value for
different keys, leading to hash collisions. To handle collisions, we
need to use collision resolution techniques like chaining or open
addressing.