0% found this document useful (0 votes)
32 views14 pages

Hashing: Review Examples Questions

No empty slots are available.

Uploaded by

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

Hashing: Review Examples Questions

No empty slots are available.

Uploaded by

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

 Hashing

Review
Examples
Questions
Hashing : review
 Background
How to make searching even faster?

O(1) with Hashing

Some limitations, but suitable for some


applications, e.g., compilers, spell checkers,
and security tools.
Hashing : review

 One sentence to explain


 Given a key, use a hash function to compute an index
into a hash table
 Finds, insertions, deletions

Index = hash_function( Key )


 One limitation to consider
Table size is limited
What are the critical issues for the
implementation of Hashing?
Hashing : review

 Two steps to go
1. Design a good hash function
 Fast to compute and
 Minimize the number of collisions

2. Design a method to resolve the collisions


when they occur
 Separate Chaining
 Open Addressing
Hashing : review

 Separate Chaining
Keep a linked list of keys that hash to the same
value

Advantage: deletion is easy

Disadvantage: memory allocation in linked list


manipulation will slow down the program
Hashing : review
 Open Addressing
Linear probing
 hi(K) = (hash(K) + i) mod m, probed sequentially
 with Primary Clustering
Quadratic probing
 hi(K) = ( hash(K) + i2 ) mod m
 with Secondary Clustering
Double hashing
 hi(K) = ( hash(K) + i * hash2(K)) mod m
 to alleviate the problem of clustering
Hashing : review

 Cluster : a block of contiguously occupied


table entries

 Insert 49, 58, 69


with hash(K)=K mod 10
and linear probing
 Blocks of occupied cells start forming,
primary clustering.
 Any key that hashed into the cluster will require
several attempts to resolve the collision.
Hashing : review
 Quadratic Probing
Helps to eliminate primary clustering

Two keys with different


home positions will have
different probe sequences

But keys that hash to the


same home position will
probe the same alternative
cells, i.e., secondary clustering
Hashing : examples

 Given input 4371, 1323, 6173, 4199, 4344,


9679, 1989 and a hash function h(x) = x
mod 10, what are the results of the
following?
(a) Separate chaining hash table
(b) Open addressing hash table using linear
probing
(c) Open addressing hash table with second
hash function h2(x) =7 − (x mod 7)
Hashing : examples
 (a) Separate chaining hash table
0

1 4371

3 1323 6173
4 4344
5

9 4199 9679 1989


Hashing : examples
 (b) Open addressing hash table using linear
probing 0 9679
1 4371

2 1989

3 1323
4 6173
5 4344

9 4199
Hashing : examples
 (c) Open addressing hash table with second
hash function h2(x) =7 − (x mod 7)
0

1 4371

2
Then, what about 1989?
3 1323
4 6173
5 9679

7 4344
8

9 4199
Hashing : examples

 1989 cannot be inserted into the table

because h2(1989) = 6,

and the alternative locations 5, 1, 7, and 3 are


already taken.

You might also like