Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

16-amortized.pdf lecture, Lecture notes of Advanced Algorithms

16-amortized.pdf lecture notes

Typology: Lecture notes

2024/2025

Uploaded on 05/23/2025

fancycode
fancycode 🇺🇸

7 documents

1 / 45

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Amortized analysis
Yan Gu
CS218: Design and Analysis of algorithms
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d

Partial preview of the text

Download 16-amortized.pdf lecture and more Lecture notes Advanced Algorithms in PDF only on Docsity!

Amortized analysis

Yan Gu

CS218: Design and Analysis of algorithms

Exam structure (55 points in total)

  • Basic knowledge (multiple choices), 10 pts
  • DP basics, 9 pts
  • Greedy proof, 5 pts
  • Augmented-tree design, 5 pts
  • Graph basics, 9 pts
  • Analysis basics, 6 pts
  • Algorithm design, 16 pts
  • 60 points in total
  • Plus bonus points
  • Basic knowledge (multiple choices), 10 pts
  • DP basics, 9 pts
  • Greedy proof, 5 pts
  • Augmented-tree design, 5 pts
  • Graph basics, 9 pts
  • Analysis basics, 6 pts
  • Algorithm design, 16 pts
  • 60 points in total
  • Plus bonus points

CEP prob 2 CEP prob 1 Midterm Final

  • Multiple choices, 5 pts
  • DP basics, 9 pts
  • Greedy proof, 5 pts
  • Augmented-tree design, 5 pts
  • Algorithm design, 8 pts
  • 32 points in total
    • Multiple choices, 5 pts
    • DP basics, 9 pts
    • Greedy proof, 5 pts
    • Graph basics, 9 pts
    • Analysis basics, 6 pts
    • Algorithm design, 8+8 pts
    • 50 points in total

Amortized analysis

Yan Gu

CS218: Design and Analysis of algorithms

Hashing and hash table

Hash function

  • Maps arbitrary data to fixed-size values
    • Usually integers
  • The same data are always mapped to the same value
  • Different data are unlikely to be mapped to the same value
    • Collision: two keys are hashed to the same hash value

Hash table and hash functions

  • E.g., Strings - > integers
  • A[“John Smith”] = false
    • A[2] = false (John Smith)
  • A[“Lisa Smith”] = true
    • A[1] = true (Lisa Smith)
  • X = A[“John Smith”]
    • X = A[2]
  • A[“Sandra Dee”] = false
    • A[2]?

Simple uniform hashing strategy

  • For each element with key 𝑥 , find a random position 𝒉𝟏 𝒙 ;
    • if there’s a collision, try another (i.e., 𝒉𝟐 𝒙 )
  • Say insert key to be 5
  • Then insert key to be 88
  • What’s the expected number of retries?

Ways to deal with collisions

  • When an element is mapped to a index 𝒊 , but finds out that position 𝒊 has been taken by another element?
  • Open addressing / closed hashing
    • Find another empty position (e.g., linear probing: try the next position)
    • It can also be using other ways to find the next empty position, not necessarily try the next position (e.g., probe quadratically)
  • Closed addressing / open hashing
    • Throw the element still to position 𝑖
    • All elements in position 𝑖 will be further organized as another data structure, e.g., a linked list

Open addressing vs. closed addressing Open Addressing Closed Addressing Source: https://programming.guide/h ash-tables-open-vs-closed- addressing.html

In the above analysis, it’s very important that the hash table load factor is a below 1/

  • That’s why we know that an insertion succeeds with probability ½
  • So the cost is 1/2+1/4+1/8+… = O(1)
  • Actually, any constant works
  • But, how can we guarantee that?

Resizing hash table

  • When the load factor of the hash table is more than ½
  • i.e., when we have more than n/2 elements in the hash table of size n
  • Resize the table 3 5 8 7 2 Array of size 2n Array of size n, half full 3 8 7 5 2

What is the cost of an insertion?

  • Worst case cost: 𝑶(𝒏)?
    • 𝑛 is the current table size
  • However, this happens very rarely - at least every 𝑶 𝒏 insertions!
  • All the rest of the insertions cost 𝑂( 1 )!
  • Every 𝑡 insertions, we have an insertion of cost 𝑂(𝑡)
  • Somehow “on average”, the cost is still a constant? Initial total size Current #slots filled #insertions before resizing Resizing cost Phase 1 k 0 k/2 k/ Phase 2 2k k/2 k/2 k Phase 3 4k k k 2k Phase 4 8k 2k 2k 4k …… Happens after k/2 insertions Happens after k/2 insertions Happens after k insertions Happens after 2k insertions (Assume unit cost per insertion and per rehash)

Amortized Analysis