Definition of amortized cost
- Setting: a data structure supports operations A, B, C. Their amortized costs are
t_A,t_B,t_Cif any sequence ofn_AA-operations,n_BB-operations andn_CC-operations takes total time at mostn_A·t_A + n_B·t_B + n_C·t_C2:03,3:08. - Contrast with worst case: a red-black tree searches in
O(log n)worst case. Saying searches takeO(log n)amortized only means thatksearches together cost at mostk·log n3:52,4:07. - Individual operations may be expensive — even linear time — as long as the running total respects the bound
4:07,4:23.
The potential function method
Requirements on the potential function Φ 5:00, 5:38:
Φmaps the state space of the data structure to the non-negative reals.Φ(empty data structure) = 0.
- Perform
koperations with actual running timest_1, …, t_k. - States
S_0(before anything) throughS_k, whereS_iis the state after operationi. - Amortized cost of operation i :=
t_i + Φ(S_i) − Φ(S_{i−1}).
Why it is a valid upper bound
The proof is a telescoping sum 8:59, 9:34, 9:57:
Σ_i (t_i + ΔΦ_i) = Σ_i t_i + Φ(S_k) − Φ(S_0)
= Σ_i t_i + Φ(S_k) (since Φ(S_0) = 0)
≥ Σ_i t_i (since Φ ≥ 0)
- Therefore bounding the per-operation cost by
t_i + ΔΦyields a correct upper bound on the true total cost9:57. - To quote a bound for one type of operation (insert, delete, search), take the worst case of
t_i + ΔΦover all instances of that operation type10:27. - Only these two conditions —
Φ(∅) = 0andΦ ≥ 0— are needed for the whole argument to work1:18:47,1:19:19.