1E

At every stack operation, the charge is twice. First the actual cost of the operation which is performed on the stack is charged and second the cost of copying an element later is charged.

The maximum size of the stack can never exceed k, so there will be k operations. And after every k operations, at least k units will be left.


The amortized cost of the stack operation is constant that is O (1) and the cost to perform the operation on the stack having n elements will be O(n).

2E

• The accounting analysis is done by assuming amortized costs for a given operation.

• In the accounting method, the operations are charged with different costs than its actual cost.

• This cost can be less or more than the actual cost an operation.

• The amount charges for an operation is called as amortized cost.


For the operation i, the actual cost is as follows:

Assume the amortized cost to be as follows:


• For an operation, which is expressed as an exact power of 2, its actual cost will be the sum of the credit which is accumulated and the amortized cost of its own which is one unit.

The credits which are left will be assigned as credit.

• And, if the operation is not the exact power of two, then that operation will use its one unit to pay its actual cost and the cost left which is 2 will be assigned as credit.

If the operation can be expressed in the terms of power of 2 then, the value of c i is as follows:

And the value of the is as follows:

Since, the value of (2n – 3n) is always greater than 1 for n = 0 and all n > 1 therefore, the value of will always be greater than ci for n=0 and n>1.

Now for n=1, the value of is 2 and the value of ci is 1.

Thus, the value of ci is always less than .

Therefore, as per the accounting analysis, the amortized cost is always greater than the actual cost which can be represented as follows:

3E

Consider a variable A.max to hold the higher-order of 1 in A. A.max helps to use the amount limit to the credit resulted by INCREMENTs. That is, using A.max ensures RESET to use not more than the amount available in credit. Initially, A.max is assigned with -1, because initially, there are no 1’s in the counter. A.max is updated when the counter is incremented and reset.

Consider that the counting time to examine and modify a bit as 1 is and the counting time to examine and modify a bit as 0 is .

INCREMENT (A)

1. i=0

2. while i< A.length and A[i] = = 1

3. A[i]=0

4. i=i+1

5. if i < A.length

6. A[i] = 1

7. if i > A.max

8. A.max = i

9. else A.max = -1

Consider operation RESET sets all bits in the counter to 0.

RESET (A)

1. i = 0

2. while i < A.length and A.max

3. if A[i] = = 1

4. A[i] = 0

5. A.max = -1


When a bit is set to 1, $2 will be paid. One dollar is for setting a bit to 1 and another for resetting a bit to 0. That is, $1 is credited for each insertion. Thus, credit has dollars equal to number of 1s in the counter.

• It is known that $1 is required to reset a bit to 0. RESET makes all 1s in the counter to 0.This uses the dollars in credit that is the result of earlier INCREMENTs. Thus, for resetting all 1s to 0s, there is no need of paying dollars.

• In addition to $2 (for increment and reset), $1 is paid to update max. Thus, INCREMENT pays $3 each time ($2 for setting a bit to 1 and $1 for updating A.max with higher-order of 1).

• RESET pays $1 for updating A.max as -1. Since RESET runs loop to reset bits upto A.max and since every 1 bit seen by RESET has $1 of credit on it, the zeroing of bits of A can be completely paid for, by the credit stored on the bits.

• Only $1 more is to paid for resetting max.

Since the INCREMENT and RESET are charging a constant amount $4, the sequence of n INCREMENT and RESET operations takes O(n) time.

Chegg Rip, Introduction to Algorithms, 3rd Edition. [RipVer 0.1] (index)