0% found this document useful (0 votes)
462 views3 pages

Introduction To Algorithms, Cormen Et Al, Chap17 Solutions

The document provides solutions to exercises from Chapter 17 on amortized analysis. It gives the solutions to Exercise 17.1-3 on analyzing the amortized cost of a counter data structure where incrementing costs more for powers of 2. It provides the solution to Exercise 17.2-2 on charging a flat fee per operation to cover the actual costs. It also gives the solution to Exercise 17.2-3 on modifying an increment-only counter to support reset by tracking the maximum index set to 1.

Uploaded by

eab
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)
462 views3 pages

Introduction To Algorithms, Cormen Et Al, Chap17 Solutions

The document provides solutions to exercises from Chapter 17 on amortized analysis. It gives the solutions to Exercise 17.1-3 on analyzing the amortized cost of a counter data structure where incrementing costs more for powers of 2. It provides the solution to Exercise 17.2-2 on charging a flat fee per operation to cover the actual costs. It also gives the solution to Exercise 17.2-3 on modifying an increment-only counter to support reset by tracking the maximum index set to 1.

Uploaded by

eab
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/ 3

Selected Solutions for Chapter 17:

Amortized Analysis

Solution to Exercise 17.1-3


Let ci D cost of ith operation.
(
i if i is an exact power of 2 ;
ci D
1 otherwise :
Operation
1
2
3
4
5
6
7
8
9
10
::
:

Cost
1
2
1
4
1
1
1
8
1
1
::
:

n operations cost
n
X
iD1

ci  n C

lg n
X

2j D n C .2n

1/ < 3n :

j D0

(Note: Ignoring floor in upper bound of


Average cost of operation D

2j .)

Total cost
<3.
# operations

By aggregate analysis, the amortized cost per operation D O.1/.

Solution to Exercise 17.2-2


Let ci D cost of ith operation.

17-2

Selected Solutions for Chapter 17: Amortized Analysis

ci D

i if i is an exact power of 2 ;
1 otherwise :

Charge each operation $3 (amortized cost cyi ).





If i is not an exact power of 2, pay $1, and store $2 as credit.


If i is an exact power of 2, pay $i , using stored credit.

Operation
1
2
3
4
5
6
7
8
9
10
::
:

Cost
3
3
3
3
3
3
3
3
3
3
::
:

Actual cost
1
2
1
4
1
1
1
8
1
1
::
:

Credit remaining
2
3
5
4
6
8
10
5
7
9
::
:

Since the amortized cost is $3 per operation,

n
X

cyi D 3n.

i D1

We know from Exercise 17.1-3 that

n
X

ci < 3n.

iD1

Then we have

n
X
iD1

cyi 

n
X

ci ) credit D amortized cost

actual cost  0.

iD1

Since the amortized cost of each operation is O.1/, and the amount of credit never
goes negative, the total cost of n operations is O.n/.

Solution to Exercise 17.2-3


We introduce a new field A:max to hold the index of the high-order 1 in A. Initially,
A:max is set to 1, since the low-order bit of A is at index 0, and there are initially
no 1s in A. The value of A:max is updated as appropriate when the counter is
incremented or reset, and we use this value to limit how much of A must be looked
at to reset it. By controlling the cost of R ESET in this way, we can limit it to an
amount that can be covered by credit from earlier I NCREMENTs.

Selected Solutions for Chapter 17: Amortized Analysis

17-3

I NCREMENT.A/
i D0
while i < A:length and Ai == 1
Ai D 0
i D i C1
if i < A:length
Ai D 1
// Additions to books I NCREMENT start here.
if i > A:max
A:max D i
else A:max D 1
R ESET.A/
for i D 0 to A:max
Ai D 0
A:max D 1
As for the counter in the book, we assume that it costs $1 to flip a bit. In addition,
we assume it costs $1 to update A:max.
Setting and resetting of bits by I NCREMENT will work exactly as for the original
counter in the book: $1 will pay to set one bit to 1; $1 will be placed on the bit
that is set to 1 as credit; the credit on each 1 bit will pay to reset the bit during
incrementing.
In addition, well use $1 to pay to update max, and if max increases, well place an
additional $1 of credit on the new high-order 1. (If max doesnt increase, we can
just waste that $1it wont be needed.) Since R ESET manipulates bits at positions
only up to A:max, and since each bit up to there must have become the high-order 1
at some time before the high-order 1 got up to A:max, every bit seen by R ESET
has $1 of credit on it. So the zeroing of bits of A by R ESET can be completely paid
for by the credit stored on the bits. We just need $1 to pay for resetting max.
Thus charging $4 for each I NCREMENT and $1 for each R ESET is sufficient, so the
sequence of n I NCREMENT and R ESET operations takes O.n/ time.

You might also like