Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

Amortized Analysis

1] If the set of stack operations included in a multipush operation which pushes a


k items onto the stack ,would O(1) bound on the amortized cost of stack operation
continue to hold .
Consider the aggregate method .Justify your answer.

ANS-

No. The time complexity of such a series of operations depends on the


number of pushes (pops vice versa) could be made. Since one
MULTIPUSH needs Θ(k) time, performing n MULTIPUSH operations, each
with k elements, would take Θ(kn) time, leading to amortized cost of Θ(k).

2] Show how to implement a queue with two ordinary stacks so that amortized cost
of each enqueue and dequeue operation is O(1).
Consider accounting method.

ANS-
It is required to model queue data structure with two stacks, such that
amortized cost of operations is O(1). Let us assume that we have two stacks
S1 and S2. The simplest operation is enqueue(x), we model it with just
simple call S1.push(x), so we always put a new element on top of the first
stack. Now, if we want to dequeue() and element, we face a problem, that
the element which we need to return is on the bottom of the stack and there
is no simple way to get it (if there are more than one element in the stack).
To implement the dequeue() function, it is proposed to do the following. We
put all elements that are currently in the S1 stack (let it be k) into S2 buy
calling the S2.push(S1.pop()) k times, if S2 is empty. Notice that the order
of the elements in the S2 stack is now the same as should have been in the
queue, so after this costly operation time, we can model dequeue as a simple
S2.pop() for the next k times and this operation is very cheap. Summing up,
the idea is rather simple:
• enqueue(·) : always push a new element on top of S1.
• dequeue() : always pop an element from S2.
If S2 is empty call S2.push(S1.pop()) until all elements of S1 are in S2.
Return S2.pop(). Let us now take a closer look to the amortized cost of these
operations. We will use accounting method of analysis. The real costs of
operations that we need to pay are the following: putting an element into
queue (on top of S1) costs 1; if second stack is not empty getting an element
costs 1; if S2 is empty and S1 has k elements the cost is 2k + 1, . Consider
the following amortized cost of operations:
• enqueue(·) : amortized cost is 3, from which we put 2 to account and 1 is
the real cost.
• dequeue(·) : amortized cost is 3.
If stack S2 is not empty, we delete the top element
(real cost 1) and put 2 to the account. If stack S2 is
empty, we copy all elements (assume k) from S1 to S2
and take for each copied element 2 from the account (1
for each S1.pop() and S2.push(), so 2k for copying) and
also, we need to call S2.pop() one time to return the
required element. There real cost is 2k + 1. Summing
this up, we get amortized cost 3 if S2 is not empty and
we get 3 = 2k + 1 − 2k + 2 in case if S2 is empty. Notice,
that the number of elements that we move from S1 to S2
for any sequence of operations is always not more than
number of enqueue(·) operation calls and so, for any
execution: since Xn i=0 ti ≤ Xn i=0 ai = 3n.

3]Suppose we perform a sequence of stack operations on a stack whose size never


exceeds k. After k operations we make a copy of the entire S for backup purpose.
Show that the cost of n stack operations including copying the stack is O(n) by
assigning suitable amortized costs to the various stack operations.

ANS.
The cost of n stack operation, including copying the stack, is O(n) by
assigning amortized costs to the various stack operations.
1. So, first assign the 2-2 credits to each push-operation and pop-
operation. Here one credit used to complete the given operation and
other credit used to store with the stack.
2. After K operations the stack has k credits.
3. These k credits can be used to pay for making the backup copy of the
stack. So we need O(k) credits to make a k-sizecopy.
4. Thus, the worst- case scenario for n operation, equal to the number of
credits used, is O(n).

