0% found this document useful (0 votes)
209 views8 pages

Task 4 - Hashing - Separate Chaining and Rehashing

The document discusses hashing using separate chaining and rehashing. It provides an example of separate chaining using a hash table with linked lists to handle collisions. It discusses the advantages of separate chaining as being simple to implement and able to grow the hash table, but disadvantages include poorer cache performance and wasted space as the chains get longer. The document then discusses rehashing as expanding the hash table size when it gets too full to avoid performance degradation. It provides an example of rehashing a hash table when the load reaches over 50%.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
209 views8 pages

Task 4 - Hashing - Separate Chaining and Rehashing

The document discusses hashing using separate chaining and rehashing. It provides an example of separate chaining using a hash table with linked lists to handle collisions. It discusses the advantages of separate chaining as being simple to implement and able to grow the hash table, but disadvantages include poorer cache performance and wasted space as the chains get longer. The document then discusses rehashing as expanding the hash table size when it gets too full to avoid performance degradation. It provides an example of rehashing a hash table when the load reaches over 50%.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

UNIVERSITY OF ENGINEERING AND TECHNOLOGY,

LAHORE

Electrical Department Kala Shah Kaku Campus

Name: Muhammad Faiz Alam Khan


Class: 2016-EE-267
Section: A
Topic: Hashing: Separate Chaining and Rehashing
Supervisor: Dr. Bilal Wajid
TABLE OF CONTENTS

Sr. No Topic Page No

1 Introduction to Separate Chaining 3

2 Examples of Separate Chaining 4

Advantages, Disadvantages and


3 Complexity 5

4 Rehashing 6

5 Example of Rehashing 7

6 Exercise 8

7 Solution to the Exercise File Attached


Separate Chaining

To handle collisions the hash table has a technique known as separate chaining.
Separate chaining is defined as a method by which linked lists of values are built in
association with each location within the hash table when a collision occurs.

The concept of separate chaining involves a technique in which each index key is built
with a linked list. This means that the table's cells have linked lists governed by the
same hash function. So, in place of the collision error which occurred in Figure 1, the
cell now contains a linked list containing the string 'Janet' and 'Martha' as seen in
Figure 2. We see how the subsequent strings are loaded using the separate chaining
technique.
Now let’s discuss one more problem for the better understanding
Example

Lets Consider a Hash Function


H(x) = x mod 7
Here x will be the key which we want to map to. Now the range of the function will
be 0 to 6, so we have a hash table
shown here Lets have some keys, which we are going to hash into the
table.
700 0
Keys: 50, 700, 76, 85, 92, 73, 101
1 50 mod 7=1
50
85
700 mod 7 = 0
2 76 mod 7 = 6

3 85 mod 7 = 1

4 Now we have collision, we have already put 50 in our slot


with index 1 and therefore cannot put 85 in it. So what we do
is we create a slot and link it to the slot with the same hash
76 value.
5
Next we have 92 and
92 mod 7 =1
Again, we have collision, now we are going to add 92.

700 0
1
50
85 92
Next we have 73
2 73 mod 7 = 3
73 3 101 The last one is 101

4 101 mod 7 =3
So again we have a collision, so we create a
76 linked list of hash value 4 and add a node to it
5 with key as 101
So this is how our hash table would look like. Now lets have a look at some of
the advantages and disadvantages we have in separate chaining.

Advantages:
1) It is very simple method to handle collision and therefore can be
implemented easily
2) Hash Table can grow and we can add new elements to it
3) Less sensitive to the hash function or load factors
4) It is mostly used when it is unknown how many and how frequently keys
may be inserted or deleted
Disadvantages:
1) Cache performance of chaining is not good as keys are stored using linked
list
2) Wastage of space
3) If the chain becomes long, then search time cane become O(n) in worst case
4) We have to waste extra memory to save the link address.

Complexity
Finally let’s give a look at the performance of the separate chaining.
For this we define load Factor alpha which is n/m

N= Number of keys stored in table


M= Number of slots in table
α = Average keys per slot or load factor = n/m

The load factor is basically average number of keys per slot given an assumption that
each key is equally likely to be hashed to any slot of table, independent of where other
keys are hashed.
So the expected time to insert/search/delete O(1+α)
Rehashing

Rehashing or variable hashing attempts to circumvent this dilemma by expanding the


hash table size whenever it gets too full.

Lets look at an example for better Understanding

We have to use rehashing when following conditions occur.

1. When table is half full

2. When an insertion fails

3. When load reaches a certain level

Problem

Use open addressing (linear probing) on a table of integers with hash(k)=k (assume the table does an
internal % hSize):

We know that performance degrades when λ > 0.5


Solution:
Rehash when more than half full

So if we have this table, everything is fine.

But if we try to add another element (24), then more than half the slots are occupied…

So we expand the table, and use the hash function to relocate the elements within the larger table…

In this case, I've shown the hash table size doubling, because that's easy to do, despite the fact that it
doesn't lead to prime-number sized tables. If we were going to use quadratic probing, we would
probably keep a table of prime numbers on hand for expansion sizes, and we would probably choose
a set of primes such that each successive prime number was about twice the prior one.

2. Saving the Hash Values

The rehashing operation can be quite lengthy. Luckily, it doesn't need to be done very often.

We can speed things up somewhat by storing the hash values in the table elements along with the
data so that we don't need to recompute the hash values. Also, if we structure the table as a vector
of pointers to the hash elements, then during the rehashing we will only be copying pointers, not the
entire (potentially large) data elements.
Exercise

Question 1 )

Write a Program that demonstrates operations on Hash Tables chaining with Singly
Linked Lists. It must contain following functions

a) Hash Node Class Declaration


b) Hash Map Class Declaration
c) Insertion of an Element at a key
d) Removal of an Element at a key
e) Searching Element at a key

Question 2)

You might also like