Goal: prove that insertion is O(1) amortized even though it is O(log n) in the worst case 39:26, 38:28.
Identifying the actual cost
- Elicited from the class: the real cost of an insertion is governed by the number of trees already in the forest, since each merge destroys one tree
40:49,41:19,41:40. - Writing
tfor the old number of trees andt'for the new number:actual cost of insert = O(t − t' + 1)41:40,42:15.
The potential function
Φ(state) = number of trees in the forest
Insert: amortized O(1)
amortized = actual + ΔΦ
= (t − t' + 1) + (t' − t)
= 1
The t terms cancel exactly, leaving a constant 42:58, 43:23.
Note on the
O(·): hiding a constantcin the actual cost just means putting the samecin front of the potential term; it does not affect the conclusion42:58.