0% found this document useful (0 votes)
20 views16 pages

Hash Tables 2

Hash tables provide fast lookup times of O(1) by using hash functions to map keys to integer indices. Good hash functions are fast to compute, avoid collisions, and distribute keys evenly among table cells. Collision resolution techniques like separate chaining and open addressing are used to handle collisions when multiple keys hash to the same index. Separate chaining stores collided keys in a linked list at each index while open addressing resolves collisions by probing to subsequent indices using functions like linear or quadratic probing.

Uploaded by

hriaz2600
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)
20 views16 pages

Hash Tables 2

Hash tables provide fast lookup times of O(1) by using hash functions to map keys to integer indices. Good hash functions are fast to compute, avoid collisions, and distribute keys evenly among table cells. Collision resolution techniques like separate chaining and open addressing are used to handle collisions when multiple keys hash to the same index. Separate chaining stores collided keys in a linked list at each index while open addressing resolves collisions by probing to subsequent indices using functions like linear or quadratic probing.

Uploaded by

hriaz2600
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/ 16

Hash Tables

1
Hash Functions

1. simple/fast to compute,
2. Avoid collisions
3. have keys distributed evenly among cells.
4. Can search in O(1) times

Perfect Hash function: Uniformly distributed


over elements

2
Example
• We have hash function: x mod 10
• We have to insert values: 21, 56, 72, 39, 48,
96, 13
0
1 21
2 72
3
4
5
6 56, 96 Collision
7
8 48
9 39

3
Collision
When two items hash to the same slot,
we must have a systematic method for
placing the second item in the hash table.
This process is called collision resolution.

4
Collision Resolution

Collision: when two keys map to the same


location in the hash table.
Two ways to resolve collisions:
1. Separate Chaining
2. Open Addressing
– Linear probing (Linear search)
– Quadratic probing (Non-Linear search)
– Double hashing (uses two hash functions)

5
Separate Chaining
0 10 Insert:
10, 22, 107, 12, 42
1 Table size =10
2 22 12 42 h(key) = key %10
3
4 • Separate chaining: All keys
5 that map to the same hash
6 value are kept in a list
7 107
8
9
6
Separate Chaining
Advantages
• Simple to implement.
• Hash table never fills up, we can always add
more elements to the chain.

• Less sensitive to the hash function or load


factors.
• It is mostly used when it is unknown how many
and how frequently keys may be inserted or
deleted.

7
Separate Chaining Disadvantages

• Cache performance of chaining is not good as


keys are stored using a linked list.

• Wastage of Space

• If the chain becomes long, then search time can


become O(n) in the worst case.

• Uses extra space for links.

8
Open Addressing
0 8 Insert:
38, 19, 8, 109, 10
1 109
Table size =10
2 10 h(key) = key %10
3
4 • Linear Probing: after
5 checking spot h(k)
6 • try spot h(k)+1 if that is full
7 • try h(k)+2
8 38 • then h(k)+3
9 19 9
Linear Probing
+ Better cache performance

- Clusters are formed

10
Quadratic Probing
f(i) = i2 Less likely to
encounter
• Probe sequence: Primary
Clustering
0th probe = h(k) mod TableSize
1th probe = (h(k) + 1) mod TableSize
2th probe = (h(k) + 2*2) mod TableSize
3th probe = (h(k) + 3*3) mod TableSize
...
ith probe = (h(k) + i2) mod TableSize

11
12
13
14
Quadratic Probing Example
insert(76) insert(40) insert(48) insert(5) insert(55)
76%7 = 6 40%7 = 5 48%7 = 6 5%7 = 5 55%7 = 6
0

But… insert(47)
1
47%7 = 5
2

6
76

15
Hashing Summary
• Hashing is one of the most important
data structures.
• Hashing has many applications where
operations are limited to find, insert, and
delete.
• Dynamic hash tables have good
complexity.

16

You might also like