Docsity

Hash Tables: Understanding Hash Functions, Collisions, and Probing Techniques, Lecture notes of Data Structures and Algorithms

Partial preview of the text

Download Hash Tables: Understanding Hash Functions, Collisions, and Probing Techniques and more Lecture notes Data Structures and Algorithms in PDF only on Docsity!

Hash Tables

See 5.1-5.6 and 5.9 in the

Textbook

Hash Tables

• Hash Functions

• Collision Resolution Techniques

• Linear Probing

• Quadratic Probing

• Review Questions

Hash Function

• For integer keys that are uniformly distributed the

hash function can mod the key (%) the table size

• For strings a simple hash function adds the

character codes of the characters in the string and

mods by the table size. This is easy to implement

and works for small table sizes

• Figure 5.4 in the book shows a better hash function

for strings. If keys are long strings the function can

be modified to use only some of the characters in

the string (i.e. every other character)

Collisions

• Two keys have the same hash function

value

Linear Probing

(^0) Noether (^1) Euler (^2) Cantor 3 4 (^5) Hardy (^6) Fermat 7 8 9 10 11 (^12) Frege hash(key) h 0 (key) h 1 (key) … hi(key) = (hash(key) + f(i)) mod N Where N is the table size For linear probing f(i) = i

Linear Probing Example

Key Hash Function Value Frege 12 Euler 1 Hardy 5 Noether 12 Cantor 0 Fermat 6

Linear Probing Example

Key Hash Function Value Frege 12 Euler 1 Hardy 5 Noether 12 Cantor 0 Fermat 6

(^1) Euler 2 3 4 5 6 7 8 9 10 11 (^12) Frege

Linear Probing Example

Key Hash Function Value Frege 12 Euler 1 Hardy 5 Noether 12 Cantor 0 Fermat 6

(^1) Euler 2 3 4 (^5) Hardy 6 7 8 9 10 11 (^12) Frege

Linear Probing Example

Key Hash Function Value Frege 12 Euler 1 Hardy 5 Noether 12 Cantor 0 Fermat 6 (^0) Noether (^1) Euler (^2) Cantor 3 4 (^5) Hardy 6 7 8 9 10 11 (^12) Frege

Linear Probing Example

Key Hash Function Value Frege 12 Euler 1 Hardy 5 Noether 12 Cantor 0 Fermat 6 (^0) Noether (^1) Euler (^2) Cantor 3 4 (^5) Hardy (^6) Fermat 7 8 9 10 11 (^12) Frege

Linear Probing Example

Key Hash Function Value Frege 12 Euler 1 Hardy 5 Noether 12 Cantor 0 Fermat 6 (^0) Noether (^1) Euler (^2) Cantor 3 4 (^5) Hardy (^6) Fermat 7 8 9 10 11 (^12) Frege Full Full Full Empty Empty Full Full Empty Empty Empty Empty Empty Full

Linear Probing Example

Key Hash Function Value Frege 12 Euler 1 Hardy 5 Noether 12 Cantor 0 Fermat 6 Search for Pascal (0) (^0) Noether (^1) Euler (^2) Cantor 3 4 (^5) Hardy (^6) Fermat 7 8 9 10 11 (^12) Frege Full Full Full Empty Empty Full Full Empty Empty Empty Empty Empty Full

Linear Probing Example

Key Hash Function Value Frege 12 Euler 1 Hardy 5 Noether 12 Cantor 0 Fermat 6 Search for Pascal (0) Insert Pascal Remove Euler Search for Pascal (^0) Noether 1 (^2) Cantor (^3) Pascal 4 (^5) Hardy (^6) Fermat 7 8 9 10 11 (^12) Frege Full Empty Full Full Empty Full Full Empty Empty Empty Empty Empty Full

Linear Probing Example

Key Hash Function Value Frege 12 Euler 1 Hardy 5 Noether 12 Cantor 0 Fermat 6 Search for Pascal (0) Insert Pascal Remove Euler Search for Pascal (^0) Noether 1 (^2) Cantor (^3) Pascal 4 (^5) Hardy (^6) Fermat 7 8 9 10 11 (^12) Frege Full Removed Full Full Empty Full Full Empty Empty Empty Empty Empty Full