See Chapter 5.1 of the Okasaki textbook for a summary of two approaches to amortized analysis, the banker’s method and the physicist’s method. Here, we will say just a few extra words about the accounting for the FastQueue
implementation from before.
Recall that when evaluating the running times of functions, we often use symbolic constants instead of concrete units (such as seconds) in order to abstract away from low-level, incidental, and variable characteristics of an execution environment. And because Big-O analysis, furthermore, abstracts away the particular constants of a function, symbolic constants (such as k) are often replaced by particular small constants (such as 1).
Once comfortable with such reasoning, we can often jump directly to saying things like “the amortized cost of enqueue
is 2” and “the amortized cost of dequeue
is 1”. In the meantime, let us spend the extra effort to explicitly account for the actual (symbolic) costs of operations before reducing them to their asymptotic characteristics.
To avoid confusion with the notation c for credits from the textbook, here we will use the metavariable k to range over constants. Let the following constants stand for actual costs of the various operations on a Queue
that contains m elements in the back
list:
enqueue
;peek
;dequeue
when List.reverse
is not called (the cheap case); anddequeue
when List.reverse
is called (the expensive case).Notice the actual cost of dequeue
in the expensive case is linear in the size of the back
list.
We now revisit the amortized analyses from the textbook using the symbolic costs above.
We allocate k4 credits for each element added to the back
, where k4 was the cost for List.reverse
to process one element.
enqueue
: k1 + k4 - 0 = k1 + k4peek
: k2 + 0 - 0 = k2dequeue
(cheap): k3 + 0 - 0 = k3dequeue
(expensive): (k4m + k5) + 0 - k4m = k5Thus, all of the operations run in O(1) amortized time.
We define the potential to be k4m where m is the length of the back
.
enqueue
: k1 + k4(m+1) - k4m = k1 + k4peek
: k2 + k4m - k4m = k2dequeue
(cheap): k3 + k4m - k4m = k3dequeue
(expensive): (k4m + k5) + k40 - k4m = k5Thus, all of the operations run in O(1) amortized time.
BinomialHeap.insert
runs in O(1) amortized time.)