Hashing
Hashing
Tushar B. Kute,
http://tusharkute.com
Hashing
• Division Method
– The easiest and quickest way to create a hash
value is through division. The k-value is divided
by M in this hash function, and the result is used.
• Formula:
h(K) = k mod M
• (where k = key value and M = the size of the hash
table)
Types of Hash Functions
• Advantages:
– This method is effective for all values of M.
– The division strategy only requires one
operation, thus it is quite quick.
• Disadvantages:
– Since the hash table maps consecutive keys to
successive hash values, this could result in poor
performance.
– There are times when exercising extra caution
while selecting M's value is necessary.
Types of Hash Functions
• Example:
k = 1987
M = 13
h(1987) = 1987 mod 13
h(1987) = 4
Types of Hash Functions
• Advantages:
– This technique works well because most or all of the
digits in the key value affect the result. All of the
necessary digits participate in a process that results in the
middle digits of the squared result.
– The result is not dominated by the top or bottom digits of
the initial key value.
• Disadvantages:
– The size of the key is one of the limitations of this system;
if the key is large, its square will contain twice as many
digits.
– Probability of collisions occurring repeatedly.
Types of Hash Functions
• Example:
k = 60
Therefore,
k=kxk
k = 60 x 60
k = 3600
Thus,
h(60) = 60
Types of Hash Functions
• Folding Method
• The process involves two steps:
– With the exception of the last component, which
may have fewer digits than the other parts, the
key-value k should be divided into a
predetermined number of pieces, such as k1, k2,
k3,..., kn, each having the exact same amount of
digits.
– Add each element individually. The hash value is
calculated without taking into account the final
carry, if any.
Types of Hash Functions
• Formula:
k = k1, k2, k3, k4, ….., kn
s = k1+ k2 + k3 + k4 +….+ kn
h(K)= s
(Where, s = addition of the parts of key k)
Types of Hash Functions
• Advantages:
– Creates a simple hash value by precisely splitting
the key value into equal-sized segments.
– Without regard to distribution in a hash table.
• Disadvantages:
– When there are too many collisions, efficiency
can occasionally suffer.
Types of Hash Functions
• Example:
k = 12345
k1 = 67; k2 = 89; k3 = 12
Therefore,
s = k1 + k2 + k3
s = 67 + 89 + 12
s = 168
Types of Hash Functions
• Multiplication Method
– Determine a constant value. A, where (0, A, 1)
– Add A to the key value and multiply.
– Consider kA's fractional portion.
– Multiply the outcome of the preceding step by M,
the hash table's size.
• Formula:
h(K) = floor (M (kA mod 1))
(Where, M = size of the hash table, k = key value and
A = constant value)
Types of Hash Functions
• Advantages:
– Any number between 0 and 1 can be applied to
it, however, some values seem to yield better
outcomes than others.
• Disadvantages:
– The multiplication method is often appropriate
when the table size is a power of two since
multiplication hashing makes it possible to
quickly compute the index by key.
Types of Hash Functions
• Example:
k = 5678
A = 0.6829
M = 200
Now, calculating the new value of h(5678):
h(5678) = floor[200(5678 x 0.6829 mod 1)]
h(5678) = floor[200(3881.5702 mod 1)]
h(5678) = floor[200(0.5702)]
h(5678) = floor[114.04]
h(5678) = 114
So, with the updated values, h(5678) is 114.
Hash Collision
Web Resources
https://mitu.co.in
@mituskillologies http://tusharkute.com @mituskillologies
contact@mitu.co.in
tushar@tusharkute.com