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
- 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
- X = A[“John Smith”]
- A[“Sandra Dee”] = false
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