The accounting method assigns each operation a charge, its amortized cost, that can differ from its actual cost. Cheap operations save the excess charge as credits associated with the data structure or its entries. An expensive operation spends these previously saved credits. The essential invariant is that the credit balance never becomes negative.
If the initial balance is zero, operation has actual cost , charge , and leaves balance , then
Thus a nonnegative credit balance makes the total charge an upper bound on the total actual cost for every operation sequence. In particular, a constant time charge proves a linear time total. The argument is a deterministic amortized analysis; it requires no probability distribution on the operations. A nonzero initial balance must be included as an additive term in the bound.
Yes: starting from zero, increments of the decimal counter cost digit changes, uniformly in the number of digits. An increment changes the trailing digits equal to into and increases the next digit. Number digits from at the least significant end. Digit changes once every increments, so the exact number of changes is
Here the floor function counts carries reaching each position, and the last equality sums a geometric series. Since digit changes on every increment, for . The loop tests and carry handling add only a constant amount per changed digit and per operation, giving the same time complexity.
An accounting method explanation is also possible. Charge two credits for each change of digit : one pays for that change and one joins a common bank. For increments the higher digits change at most times, so their costs can be covered from that bank; the displayed prefix bound ensures that it never goes negative.
The formula allows wraparound modulo ; alternatively, restrict to increments before overflow. Initializing the array itself costs if that is counted separately. If the initial digits are arbitrary, the number of changes at position is at most , giving in Big O notation. Starting with all digits equal to makes the first increment cost , so the initial-state convention matters.
For an initially zero binary counter, bit changes precisely on increments whose indices are divisible by . Weighting each such change by gives the exact total
For and , put . All summands with vanish; each of the other summands is at most . Therefore
This is sharper than simply charging for each of all positions when is small. To show that the order is best possible, use for . For every ,
Thus the asymptotic analysis gives a tight bound. If wraparound is excluded, , and this becomes , often written for . If wraparound is allowed and , it becomes . For , the cost is zero.
For completeness, the same conclusion cannot be asserted uniformly over arbitrary initial binary counter states. An initial value makes the first increment change every bit, costing . A general upper bound follows by replacing each floor function in the count by a ceiling function:
To see the second inequality, split at : for , use , and for , use . For the worst initial-state cost is consequently . This qualification separates initialization from the usual amortized analysis starting at zero.
Use two permanent stacks, and , and one temporary stack, . At operation boundaries is empty. Maintain the representation invariant that the deque, read from front to back, is the top-to-bottom contents of followed by the bottom-to-top contents of . Thus the tops of and are the two accessible ends. Maintain their sizes using two counters, requiring only additional storage in Big O notation.
Adding at the front pushes onto ; adding at the back pushes onto . Removing at an end whose permanent stack is nonempty simply pops that stack. If both permanent stacks are empty, report an empty deque.
Suppose a front removal finds empty and contains elements. Rebalance as follows:
  • Move the top elements of onto .
  • Move all remaining elements of onto .
  • Move all elements of back onto .
  • Pop and return the top of .
The parked elements are the back half of the deque; the unparked elements are the front half. Moving the front half onto reverses their stack order and exposes the correct front element. Parking and restoring the back half reverses it twice, preserving its original order in . The temporary stack is empty again. For a back removal with empty, exchange the roles of and throughout. Rebalance approximately half the elements, so both ends retain accessible elements after a large transfer.
For the amortized analysis, count each push or pop as one unit and use the potential method with
Initially the deque is empty, so , and is always nonnegative. An ordinary insertion or removal costs one unit and changes the size difference by at most one, giving amortized cost at most .
For a rebalancing removal, let . The parked half makes two transfers per element and the other half makes one; each transfer is a pop and a push. Including the final removal, the actual cost is
Before rebalancing the absolute size difference is . Immediately after the split it is at most , and after the removal it is at most . Hence
Size tests and counter updates change only these constants. Summing the amortized costs and using nonnegative final potential proves amortized running time for all four operations, with three stacks and additional storage. An individual rebalancing operation can still take linear time in the current size.

Articles by others on the same topic (0)

There are currently no matching articles.