lecture 23 separate chaining and open addressing · lecture 23. previous lecture 1. collision or...

Post on 26-Aug-2020

1 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Separate Chaining and Open Addressing

Mr. Mubashir AliLecturer (Dept. of Software Engineering)

mubashirali@lgu.edu.pkLahore Garrison University, Lahore

1

Lecture 23

Previous Lecture

1. Collision or Clash

2. Linear Probing

3. Lab-10

Mubashir Ali - Lecturer (Department of Software Engineering)

2

Outline

1. Separate ChainingI. Advantages

II. Disadvantages

2. Open AddressingI. Basic Operations

II. Linear Probing and Clustering

III. Quadratic Probing

3. Comparison

4. Lab-10

Mubashir Ali - Lecturer (Department of Software Engineering)

3

Separate Chaining

• The idea is to make each cell of hash tablepoint to a linked list of records that have samehash function value.

• Let us consider a simple hash function as “keymod 7” and sequence of keys as 50, 700, 76,85, 92, 73, 101.

Mubashir Ali - Lecturer (Department of Software Engineering)

4

Separate Chaining

Mubashir Ali - Lecturer (Department of Software Engineering)

5

Advantages

• Simple to implement.

• Hash table never fills up, we can always addmore elements to the chain.

• Less sensitive to the hash function or loadfactors.

• It is mostly used when it is unknown howmany and how frequently keys may beinserted or deleted.

Mubashir Ali - Lecturer (Department of Software Engineering)

6

Disadvantages

• Cache performance of chaining is not good askeys are stored using a linked list. Openaddressing provides better cache performanceas everything is stored in the same table.

• Wastage of Space (Some Parts of hash tableare never used)

• If the chain becomes long, then search timecan become O(n) in the worst case.

• Uses extra space for links.Mubashir Ali - Lecturer (Department of

Software Engineering)7

Open Addressing

• In Open Addressing, all elements are stored inthe hash table itself. So at any point, size ofthe table must be greater than or equal to thetotal number of keys.

• Open addressing is implemented using

– Linear Probing

– Quadratic Probing

Mubashir Ali - Lecturer (Department of Software Engineering)

8

Basic Operation via Open Addressing

• Insert(k): Keep probing until an empty slot isfound. Once an empty slot is found, insert k.

• Search(k): Keep probing until slot’s key doesn’tbecome equal to k or an empty slot isreached.

• Delete(k): Delete operation is interesting. Ifwe simply delete a key, then search may fail.So slots of deleted keys are marked speciallyas “deleted”.

Mubashir Ali - Lecturer (Department of Software Engineering)

9

Linear Probing

• Already discussed in previous lecture.

Mubashir Ali - Lecturer (Department of Software Engineering)

10

Linear Probing

Linear Probing is already discussed with detail inprevious lecture.

Clustering:

The main problem with linear probing isclustering, many consecutive elements formgroups and it starts taking time to find a free slotor to search an element.

Mubashir Ali - Lecturer (Department of Software Engineering)

11

Quadratic Probing

We look for i2‘th slot in i’th iteration.

• let hash(x) be the slot index computedusing hash function.

• If slot hash(x) % S is full, then wetry (hash(x) + 1*1) % S

• If (hash(x) + 1*1) % S is also full,

• Then we try (hash(x) + 2*2) % S If(hash(x) + 2*2) % S is also full

• Then we try (hash(x) + 3*3) % S and soon.

Mubashir Ali - Lecturer (Department of Software Engineering)

12

We look for i2‘th slot in i’th iteration.

Clustering

Mubashir Ali - Lecturer (Department of Software Engineering)

13

Mubashir Ali - Lecturer (Department of Software Engineering)

14

Separate Chaining Open Addressing

Chaining is Simpler toimplement.

Open Addressing requiresmore computation.

Hash table never fills up,we can always add moreelements to chain.

In open addressing, Hashtable may become full.

Chaining is Less sensitiveto the hash function orload factors.

Open addressing requiresextra care for to avoidclustering and load factor.

Wastage of Space (SomeParts of hash table inchaining are never used).

In Open addressing, a slotcan be used even if aninput doesn’t map to it.

Lab-10

Write C++ program that takes array of Nintegers and place Integers from Array to Hashmap by avoiding collision using followingtechniques,

– Separate Chaining

– Open Addressing

Note: Array must contains repeated numbers.

Mubashir Ali - Lecturer (Department of Software Engineering)

15

Summary

Separate Chaining Advantages

Disadvantages

Open Addressing Basic Operations

Linear Probing and Clustering

Quadratic Probing

Comparison

Lab-10

Mubashir Ali - Lecturer (Department of Software Engineering)

16

References

you will be able to find course resources at

http://www.mubashirali.com/data-structures-algorithms/

Topic 10.2 (Data Structures and Algorithms in C++ by AdamDrozdek)

Mubashir Ali - Lecturer (Department of Software Engineering)

17

top related