A method for counting Amortized Time.
Method
- Assign each operation to receive fixed credit amount .
- Assign each operation an artificial cost to each operation (no need to be actual cost)
- Some operations keep extra and that amount is stored as credit to pay more expensive operations later.
- Prove invariant () Each operation takes amortized time
Example
Suppose a multi-pop Stack with:
- Push: push a element to top of stack
- Pop: pop top element off stack
- Multi-Pop: pop top element off stack Then, we assign:
- Give each operation received amount
- Assign push to cost 1 dollar
- Assign pop to cost 1 dollar
- Multi pop to cost dollar for number of elements popped Proving invariant:
- Initially: ,
- Push:
- Assume before push
- Then,
- Then, invariant holds as
- Pop:
- Assume before pop
- Then,
- Then, invariant holds as
- Multi-pop:
- Assume before multipop
- Let be number of items popped
- Then,
- Thus,
- Finally, note that is an invariant