CS 225 Data Structures Mar March h 15 15 Hash T Table C e - - PowerPoint PPT Presentation

cs 225
SMART_READER_LITE
LIVE PREVIEW

CS 225 Data Structures Mar March h 15 15 Hash T Table C e - - PowerPoint PPT Presentation

CS 225 Data Structures Mar March h 15 15 Hash T Table C e Collisions Wade Fa Wa Fagen-Ul Ulmsch schnei eider er, , Cra Craig Zi Zilles (Example of open hashing) Collision Handling: Sep eparate e Chaining S = { 16, 8, 4, 13,


slide-1
SLIDE 1

CS 225

Data Structures

Mar March h 15 15 – Hash T Table C e Collisions

Wa Wade Fa Fagen-Ul Ulmsch schnei eider er, , Cra Craig Zi Zilles

slide-2
SLIDE 2

Collision Handling: Sep eparate e Chaining

S = { 16, 8, 4, 13, 29, 11, 22 } |S| = n h(k) = k % 7 |Array| = N

1 2 3 4 5 6 Worst Case SUHA Insert Remove/Find (Example of open hashing)

slide-3
SLIDE 3

Col Collision

  • n Handling: Prob
  • be-based

ed Hashing

S = { 16, 8, 4, 13, 29, 11, 22 } |S| = n h(k) = k % 7 |Array| = N

1 2 3 4 5 6 (Example of closed hashing)

slide-4
SLIDE 4

Col Collision

  • n Handling: Linea

ear Probing

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 …

1 2 3 4 5 6 (Example of closed hashing) Worst Case SUHA Insert Remove/Find

slide-5
SLIDE 5

A Problem em w/ Linea ear Probing

Primary clustering: Description: Remedy:

slide-6
SLIDE 6

Collision Handling: Double e 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

1 2 3 4 5 6 (Example of closed hashing)

slide-7
SLIDE 7

Running Times es

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:
slide-8
SLIDE 8

Running Times es

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

slide-9
SLIDE 9

ReH eHashing

What if the array fills?

slide-10
SLIDE 10

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?