Asgn 1
Asgn 1
Title: Consider telephone book database of N clients. Make use of a hash table implementation to quickly look up
client‘s telephone number.
Make use of two collision Handling Techniques (like Linear Probing, Quadratic probing etc.) and compare
them using number of comparisons required to find a set of telephone number.
Objective:
Understanding Hashing Concept.
Theory:
A hash table, or a hash map, is a data structure that associates keys (names) with values (attributes).
– Look-Up Table
– Dictionary
– Cache
– Extended Array
The ideal hash table structure is merely an array of some fixed size, containing the items. A stored item
needs to have a data member, called key, that will be used in computing the index value for the item
• Key could be an integer, a string, etc
• e.g. a name or Id that is a part of a large employee structure
• The size of the array is TableSize
The items that are stored in the hash table are indexed by values from 0 to TableSize – 1. Each key is
mapped into some number in the range 0 to TableSize – 1. The mapping is called a hash function.
According to problem statement, every client may have attributes like client id, client name, client address,
client telephone number, etc.
We apply hash function (specifically open addressing technique like linear probing) upon client’s id which is
an unique attribute to quickly look up client‘s telephone number.
Linear Probing Technique:
• If collision (probe) occurs, alternate cells are tried until empty cell is found.
• All data is stored inside a table, large space is required.
• Whenever a collision occurs, cells are searched sequentially (with wraparound) for empty cell.
• It is easy to implement, but suffers from “Primary Clustering” i.e. when many keys are mapped to
the same location (collision), then linear probing will not distribute keys evenly.
• These keys will be stored in the neighborhood of the location where they are mapped.
• This will lead to clustering of keys around the point of collision, and which leads to Primary
Clustering.
• So, excessive collision is a major problem. Also, more number of searches is required to locate
synonyms due to uneven distribution of keys.
•
So, in the problem statement, we consider,
Hash(key) = key % table size, where key if client id. (consider any table size)
The result is the address to store the whole client record (like client id, client name, client address, client
telephone number, etc).
Algorithm:
Construction of Hash Table for Client telephone book:
Algorithm: Insert_Linear Probe (int client_id) : function returns location of a key
begin
int i, j;
j= client_id % TableSize; // mapped location
for(i=0; i< TableSize; i++)
begin_for
if(hashtable[j] is empty) then //empty cell found
begin_if
hashtable[j] = client_record;
return(j);
end_if
j = (j+1) % TableSize; //next location in circular way.
End_for
//otherwise hash table is full
return(-1);
end
Input:
Output:
Test Cases:
Input/input
Expected output Observed output Passed/failed
characteristics
Conclusion: