![Page 1: PowerPoint Presentation€¦ · A Hash Table based Dictionary A Hash Table consists of three things: 1. 2. 3. Dictionary d; d[k] = v; 1 2 Client Code:](https://reader033.vdocuments.us/reader033/viewer/2022042508/5f955c8b2b67b4213e0285a9/html5/thumbnails/1.jpg)
CS 225 Data Structures
Oct. 30 – Hashing 2
![Page 2: PowerPoint Presentation€¦ · A Hash Table based Dictionary A Hash Table consists of three things: 1. 2. 3. Dictionary d; d[k] = v; 1 2 Client Code:](https://reader033.vdocuments.us/reader033/viewer/2022042508/5f955c8b2b67b4213e0285a9/html5/thumbnails/2.jpg)
A Hash Table based Dictionary
A Hash Table consists of three things: 1.
2.
3.
Dictionary<KeyType, ValueType> d;
d[k] = v;
1
2
Client Code:
![Page 3: PowerPoint Presentation€¦ · A Hash Table based Dictionary A Hash Table consists of three things: 1. 2. 3. Dictionary d; d[k] = v; 1 2 Client Code:](https://reader033.vdocuments.us/reader033/viewer/2022042508/5f955c8b2b67b4213e0285a9/html5/thumbnails/3.jpg)
Exam Updates
Current: Exam 8 (Programming) • Topics: MP4-like (eg: iterators), AVL Next Week: Exam 9 (Theory) • Topics: AVL trees
BTrees kD Trees
![Page 4: PowerPoint Presentation€¦ · A Hash Table based Dictionary A Hash Table consists of three things: 1. 2. 3. Dictionary d; d[k] = v; 1 2 Client Code:](https://reader033.vdocuments.us/reader033/viewer/2022042508/5f955c8b2b67b4213e0285a9/html5/thumbnails/4.jpg)
MP5
![Page 5: PowerPoint Presentation€¦ · A Hash Table based Dictionary A Hash Table consists of three things: 1. 2. 3. Dictionary d; d[k] = v; 1 2 Client Code:](https://reader033.vdocuments.us/reader033/viewer/2022042508/5f955c8b2b67b4213e0285a9/html5/thumbnails/5.jpg)
Collision Handling: Separate Chaining S = { 16, 8, 4, 13, 29, 11, 22 } |S| = n h(k) = k % 7 |Array| = N
0
1
2
3
4
5
6
Worst Case SUHA
Insert
Remove/Find
(Example of open hashing)
![Page 6: PowerPoint Presentation€¦ · A Hash Table based Dictionary A Hash Table consists of three things: 1. 2. 3. Dictionary d; d[k] = v; 1 2 Client Code:](https://reader033.vdocuments.us/reader033/viewer/2022042508/5f955c8b2b67b4213e0285a9/html5/thumbnails/6.jpg)
Collision Handling: Probe-based Hashing S = { 16, 8, 4, 13, 29, 11, 22 } |S| = n h(k) = k % 7 |Array| = N Try h(k) = (k + 0) % 7, if full… Try h(k) = (k + 1) % 7, if full… Try h(k) = (k + 2) % 7, if full… Try …
0
1
2
3
4
5
6
(Example of closed hashing)
![Page 7: PowerPoint Presentation€¦ · A Hash Table based Dictionary A Hash Table consists of three things: 1. 2. 3. Dictionary d; d[k] = v; 1 2 Client Code:](https://reader033.vdocuments.us/reader033/viewer/2022042508/5f955c8b2b67b4213e0285a9/html5/thumbnails/7.jpg)
A Problem w/ Linear Probing Primary clustering: Description: Remedy:
![Page 8: PowerPoint Presentation€¦ · A Hash Table based Dictionary A Hash Table consists of three things: 1. 2. 3. Dictionary d; d[k] = v; 1 2 Client Code:](https://reader033.vdocuments.us/reader033/viewer/2022042508/5f955c8b2b67b4213e0285a9/html5/thumbnails/8.jpg)
Collision Handling: Double hashing S = { 16, 8, 4, 13, 29, 11, 22 } |S| = n h(k) = k % 7 |Array| = N Try h(k) = (k + 0*h2(k)) % 7, if full… Try h(k) = (k + 1*h2(k)) % 7, if full… Try h(k) = (k + 2*h2(k)) % 7, if full… Try … h(k, i) = (h1(k) + i*h2(k)) % 7
0
1
2
3
4
5
6
(Example of closed hashing)
![Page 9: PowerPoint Presentation€¦ · A Hash Table based Dictionary A Hash Table consists of three things: 1. 2. 3. Dictionary d; d[k] = v; 1 2 Client Code:](https://reader033.vdocuments.us/reader033/viewer/2022042508/5f955c8b2b67b4213e0285a9/html5/thumbnails/9.jpg)
Running Times
Linear Probing: • Successful: ½(1 + 1/(1-α)) • Unsuccessful: ½(1 + 1/(1-α))2
Double Hashing: • Successful: 1/α * ln(1/(1-α)) • Unsuccessful: 1/(1-α)
Separate Chaining: • Successful: 1 + α/2 • Unsuccessful: 1 + α
The expected number of probes for find(key) under SUHA
(Don’t memorize these equations, no need.) Instead, observe: - As α increases: - If α is constant:
![Page 10: PowerPoint Presentation€¦ · A Hash Table based Dictionary A Hash Table consists of three things: 1. 2. 3. Dictionary d; d[k] = v; 1 2 Client Code:](https://reader033.vdocuments.us/reader033/viewer/2022042508/5f955c8b2b67b4213e0285a9/html5/thumbnails/10.jpg)
Running Times
Linear Probing: • Successful: ½(1 + 1/(1-α)) • Unsuccessful: ½(1 + 1/(1-α))2
Double Hashing: • Successful: 1/α * ln(1/(1-α)) • Unsuccessful: 1/(1-α)
The expected number of probes for find(key) under SUHA
![Page 11: PowerPoint Presentation€¦ · A Hash Table based Dictionary A Hash Table consists of three things: 1. 2. 3. Dictionary d; d[k] = v; 1 2 Client Code:](https://reader033.vdocuments.us/reader033/viewer/2022042508/5f955c8b2b67b4213e0285a9/html5/thumbnails/11.jpg)
ReHashing What if the array fills?
![Page 12: PowerPoint Presentation€¦ · A Hash Table based Dictionary A Hash Table consists of three things: 1. 2. 3. Dictionary d; d[k] = v; 1 2 Client Code:](https://reader033.vdocuments.us/reader033/viewer/2022042508/5f955c8b2b67b4213e0285a9/html5/thumbnails/12.jpg)
Which collision resolution strategy is better? • Big Records: • Structure Speed: What structure do hash tables replace? What constraint exists on hashing that doesn’t exist with BSTs? Why talk about BSTs at all?
![Page 13: PowerPoint Presentation€¦ · A Hash Table based Dictionary A Hash Table consists of three things: 1. 2. 3. Dictionary d; d[k] = v; 1 2 Client Code:](https://reader033.vdocuments.us/reader033/viewer/2022042508/5f955c8b2b67b4213e0285a9/html5/thumbnails/13.jpg)
CS 225 – Things To Be Doing Exam 8 (programming exam, MP4-like and AVL) is live! More Info: https://courses.engr.illinois.edu/cs225/fa2017/exams/
MP5: EC due tonight! Extra Credit +7 deadline: Monday, Oct. 30
Lab: lab_hash is released Wednesday Due Sunday, Dec. 5 at 11:59pm
POTD Every Monday-Friday – Worth +1 Extra Credit /problem (up to +40 total)