Amortized Analysis Flashcards
What is amortized analysis?
A technique for analyzing the average cost per operation over a sequence of operations, ensuring worst-case performance bounds without using probability.
How is amortized analysis different from average-case analysis?
Amortized analysis provides worst-case guarantees over sequences, while average-case uses probabilities and expectations.
When is amortized analysis useful?
When a few operations are expensive but are ‘paid for’ by many cheap operations.
What are the three main methods of amortized analysis?
- Aggregate
- Accounting
- Potential methods.
What is the Aggregate Method?
Calculate total cost T(n) of n operations, then amortized cost = T(n)/n.
What is the Accounting Method?
Overcharge some operations, store the surplus as ‘credit,’ and use it to pay for more expensive future operations.
What must the credit total always be in the accounting method?
Non-negative — credit can’t go below zero.
What is the Potential Method?
Uses a potential function Φ to represent stored work (like energy) in the whole data structure.
Formula for amortized cost in the Potential Method?
ĉᵢ = cᵢ + Φ(Dᵢ) - Φ(Dᵢ₋₁)
What’s the worst-case time for MULTIPOP?
O(n), if the stack has n items.
Aggregate cost of n stack operations (including MULTIPOP)?
O(n), since each item can only be popped once.
Amortized cost of stack operations using accounting?
PUSH = 2 units, POP/MULTIPOP = 0 units.
Potential function for stack (Potential Method)?
Φ = number of items in the stack
What causes cost in INCREMENT operation?
Flipping bits from 1 to 0 and a 0 to 1.
Worst-case time for a single increment?
O(k), where k is the number of bits.
Aggregate total cost of n increments?
Amortized cost per increment (any method)?
O(1), often exactly 2.
How does the accounting method pay for bit flips?
Pay 2 when setting a bit to 1 (1 now, 1 saved for reset).
Potential function for counter?
Φ = number of 1s in the counter
What happens when a dynamic array is full during insertion?
Allocate a new array (usually double the size), copy all items, then insert the new one.
Worst-case insert time for dynamic array resizing?
O(n) when resize occurs.
Total cost of n inserts with resizing?
O(n)
Amortized cost per insert for dynamic arrays?
O(1), often shown as 3 using accounting.
Accounting method cost breakdown for insert into dynamic arrays?
1 for insert, 1 for future move, 1 for helping move others = 3 units.