0% found this document useful (0 votes)
11 views15 pages

Lecture 13

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)
11 views15 pages

Lecture 13

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/ 15

Hashing

• Hashing is basically a searching technique


• For storing and retrieving data
• Probing means synonymous searching

1
Collisions and their Resolution
• A collision occurs when two different keys hash to the
same value
– E.g. For TableSize = 17, the keys 18 and 35 hash to the same value
– 18 mod 17 = 1 and 35 mod 17 = 1
• Cannot store both data records in the same slot in array!
• Two different methods for collision resolution:
– Separate Chaining: Use a dictionary data structure (such as
a linked list) to store multiple items that hash to the same
slot
– Closed Hashing (or probing): search for empty slots using
a second function and store item in first empty slot that is
found
2
Other Name…

• Separate chaining = Open hashing

• Closed hashing = Open addressing

3
Alternative Strategy: Closed Hashing
Problem with separate chaining:
Memory consumed by pointers –
32 (or 64) bits per key!
0
h(a) = h(d)
What if we only allow one Key at each h(e) = h(b) 1
entry? a
– two objects that hash to the same spot can’t 2
both go there d
– first one there gets the spot 3
e
– next one must go in another spot
4
b
5
c
6
4
Collision Resolution by Closed
Hashing
• Given an item X, try
cells h0(X), h1(X), h2(X), …, hi(X)
• hi(X) = (Hash(X) + F(i)) mod TableSize
– Define F(0) = 0
• F is the collision resolution function. Some
possibilities:
– Linear: F(i) = i
– Quadratic: F(i) = i2

5
Closed Hashing I: Linear Probing
• Main Idea: When collision occurs, scan down
the array one cell at a time looking for an
empty cell
– hi(X) = (Hash(X) + i) mod TableSize (i = 0, 1, 2, …)
– Compute hash value and increment it until a free cell
is found

6
Linear Probing Example
insert(14) insert(8) insert(21) insert(2)
14%7 = 0 8%7 = 1 21%7 =0 2%7 = 2
0 0 0 0
14 14 14 14
1 1 1 1
8 8 8
2 2 2 2
21 21
3 3 3 3
2
4 4 4 4

5 5 5 5

6 6 6 6

1 1 3 2
probes: 7
Drawbacks of Linear Probing
• Works until array is full, but as number of items N
approaches Table Size access time approaches O(N)

8
Closed Hashing II: Quadratic Probing
• Main Idea: Spread out the search for an empty slot –
Increment by i2 instead of i

• hi(X) = (Hash(X) + i2) % TableSize


h0(X) = Hash(X) % TableSize
h1(X) = Hash(X) + 1 % TableSize
h2(X) = Hash(X) + 4 % TableSize
h3(X) = Hash(X) + 9 % TableSize
9
Quadratic Probing Example
insert(14) insert(8) insert(21) insert(2)
14%7 = 0 8%7 = 1 21%7 =0 2%7 = 2
0 0 0 0
14 14 14 14
1 1 1 1
8 8 8
2 2 2 2
2
3 3 3 3

4 4 4 4
21 21
5 5 5 5

6 6 6 6

1 1 3 1
probes: 10
Problem With Quadratic Probing
insert(14) insert(8) insert(21) insert(2) insert(7)
14%7 = 0 8%7 = 1 21%7 =0 2%7 = 2 7%7 = 0
0 0 0 0 0
14 14 14 14 14
1 1 1 1 1
8 8 8 8
2 2 2 2 2
2 2
3 3 3 3 3

4 4 4 4 4
21 21 21
5 5 5 5 5

6 6 6 6 6

probes: 1 1 3 1 ?? 11
Closed Hashing III: Double Hashing
• Idea: Spread out the search for an empty slot by using
a second hash function
– No primary or secondary clustering
• hi(X) = (Hash1(X) + iHash2(X)) mod TableSize
for i = 0, 1, 2, …
• Good choice of Hash2(X) can guarantee does not
get “stuck” as long as  < 1
– Integer keys:
Hash2(X) = R – (X mod R)
where R is a prime smaller than TableSize

12
Double Hashing Example
insert(14) insert(8) insert(21) insert(2) insert(7)
14%7 = 0 8%7 = 1 21%7 =0 2%7 = 2 7%7 = 0
5-(21%5)=4 5-(21%5)=4
0 0 0 0 0
14 14 14 14 14
1 1 1 1 1
8 8 8 8
2 2 2 2 2
2 2
3 3 3 3 3

4 4 4 4 4
21 21 21
5 5 5 5 5

6 6 6 6 6

probes: 1 1 2 1 ?? 13
Double Hashing Example
insert(14) insert(8) insert(21) insert(2) insert(7)
14%7 = 0 8%7 = 1 21%7 =0 2%7 = 2 7%7 = 0
5-(21%5)=4 5-(21%5)=4
0 0 0 0 0
14 14 14 14 14
1 1 1 1 1
8 8 8 8
2 2 2 2 2
2 2
3 3 3 3 3

4 4 4 4 4
21 21 21
5 5 5 5 5
7
6 6 6 6 6

probes: 1 1 2 1 4 14
Double Hashing Example

15

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy