Hashing Data Structures and Algorithms Nanjing University Fall

  • Slides: 32
Download presentation
Hashing Data Structures and Algorithms Nanjing University, Fall 2020 郑朝栋

Hashing Data Structures and Algorithms Nanjing University, Fall 2020 郑朝栋

Efficient implementation of OSet Search(S, k) Insert(S, x) Remove(S, x) Binary. Search. Tree Treap

Efficient implementation of OSet Search(S, k) Insert(S, x) Remove(S, x) Binary. Search. Tree Treap RB-Tree Skip. List Can we be faster? (if we only care about Search/Insert/Remove)

 • Direct-address Tables Any potential issue? Insert(2) 0 Insert(3) 1 Search(2) Remove(3) Search(3)

• Direct-address Tables Any potential issue? Insert(2) 0 Insert(3) 1 Search(2) Remove(3) Search(3) 2 2 3 3 4 5

Direct-address Tables • Said Πυθαγόρας. That is, Pythagoras.

Direct-address Tables • Said Πυθαγόρας. That is, Pythagoras.

Direct-address Tables •

Direct-address Tables •

Hashing • 3 3 0 1 2 3 4 5 6 7 8

Hashing • 3 3 0 1 2 3 4 5 6 7 8

Hashing • Hashing “randomly” maps keys to buckets 3 3 0 1 2 3

Hashing • Hashing “randomly” maps keys to buckets 3 3 0 1 2 3 4 5 6 7 8

Hashing • But is this possible? NO!!! 3 3 0 1 2 3 4

Hashing • But is this possible? NO!!! 3 3 0 1 2 3 4 5 6 7 8

Collisions in hashing • How to cope with collisions? 3 0 1 2 3

Collisions in hashing • How to cope with collisions? 3 0 1 2 3 4 5 6 7 8

Coping with collisions in hashing Chaining • 0 1 2 3 4 5 6

Coping with collisions in hashing Chaining • 0 1 2 3 4 5 6 7 8 Hash Table 3 3 3

Hashing with Chaining • Time depends on length of the linked list!

Hashing with Chaining • Time depends on length of the linked list!

Let’s be optimistic (for now): The “Simple Uniform Hashing” Assumption •

Let’s be optimistic (for now): The “Simple Uniform Hashing” Assumption •

Assuming “Simple Uniform Hashing” Performance of hashing with chaining •

Assuming “Simple Uniform Hashing” Performance of hashing with chaining •

 Assuming “Simple Uniform Hashing” Performance of hashing with chaining •

Assuming “Simple Uniform Hashing” Performance of hashing with chaining •

Assuming “Simple Uniform Hashing” Performance of hashing with chaining •

Assuming “Simple Uniform Hashing” Performance of hashing with chaining •

Reality fights back •

Reality fights back •

How to design hash functions? Stop! Please don’t try! Unless you really need to…

How to design hash functions? Stop! Please don’t try! Unless you really need to… We will now show you this is really not easy. . .

Designing hash functions Some bad hash functions •

Designing hash functions Some bad hash functions •

Designing hash functions Some bad hash functions •

Designing hash functions Some bad hash functions •

Common technique when designing hash functions The Division Method •

Common technique when designing hash functions The Division Method •

Common technique when designing hash functions The Division Method •

Common technique when designing hash functions The Division Method •

Common technique when designing hash functions The Multiplication Method •

Common technique when designing hash functions The Multiplication Method •

Common technique when designing hash functions The Multiplication Method •

Common technique when designing hash functions The Multiplication Method •

However… •

However… •

Universal Hashing •

Universal Hashing •

Assuming “Universal Hashing” Performance of hashing with chaining •

Assuming “Universal Hashing” Performance of hashing with chaining •

 Strictly speaking, the definition here is actually “ 2 -universal hash family”.

Strictly speaking, the definition here is actually “ 2 -universal hash family”.

A Typical Universal Hash Family •

A Typical Universal Hash Family •

Reading • [CLRS] Ch. 11 (11. 1 -11. 3)

Reading • [CLRS] Ch. 11 (11. 1 -11. 3)