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

17
Separate Chaining and Open Addressing Mr. Mubashir Ali Lecturer (Dept. of Software Engineering) [email protected] Lahore Garrison University, Lahore 1 Lecture 23

Upload: others

Post on 26-Aug-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Lecture 23 Separate Chaining and Open Addressing · Lecture 23. Previous Lecture 1. Collision or Clash 2. Linear Probing 3. Lab-10 Mubashir Ali - Lecturer (Department of Software

Separate Chaining and Open Addressing

Mr. Mubashir AliLecturer (Dept. of Software Engineering)

[email protected] Garrison University, Lahore

1

Lecture 23

Page 2: Lecture 23 Separate Chaining and Open Addressing · Lecture 23. Previous Lecture 1. Collision or Clash 2. Linear Probing 3. Lab-10 Mubashir Ali - Lecturer (Department of Software

Previous Lecture

1. Collision or Clash

2. Linear Probing

3. Lab-10

Mubashir Ali - Lecturer (Department of Software Engineering)

2

Page 3: Lecture 23 Separate Chaining and Open Addressing · Lecture 23. Previous Lecture 1. Collision or Clash 2. Linear Probing 3. Lab-10 Mubashir Ali - Lecturer (Department of Software

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

Page 4: Lecture 23 Separate Chaining and Open Addressing · Lecture 23. Previous Lecture 1. Collision or Clash 2. Linear Probing 3. Lab-10 Mubashir Ali - Lecturer (Department of Software

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

Page 5: Lecture 23 Separate Chaining and Open Addressing · Lecture 23. Previous Lecture 1. Collision or Clash 2. Linear Probing 3. Lab-10 Mubashir Ali - Lecturer (Department of Software

Separate Chaining

Mubashir Ali - Lecturer (Department of Software Engineering)

5

Page 6: Lecture 23 Separate Chaining and Open Addressing · Lecture 23. Previous Lecture 1. Collision or Clash 2. Linear Probing 3. Lab-10 Mubashir Ali - Lecturer (Department of Software

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

Page 7: Lecture 23 Separate Chaining and Open Addressing · Lecture 23. Previous Lecture 1. Collision or Clash 2. Linear Probing 3. Lab-10 Mubashir Ali - Lecturer (Department of Software

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

Page 8: Lecture 23 Separate Chaining and Open Addressing · Lecture 23. Previous Lecture 1. Collision or Clash 2. Linear Probing 3. Lab-10 Mubashir Ali - Lecturer (Department of Software

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

Page 9: Lecture 23 Separate Chaining and Open Addressing · Lecture 23. Previous Lecture 1. Collision or Clash 2. Linear Probing 3. Lab-10 Mubashir Ali - Lecturer (Department of Software

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

Page 10: Lecture 23 Separate Chaining and Open Addressing · Lecture 23. Previous Lecture 1. Collision or Clash 2. Linear Probing 3. Lab-10 Mubashir Ali - Lecturer (Department of Software

Linear Probing

• Already discussed in previous lecture.

Mubashir Ali - Lecturer (Department of Software Engineering)

10

Page 11: Lecture 23 Separate Chaining and Open Addressing · Lecture 23. Previous Lecture 1. Collision or Clash 2. Linear Probing 3. Lab-10 Mubashir Ali - Lecturer (Department of Software

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

Page 12: Lecture 23 Separate Chaining and Open Addressing · Lecture 23. Previous Lecture 1. Collision or Clash 2. Linear Probing 3. Lab-10 Mubashir Ali - Lecturer (Department of Software

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.

Page 13: Lecture 23 Separate Chaining and Open Addressing · Lecture 23. Previous Lecture 1. Collision or Clash 2. Linear Probing 3. Lab-10 Mubashir Ali - Lecturer (Department of Software

Clustering

Mubashir Ali - Lecturer (Department of Software Engineering)

13

Page 14: Lecture 23 Separate Chaining and Open Addressing · Lecture 23. Previous Lecture 1. Collision or Clash 2. Linear Probing 3. Lab-10 Mubashir Ali - Lecturer (Department of Software

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.

Page 15: Lecture 23 Separate Chaining and Open Addressing · Lecture 23. Previous Lecture 1. Collision or Clash 2. Linear Probing 3. Lab-10 Mubashir Ali - Lecturer (Department of Software

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

Page 16: Lecture 23 Separate Chaining and Open Addressing · Lecture 23. Previous Lecture 1. Collision or Clash 2. Linear Probing 3. Lab-10 Mubashir Ali - Lecturer (Department of Software

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

Page 17: Lecture 23 Separate Chaining and Open Addressing · Lecture 23. Previous Lecture 1. Collision or Clash 2. Linear Probing 3. Lab-10 Mubashir Ali - Lecturer (Department of Software

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