You are on page 1of 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