Hashing
Hashing
HASH FUNCTION
A Hash Function is a function that converts a given numeric or alphanumeric key
to a small practical integer value. The mapped integer value is used as an index in
the hash table. In simple terms, a hash function maps a significant number or string
to a small integer that can be used as the index in the hash table.
The pair is of the form (key, value), where for a given key, one can find a value
using some kind of a “function” that maps keys to values. The key for a given object
can be calculated using a function called a hash function. For example, given an
array A, if i is the key, then we can find the value by simply looking up A[i].
TYPES OF HASH FUNCTIONS
1. Division Method.
2. Mid Square Method.
3. Folding Method.
4. Multiplication Method.
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:
h(K) = k mod M
Here,
k is the key value, and
M is the size of the hash table.
It is best suited that M is a prime number as that can make sure the keys are
more uniformly distributed. The hash function is dependent upon the
remainder of a division.
2. MID SQUARE METHOD:
The mid-square method is a very good hashing method. It involves two steps to
compute the hash value-
Square the value of the key k i.e. k2
Extract the middle r digits as the hash value.
Formula:
h(K) = h(k x k)
Here,
k is the key value.
3. DIGIT FOLDING METHOD:
This method involves two steps:
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 have lesser digits than
the other parts.
Add the individual parts. The hash value is obtained by ignoring the last carry if any.
Formula:
k = k1, k2, k3, k4, ….., kn
s = k1+ k2 + k3 + k4 +….+ kn
h(K)= s
4. MULTIPLICATION METHOD
This method involves the following steps:
Choose a constant value A such that 0 < A < 1.
Multiply the key value with A.
Extract the fractional part of kA.
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.
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.
COLLISION RESOLUTION
TECHNIQUES
There are two types of collision resolution techniques.
Separate chaining (open hashing)
Open addressing (closed hashing)
SEPARATE CHAINING
This method involves making a linked list out of the slot where the collision happened, then adding the
new key to the list. Separate chaining is the term used to describe how this connected list of slots
resembles a chain. It is more frequently utilized when we are unsure of the number of keys to add or
remove.
Time complexity, Its worst-case complexity for searching is o(n), Its worst-case complexity for deletion
is o(n).
Advantages of separate chaining
It is easy to implement.The hash table never fills full, so we can add more elements to the chain.It is
less sensitive to the function of the hashing.
Disadvantages of separate chaining
In this, the cache performance of chaining is not good. Memory wastage is too much in this method.
It requires more space for element links.
OPEN ADDRESSING:
To prevent collisions in the hashing table open, addressing is employed as a
collision-resolution technique. No key is kept anywhere else besides the hash table.
As a result, the hash table’s size is never equal to or less than the number of keys.
Additionally known as closed hashing.
The following techniques are used in open addressing:
1. Linear probing
2. Quadratic probing
3. Double hashing
CHAINING
Keys 42 19 10 12
K mod 5 index keys
0 10
1
2 12 42 adr
3
4 19
LINEAR PROBING
pro key index
Keys: 43, 135, 72, 23, 99, 19, 82 b
19 0
h(k) = K mod 10 2
1
h(k,i)=(h(k)+i) mod 10 72 2
3
43
23 4
2 5
135
82 6
5
7
8
1 99 9
QUADRATIC PROBING
Keys 42 19 10 12
h(k) = K mod 5
h(k,i)=(h(k)+(i)2) mod 5
DOUBLE HASHING
Keys: 20, 34, 45, 70, 56
h1(k) = k mod 11
h2(k)= 8- (k mod 8)
(h1(k)+ih2(k)) mod 11