Module 5 Hashing
Module 5 Hashing
(BCS304)
Dr. Hemavathi P
Associate Professor
Dept. of CSE
BIT
Text Books
1. Ellis Horowitz and Sartaj Sahni,
Fundamentals of Data Structures in C, 2nd
Ed, Universities Press, 2014.
Module 5:
Hashing
Outline
Hashing
Hash Table organizations,
Hashing Functions,
Dynamic Hashing.
Hashing
• There are several searching techniques like linear search, binary search, search trees etc.
• In these techniques, time taken to search any particular element depends on the total
number of elements.
• Linear Search takes O(n) time to perform the search in unsorted arrays consisting of n
elements.
• Binary Search takes O(logn) time to perform the search in sorted arrays consisting of n
elements.
• It takes O(logn) time to perform the search in Binary Search Tree consisting of n elements.
s slots
0 1 s-1
0 . . .
1
b buckets
. . .
. . .
. . .
. . .
b-1
Properties of a good hash function
1. Low cost
• Execution cost and searching cost should be less
2. Determinism
• Hash procedure must be deterministic i.e same hash value must be generated
for a given input value excluding time of day and memory address of the
object
3. Uniformity
• Must map the keys as evenly as possible over its output range which
minimizes the number of collisions
Types of Hash Functions
• There are various types of hash functions available such as-
• Division Method
• Mid Square Method
• Folding Method
• Multiplication Method
h(K) = h(k x k)
Here,
k is the key value.
• The performance of this method is good as most or all digits of the key
value contribute to the result. 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 double the number of digits.
• Another disadvantage is that there will be collisions but we can try to
reduce collisions.
3. 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 have lesser
digits than the other parts.
• Add the individual parts. The hash value is obtained by ignoring the last carry if
any.
Formula:
Here,
s is obtained by adding the parts of the key k
Example:
k = 12345
k1 = 12, k2 = 34, k3 = 5
s = k1 + k2 + k3
= 12 + 34 + 5
= 51
h(K) = 51
Note:
The number of digits in each part varies depending upon the size of the hash
table. Suppose for example the size of the hash table is 100, then each part
must have two digits except for the last part which can have a lesser number
of digits.
4. Multiplication Method
This method involves the following steps:
1.Choose a constant value A such that 0 < A < 1.
2.Multiply the key value with A.
3.Extract the fractional part of kA.
4.Multiply the result of the above step by the size of the hash table
i.e. M.
5.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.
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
Pros:
• The advantage of the multiplication method is that it can work with any
value between 0 and 1, although there are some values that tend to
give better results than the rest.
Cons:
• The multiplication method is generally suitable when the table size is
the power of two, then the whole process of computing the index by the
key using multiplication hashing is very fast.
Types of Hashing
• Static Hashing
• Dynamic Hashing
Static Hashing
• It is a hashing technique that enables users to lookup a definite data
set. Meaning, the data in the directory is not changing, it is "Static" or
fixed. In this hashing technique, the resulting number of data buckets
in memory remains constant.
• Operations Provided by Static Hashing
• Delete − Search a record address and delete a record at the same address or
delete a chunk of records from records for that address in memory.
• Insertion − While entering a new record using static hashing, the hash
function (h) calculates bucket address "h(K)" for the search key (k), where the
record is going to be stored.
• Search − A record can be obtained using a hash function by locating the
address of the bucket where the data is stored.
• Update − It supports updating a record once it is traced in the data bucket.
Static Hashing
• Advantages of Static Hashing
• Static hashing is advantageous in the following ways −
• Offers unparalleled performance for small-size databases.
• Allows Primary Key value to be used as a Hash Key.
• Disadvantages of Static Hashing
• Static hashing comes with the following disadvantages −
• It cannot work efficiently with the databases that can be scaled.
• It is not a good option for large-size databases.
• Bucket overflow issue occurs if there is more data and less memory.
Dynamic Hashing
• It is a hashing technique that enables users to lookup a dynamic data
set. Means, the data set is modified by adding data to or removing the
data from, on demand hence the name ‘Dynamic’ hashing. Thus, the
resulting data bucket keeps increasing or decreasing depending on the
number of records.
• In this hashing technique, the resulting number of data buckets in
memory is ever-changing.
• Operations Provided by Dynamic Hashing
• Delete − Locate the desired location and support deleting data (or a chunk of
data) at that location.
• Insertion − Support inserting new data into the data bucket if there is a space
available in the data bucket.
• Query − Perform querying to compute the bucket address.
• Update − Perform a query to update the data.
Dynamic Hashing
• Advantages of Dynamic Hashing
• Dynamic hashing is advantageous in the following ways −
• It works well with scalable data.
• It can handle addressing large amount of memory in which data size is always
changing.
• Bucket overflow issue comes rarely or very late.
• Disadvantages of Dynamic Hashing
• Dynamic hashing comes with the following disadvantage −
• The location of the data in memory keeps changing according to the bucket
size. Hence if there is a phenomenal increase in data, then maintaining the
bucket address table becomes a challenge.
Differences between Static and Dynamic Hashing
Collision in Hashing
• Hash function is used to compute the hash value for a key.
• Hash value is then used as an index to store the key in the hash table.
• Hash function may return the same hash value for two or more keys.
• When the hash value of a key maps to an already occupied bucket of
the hash table, it is called as a Collision.
Collision Resolution Techniques
• Collision Resolution Techniques are the techniques used for resolving
or handling the collision.
Collision Resolution
Techniques
Linear Probing
Quadratic Probing
Double Hashing
Separate Chaining
• To handle the collision,
• This technique creates a linked list to the slot for which collision occurs.
• The new key is then inserted in the linked list.
• These linked lists to the slots appear like chains.
• That is why, this technique is called as separate chaining.
Example-Separate Chaining
• Using the hash function ‘key mod 7’, insert the following sequence of
keys in the hash table-
• 50, 700, 76, 85, 92, 73 and 101
Step-1
• Draw an empty hash table.
• For the given hash function, the possible range of hash values is [0, 6].
• So, draw an empty hash table consisting of 7 buckets as-
Step-2
• Insert the given keys in the hash table one by one.
• The first key to be inserted in the hash table = 50.
• Bucket of the hash table to which key 50 maps = 50 mod 7 = 1.
• So, key 50 will be inserted in bucket-1 of the hash table as-
Step-3
• The next key to be inserted in the hash table = 700.
• Bucket of the hash table to which key 700 maps = 700 mod 7 = 0.
• So, key 700 will be inserted in bucket-0 of the hash table as-
Step-4
• The next key to be inserted in the hash table = 76.
• Bucket of the hash table to which key 76 maps = 76 mod 7 = 6.
• So, key 76 will be inserted in bucket-6 of the hash table as-
Step-5
• The next key to be inserted in the hash table = 85.
• Bucket of the hash table to which key 85 maps = 85 mod 7 = 1.
• Since bucket-1 is already occupied, so collision occurs.
• Separate chaining handles the collision by creating a linked list to bucket-1.
• So, key 85 will be inserted in bucket-1 of the hash table as-
Step-6
• The next key to be inserted in the hash table = 92.
• Bucket of the hash table to which key 92 maps = 92 mod 7 = 1.
• Since bucket-1 is already occupied, so collision occurs.
• Separate chaining handles the collision by creating a linked list to bucket-1.
• So, key 92 will be inserted in bucket-1 of the hash table as-
Step-7
• The next key to be inserted in the hash table = 101.
• Bucket of the hash table to which key 101 maps = 101 mod 7 = 3.
• Since bucket-3 is already occupied, so collision occurs.
• Separate chaining handles the collision by creating a linked list to bucket-3.
• So, key 101 will be inserted in bucket-3 of the hash table as-
Algorithm to insert an item using
Chaining Approach
• In case of collision,
• Probing is performed until an empty bucket is found.
• Once an empty bucket is found, the key is inserted.
• Probing is performed in accordance with the technique used for open
addressing.
1. Linear Probing
• In linear probing,
• When collision occurs, we linearly probe for the next bucket.
• We keep probing until an empty bucket is found.
• Advantage-
• It is easy to compute.
• Disadvantage-
For(i=j=0;i<10;i++)
{ temp=b[i];
while(temp!=NULL)
{ a[j++]=temp->info;
temp=temp->link;
}
}
}