4]Problem Definition
The famous McWidget company manufactures widgets. At its headquarters,
the company has a large display that shows how many widgets have been
manufactured so far. Each time a widget is manufactured, a maintenance
person updates this display. The cost for this update is $c + dm, where c is
a fixed trip charge, d is a charge per display digit that is to be changed, and
m is the number of digits that are to be changed. For example, when the
display is changed from 1399 to 1400, the cost to the company is $c +
3d because 3 digits must be changed. The McWidget company wishes to
amortize the cost of maintaining the display over the widgets that are
manufactured, charging the same amount to each widget. More precisely, we
are looking for an amount $e = amortized(i) that should leavied
against each widget so that the sum of these charges equals or exceeds the
actual cost of maintaining/updating the display ($e*n >= actual
total cost incurred for first n widgets for all n >= 1).
To keep the overall selling price of a widget low, we wish to find as small an
e as possible. Clearly, e > c + d because each time a widget is made, at
least one digit (the least significant one) has to be changed.
Find the amortized cost. Solve using Accounting method.

ANS.

We begin by assigning an amortized cost to the individual operations, and


then we show that these assigned costs satsify Equation (3). Having already
done an amortized analysis using the aggregate method, we see that
Equation (3) is satisfied when we assign an amortized cost of $c + 1.12d to
each display change. Typically, however, the use of the accounting method
is not preceded by an application of the aggregate method and we start by
guessing an amortized cost and then showing that this guess satisfies
Equation (3).

Suppose we assign a guessed amortized cost of $c + 2d for each display


change.

P(n) - P(0) = sum 1 <= i <= n(amortized(i) - actual(i))

= (c + 2d)n - sum1 <= i <= nactual(i)

= (c + 2d)n - (c + (1 + 1/10 + 1/100 + ...)d)n

>= (c + 2d)n - (c + 1.12d)n

>= 0

This analysis also shows us that we can reduce the amortized cost of a
widget to $c + 1.12d.

An alternative proof method that is useful in some analyses involves


distributing the excess charge P(i) - P(0) over various accounting entities,
and using these stored excess charges (called credits) to establish P(i+1) -
P(0) >= 0. For our McWidget example, we use the display digits as the
accounting entities. Initially, each digit is 0 and each digit has a credit of 0
dollars. Suppose we have guessed an amortized cost of $c + (1.111...)d.
When the first widget is manufactured, $c + d of the amortized cost is used
to pay for the update of the display and the remaining $(0.111...)d of the
amortized cost is retained as a credit by the least significant digit of the
display. Similarly, when the second through ninth widgets are manufactured,
$c + d of the amortized cost is used to pay for the update of the display and
the remaining $(0.111...)d of the amortized cost is retained as a credit by the
least significant digit of the display. Following the manufacture of the ninth
widget, the least significant digit of the display has a credit of $(0.999...)d
and the remaining digits have no credit. When the tenth widget is
manufactured, $c + d of the amortized cost are used to pay for the trip
charge and the cost of changing the least significant digit. The least
significant digit now has a credit of $(1.111...)d. Of this credit, $d are used
to pay for the change of the next least significant digit (i.e., the digit in the
tens place), and the remaining $(0.111...)d are transferred to the ten's digit as
a credit. Continuing in this way, we see that when the display shows 99, the
credit on the ten's digit is $(0.999...)d and that on the one's digit (i.e., the
least significant digit) is also $(0.999...)d. When the 100th widget is
manufactured, $c + d of the amortized cost are used to pay for the trip
charge and the cost of changing the least significant digit, and the credit on
the least significant digit becomes $(1.111...)d. Of this credit, $d are used to
pay for the change of the tens digit from 9 to 0, the remaining $(0.111...)d
credit on the one's digit is transferred to the ten's digit. The credit on the ten's
digit now becomes $(1.111...)d. Of this credit, $d are used to pay for the
change of the hundred's digit from 0 to 1, the remaining $(0.111...)d credit
on the ten's digit is transferred to the hundred’s digit.

The above accounting scheme ensures that the credit on each digit of the
display always equals $(0.111...)dv, where v is the value of the digit (e.g.,
when the display is 206 the credit on the one's digit is $(0.666...)d, the credit
on the tens's digit is $0, and that on the hundred’s digit is $(0.222...)d.

From the preceding discussion, it follows that P(n) - P(0) equals the sum of
the digit credits and this sum is always nonnegative. Therefore, Equation (3)
holds for all n.

You might also like