20C23027 Prac4
20C23027 Prac4
20C23027 Prac4
Practical 04
Aim: Python Implementation of Hash Table using Separate Chaining
Theory:
Hash table is a data structure that is used to store a large amount of data, which can be accessed in
O(1) time by operations such as search, insert and delete.
Hashing is an important data structure which is designed to use a special function called the Hash
Function which is used to map a given value with a particular key for faster access of elements. The
efficiency of mapping depends on the efficiency of the hash function used.
Example:
h(large_value) = large_value % m
Here h() is the required hash function and ‘m’ is the size of the hash table. For large values, hash
functions produce value in a given range.
1|Page 2 0 C 2 3 0 2 7 – P u r v i Va g h e l a
ITM SLS Baroda University
School of Computer Science Engineering & Technology
BTech Semester IV AI and CS and
BTech Semester IV CSE and IT
Batch 2020-24
Data Structure & Algorithm II Practical Lab Journal
Collision Handling:
If we know the keys beforehand, then we have can have perfect hashing. In perfect hashing, we do
not have any collisions. However, If we do not know the keys, then we can use the following
methods to avoid collisions:
Chaining
Open Addressing (Linear Probing, Quadratic Probing, Double Hashing)
Chaining
While hashing, the hashing function may lead to a collision that is two or more keys are mapped to
the same value. Chain hashing avoids collision. The idea is to make each cell of hash table point to
a linked list of records that have same hash function value.
Note: In Linear Probing, whenever a collision occurs, we probe to the next empty slot. While in
Quadratic Probing, whenever a collision occurs, we probe for i^2th slot in the ith iteration and we
keep probing until an empty slot in the hashtable is found.
The performance of hashing is evaluated on the basis that each key is equally likely to be hashed for
any slot of the hash table.
2|Page 2 0 C 2 3 0 2 7 – P u r v i Va g h e l a
ITM SLS Baroda University
School of Computer Science Engineering & Technology
BTech Semester IV AI and CS and
BTech Semester IV CSE and IT
Batch 2020-24
Data Structure & Algorithm II Practical Lab Journal
for i in range(len(hashTable)):
print(i, end = " ")
for j in hashTable[i]:
print("-->", end = " ")
print(j, end = " ")
print()
# Creating Hashtable as
# a nested list.
HashTable = [[] for _ in range(10)]
hash_key = Hashing(keyvalue)
Hashtable[hash_key].append(value)
# Driver Code
insert(HashTable, 10, 'Allahabad')
insert(HashTable, 25, 'Mumbai')
insert(HashTable, 20, 'Mathura')
insert(HashTable, 9, 'Delhi')
3|Page 2 0 C 2 3 0 2 7 – P u r v i Va g h e l a
ITM SLS Baroda University
School of Computer Science Engineering & Technology
BTech Semester IV AI and CS and
BTech Semester IV CSE and IT
Batch 2020-24
Data Structure & Algorithm II Practical Lab Journal
display_hash (HashTable)
Output:
Advantages
1. Separate Chaining is one of the simplest methods to implement and understand.
2. We can add any number of elements to the chain.
3. It is frequently used when we don't know about the number of elements and the number of
keys that can be inserted or deleted.
Disadvantages
1. The keys in the hash table are not evenly distributed.
2. Some amount of wastage of space occurs.
3. The complexity of searching becomes O(n) in the worst case when the chain becomes long.
4|Page 2 0 C 2 3 0 2 7 – P u r v i Va g h e l a