Amortized Analysis
Amortized Analysis
Amortized Analysis
ANS-
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.
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.
>= 0
This analysis also shows us that we can reduce the amortized cost of a
widget to $c + 1.12d.
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.