next up previous
Next: Up: Previous:

Example

In our example, \(\Phi(D_i) \;=\; b_i\), the number of 1s in Di (counter).

Let ti = number of bits reset to 0 on the ith call to Increment.

Thus, the actual cost ci is at most (cost of reset) + (cost to set one) = \(t_i \;+\; 1\).

We know that \(b_i \;\leq\; b_{i-1} \;-\; t_i \;+\; 1\)
Thus, \(\Phi(D_i) \;-\; \Phi(D_{i-1}) \;\leq\; (b_{i-1} \;-\; t_i \;+\; 1)
\;-\; b_{i-1}\)
\(=\; 1 \;-\; t_i\)
\(\hat{c_i} \;=\; c_i \;+\; \Phi(D_i) \;-\; \Phi(D_{i-1})\)
\(\leq\; t_i \;+\; 1 \;+\; (1 \;-\; t_i)\)
= 2

If \(\Phi(D_0) \;=\; 0\), then \(\Phi(D_i) \;\geq\; 0\) for all i, and the total amortized cost is an upper bound to the actual cost.

The counter starts at zero, \(\Phi(D_0) \;=\; 0\).

\(\hat{c_i}\) = O(2)
n calls is O(n)


next up previous
Next: Up: Previous: