Computer-Science

Maps, Hash Tables, Skip Lists, and Dictionaries

1. Maps

1) The Map ADT

Example

Operation Output Map
empty() true ร˜
put(5,A) p1:[(5,A)] (5,A)
put(7,B) p2:[(7,B)] (5,A),(7,B)
put(2,C) p3:[(2,C)] (5,A),(7,B),(2,C)
put(7,D) p2:[(7,E)] (5,A),(7,D),(2,C)
find(7) p2:[(7,E)] (5,A),(7,D),(2,C)
find(3) end (5,A),(7,D),(2,C)
find(2) p3:[(2,C)] (5,A),(7,D),(2,C)
size() 3 (5,A),(7,D),(2,C)
erase(5) - (7,D),(2,C)
erase(p2) - (7,D)
find(7) p1:[(7,D)] (7,D)
empty() false (7,D)

A Simple List-Based Map

แ„‰แ…ณแ„แ…ณแ„…แ…ตแ†ซแ„‰แ…ฃแ†บ 2021-12-13 แ„‹แ…ฉแ„’แ…ฎ 4 10 10

Algorithms

The find Algorithm
Algorithm find(k):
  for each p in [L.begin(), L.end()) do
    if p.key() == k then
      return p
  return L.end() // there is no entry with key equal to k
The put Algorithm
Algorithm put(k,v):
  for each p in [L.begin(), L.end()) do
    if p.key() == k then
      *p โ† (k, v)
      return p
  p = L.insertBack((k, v)) // there is no entry with key k
  n = n + 1 // increment number of entries
  return p
The erase Algorithm
Algorithm erase(k):
  for each p in [L.begin(), L.end()) do
    if p.key() == k then
      L.erase(p)
      n = n - 1 // decrement number of entries
      return

Performance of a List-Based Map

2) The Ordered Map ADT

แ„‰แ…ณแ„แ…ณแ„…แ…ตแ†ซแ„‰แ…ฃแ†บ 2021-12-13 แ„‹แ…ฉแ„’แ…ฎ 4 17 15

2. Hash Tables

Intuitive Notion of a Map

แ„‰แ…ณแ„แ…ณแ„…แ…ตแ†ซแ„‰แ…ฃแ†บ 2021-12-13 แ„‹แ…ฉแ„’แ…ฎ 4 24 51

More General Kinds of Keys

แ„‰แ…ณแ„แ…ณแ„…แ…ตแ†ซแ„‰แ…ฃแ†บ 2021-12-13 แ„‹แ…ฉแ„’แ…ฎ 4 27 55

Hash Functions and Hash Tables

Example

แ„‰แ…ณแ„แ…ณแ„…แ…ตแ†ซแ„‰แ…ฃแ†บ 2021-12-13 แ„‹แ…ฉแ„’แ…ฎ 4 33 11

Hash Collisions

Hash Functions

Hash Codes

Compression Functions

Collision Handling: Separate Chaining

แ„‰แ…ณแ„แ…ณแ„…แ…ตแ†ซแ„‰แ…ฃแ†บ 2021-12-13 แ„‹แ…ฉแ„’แ…ฎ 4 57 05

Exercise: Separate Chaining

Result :
  • (Jacob,TX)โ†’5โ†’5
  • (Kevin,PA)โ†’28โ†’1
  • (Angie,PA)โ†’19โ†’1
  • (Mary,NY)โ†’15โ†’6
  • (John,MA)โ†’20โ†’2
  • (Mike,WA)โ†’33โ†’6
  • (James,CA)โ†’12โ†’3
  • (Jane,WY)โ†’17โ†’8
  • (Julie,FL)โ†’10โ†’1
0 1 2 3 4 5 6 7 8
ย  (Kevin, PA) (John, MA) (James, CA) ย  (Jacob, TX) (Mary, NY) ย  (Jane, WY)
ย  (Angie, PA) ย  ย  ย  ย  (Mike, WA) ย  ย 
ย  (Julie, FL) ย  ย  ย  ย  ย  ย  ย 

Collision Handling: Linear Probing

Exercise: Linear Probing

Result :
  • (Jacob,TX)โ†’10โ†’ 10
  • (Kevin,PA)โ†’22โ†’ 0
  • (Angie,PA)โ†’31โ†’ 9
  • (Mary,NY)โ†’4โ†’ 4
  • (John,MA)โ†’15โ†’ 4
  • (Mike,WA)โ†’28โ†’ 6
  • (James,CA)โ†’17โ†’ 6
  • (Jane,WY)โ†’88โ†’ 0
  • (Julie,FL)โ†’59โ†’ 4
0 1 2 3 4 5 6 7 8 9 10
(Kevin, PA) (Jane, WY) ย  ย  (Mary, NY) (John, MA) (Mike, WA) (James, CA) (Julie, FL) (Angie, PA) (Jacob, TX)

Collision Handling: Quadratic Probing

Collision Handling: Double Hashing

Exercise: Double Hashing

Result :

แ„‰แ…ณแ„แ…ณแ„…แ…ตแ†ซแ„‰แ…ฃแ†บ 2021-12-13 แ„‹แ…ฉแ„’แ…ฎ 5 59 32

Performance of Hashing

3. Skip Lists

What is a Skip List

แ„‰แ…ณแ„แ…ณแ„…แ…ตแ†ซแ„‰แ…ฃแ†บ 2021-12-13 แ„‹แ…ฉแ„’แ…ฎ 10 03 59

Insertion

แ„‰แ…ณแ„แ…ณแ„…แ…ตแ†ซแ„‰แ…ฃแ†บ 2021-12-13 แ„‹แ…ฉแ„’แ…ฎ 10 09 45

4. Dictionaries