0% found this document useful (0 votes)
49 views4 pages

Asgn 1

Uploaded by

rajsolat18
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views4 pages

Asgn 1

Uploaded by

rajsolat18
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

ASSIGNMENT NUMBER: 01

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.

Environment: Open Source C++ Programming tool like G++.

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.

There are two different forms of hashing:


1. Open or external hashing
– Allows records to be stored in unlimited space (could be hard disk)
2. Closed or internal hashing
– Uses fixed space for storage, thus limits the size of hash table.

Synonyms: If the set of keys that hash to the same location.
Collision : If the actual data that we insert into our list contains two or more synonyms , then collision occur.

Few Collision Resolution ideas :


– Separate chaining (used for open hashing)
– Some Open Addressing techniques to avoid collision.
• Linear Probing
• Quadratic Probing
• Double hashing

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

Algorithm: Search_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].client_id ==client_id) then //empty cell found
begin_if
// record found
return(j);
end_if
j = (j+1) % TableSize; //next location in circular way.
End_for
//if record doesn’t exist
return(-1);
end

Input:

Output:

Test Cases:

Input/input
Expected output Observed output Passed/failed
characteristics

Conclusion:

Hash table with Linear Probing is implemented for client records.


Frequently Asked Questions (FAQ):

1) Explain Following with example:

a) Linear Probing with chaining without replacement

b) Linear Probing with chaining with replacement

2) Differentiate between Linear Probing and Quadratic Probing.

3) Discuss about Double Hashing with example.

You might also like