Docsity

Amortized Cost Analysis of Algorithms: ECE 537 Homework Solutions, Assignments of Electrical and Electronics Engineering

Toggle sidebar
bg1
ECE 537 - Foundations of Computing
Prof. Sen
Homework #9
Solutions
1. Work problem 17.2-2 in the Cormen et al. text.
Solution: Let cibe the cost of the ith operation. Where
ci=i, if iis an exact power of 2,
1,otherwise.
If we charge $3 per operation (i.e., ˆc= 3), then if iis not a power of 2, we’ll be paying $1
for the operation, and saving $2 on the balance sheet, and when iis a power of 2, we’ll need to
pay $i using some of the credits stored on the balance sheet. Furthermore, if there are mtotal
operations, then Pm
i=1 ˆc= 3m.
We can see from the table below that the balance sheet can never go negative.
operation am. cost actual cost balance
1 3 1 2
2 3 2 3
3 3 1 5
4 3 4 4
5 3 1 6
6 3 1 8
7 3 1 10
8 3 8 5
9 3 1 7
.
.
..
.
.
Therefore, Pm
i=1 ˆc≥Pm
i=1 c, and since Pm
i=1 ˆc= 3m, the amortized cost per operation is Θ(1).
2. Work problem 17.3-3 in the Cormen et al. text.
Solution: Let us denote the initial configuration of the binary heap data structure using D0, and
the i-th configuration using Di. Furthermore, use nito denote the number of data elements stored
in Di. We have seen that the Insert and DeleteMin operations can be implemented in O(log n)
time using a binary heap.1Next, let di(x)denote the depth of vertex xin Di. Consider the
following potential function:
Φ(Di) = X
x∈Di
(di(x) + 1)
=ni+X
x∈Di
di(x).
1In order to be consistent with the lecture notes, I’m using the name DeleteMin rather than Extract-Min.
1
pf3

Partial preview of the text

Download Amortized Cost Analysis of Algorithms: ECE 537 Homework Solutions and more Assignments Electrical and Electronics Engineering in PDF only on Docsity!

ECE 537 - Foundations of Computing Prof. Sen Homework # Solutions

  1. Work problem 17.2-2 in the Cormen et al. text. Solution: Let ci be the cost of the ith operation. Where

ci =

i, if i is an exact power of 2, 1 , otherwise.

If we charge $3 per operation (i.e., ˆc = 3), then if i is not a power of 2, we’ll be paying $ for the operation, and saving $2 on the balance sheet, and when i is a power of 2, we’ll need to pay $i using some of the credits stored on the balance sheet. Furthermore, if there are m total operations, then

∑m i=1 cˆ^ = 3m. We can see from the table below that the balance sheet can never go negative.

operation am. cost actual cost balance 1 3 1 2 2 3 2 3 3 3 1 5 4 3 4 4 5 3 1 6 6 3 1 8 7 3 1 10 8 3 8 5 9 3 1 7 .. .

Therefore,

∑m i=1 ˆc^ ≥^

∑m i=1 c, and since^

∑m i=1 cˆ^ = 3m, the amortized cost per operation is^ Θ(1).

  1. Work problem 17.3-3 in the Cormen et al. text. Solution: Let us denote the initial configuration of the binary heap data structure using D 0 , and the i-th configuration using Di. Furthermore, use ni to denote the number of data elements stored in Di. We have seen that the Insert and DeleteMin operations can be implemented in O(log n) time using a binary heap.^1 Next, let di(x) denote the depth of vertex x in Di. Consider the following potential function:

Φ(Di) =

x∈Di

(di(x) + 1)

= ni +

x∈Di

di(x).

(^1) In order to be consistent with the lecture notes, I’m using the name DeleteMin rather than Extract-Min.

Note that Φ(D 0 ) = 0, and Φ(Di) > 0 for all i > 0. Thus, this is a suitably defined potential function. Now, if the i-th operation is an Insert , the sum in the previous potential function will only change by an amount that is equal to the depth of the new vertex, which has been inserted as the rightmost vertex on the last level. Thus, the sum will increase by blg nic, and the change in potential due to this operation is:

∆Φ(Di) = 1 + blg nic = O(log n).

Therefore, the amortized cost of the Insert operation is:

ˆci = O(log n) + O(log n) = O(log n).

For the DeleteMin operation we have:

∆Φ(Di) = −(1 + blg ni− 1 c = −O(log n),

and the amortized cost of the DeleteMin operation is:

ˆci = O(log n) − O(log n) = O(log n).

  1. We can generalize the analysis of the MTF algorithm provided in a class lecture by considering what happens when the M T F algorithm moves an accessed element some fixed fraction of the way towards the head of the list, instead of all the way to the head of the list. Specifically, prove that if M T F (d) (d ≥ 1) is an algorithm which takes an element at position μ and moves it p = dμ/de − 1 elements closer to the front of the list after a Search or Insert operation, then

∑^ m

i=

cM T F i (d) ≤ d

∑^ m

i=

cAi + XA^ − F A^ − m

where A is any self-organizing list algorithm that obeys the other assumptions given in class. Note that when d = 1, this reduces to the analysis provided in the lecture. Solution: This proof is very similar to the one given in the lecture. Once again we simultaneously maintain two initially empty lists—one using M T F (d), and the other using A. In this case, however, the potential function will be d times the number of inversions in M T F (d)’s list with respect to A’s list. This potential function works because it is initially zero, and can never be nonnegative. We will show that the amortized cost of any operation in M T F (d)’s list is at most d(2cA^ − 1). As before, we assume that any DICTIONARY ADT operation that takes place at position μ in M T F (d)’s list takes place at position α in A’s list. Consider first the Search operation. Let Iα be the element stored at position α in A’s list. In the M T F (d) list, Iα is moved