0% found this document useful (0 votes)
36 views9 pages

Amortized Analysis of A Binary Counter: Andreas Klappenecker

This document summarizes three different methods for analyzing the amortized runtime of increment operations on a k-bit binary counter: 1) The aggregate method shows that the total number of bit flips over n increments is O(n), so the amortized cost per operation is O(1). 2) The accounting method assigns an amortized cost of 2 per increment, using credits to pay for flipping bits from 1 to 0 later. 3) The potential method uses a potential function equal to the number of 1s in the counter. It shows that the difference in potential plus the actual cost is always less than or equal to 2, proving an amortized cost of O(1)

Uploaded by

Tarun verma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views9 pages

Amortized Analysis of A Binary Counter: Andreas Klappenecker

This document summarizes three different methods for analyzing the amortized runtime of increment operations on a k-bit binary counter: 1) The aggregate method shows that the total number of bit flips over n increments is O(n), so the amortized cost per operation is O(1). 2) The accounting method assigns an amortized cost of 2 per increment, using credits to pay for flipping bits from 1 to 0 later. 3) The potential method uses a potential function equal to the number of 1s in the counter. It shows that the difference in potential plus the actual cost is always less than or equal to 2, proving an amortized cost of O(1)

Uploaded by

Tarun verma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Amortized Analysis of a Binary

Counter
Andreas Klappenecker
Binary Counter

A binary k-bit counter can be implemented with a k-element binary


array. The counter is initially 0.

The only operation is increment(A), which adds 1 to the current


number in the counter.

The increment operation can be implemented using the grade-school


ripple-carry algorithm.


Aggregate Method
The worst case running time occurs when all k bits are flipped, so
increment(A) has running time O(k).

In a sequence of n increment operations, few increments will cause


that many bits to flip. Indeed,

bit 0 flips with every increment

bit 1 flips with every 2nd increment

bit 2 flips with every 4th increment, ...


Aggregate Method

Total number of bit flips in n increment operations is

n + n/2 + n/4 + … + n/2k < n(1/(1-1/2))= 2n

So total cost of the sequence is O(n).

Amortized cost per operation is O(n)/n = O(1).

21
Accounting Method for k-Bit

The actual cost for an increment operation is the number of bits


flipped.

We can assign an amortized cost of 2 for each increment operation.


The main idea is to use 1 to flip the bit from 0 to 1 and store 1
credit to flip it back to 0 later.

Accounting Method for k-Bit Counter

1
0 0 0 0 0 0 0 1 0 0
1 1 1
0 0 0 0 1 0 0 1 0 1
1 1 1
0 0 0 1 0 0 0 1 1 0
1 1 1 1 1
0 0 0 1 1 0 0 1 1 1
Accounting Method

All changes from 1 to 0 are paid for with previously stored credit
(never go into red)

The amortized time per operation is O(1)


Potential Method
Potential Function Bi = number of 1s in counter after ith increment.

Suppose ith operation resets ti bits.

Actual cost: ci = ti + 1

Notice that Bi <= Bi-1 - ti + 1

- if Bi = 0, then Bi-1 = ti = k

- if Bi > 0, then Bi = Bi-1 - ti + 1


Potential Method

Difference in Potentials:

Bi - Bi-1 <= (Bi-1 - ti + 1) - Bi-1 = -ti + 1

Amortized cost: ci + Bi - Bi-1 <= ti + 1 -ti + 1 = 2

Advantage of the potential method: We can use it to analyze


counters that do not start from 0, see CLRS.

You might also like