# Lecture 20 - Amortized Analysis Flashcards

What is amortized analysis?

What is the goal?

What are the 2 methods we use?

Analyze a sequence of operations on a data structure.

• We will talk about average cost in the worst case (i.e. not averaging over a distribution of inputs. No probability!)

Goal: Show that although some individual operations may be expensive, on average the cost per operation is small.

- aggregate analysis
- accounting method

What are the running times of Push and Pop on a stack? What about a sequence of each?

O(1)

O(n) for a sequence

What’s the worst case running time of a sequence of Multipop?

O(n^2)

What’s the average over the n operations of push pop? why?

O(1).

Each object can be popped only once per time that it is pushed.

• Have ≤ n PUSHes ⇒ ≤ n POPs, including those in MULTIPOP.

• Therefore, total cost = O(n).

What is the cost of the INCREMENT function we saw?

Cost of INCREMENT = Q(# of bits flipped)

What is the total # of flips per INCREMENT? Explain how this was achieved.

What is the cost of n INCREMENTs. and Average?

2*n

Every bit i is flipped 1/2^i times, thus geometric.

O(n)

O(1)

What is the amortized cost in accounting method? How is it handled when cost is higher than actual cost?

Amortized cost = amount we charge different operations

• When amortized cost is higher than the actual cost, store the difference on specific objects in the data structure as credit.

• Use credit later to pay for operations whose actual cost is higher than the amortized cost.

How is the accounting method different than aggregate method?

Accounting method differs from aggregate analysis:

• In the accounting method, different operations can have different costs.

• In aggregate analysis, all operations have same cost.

What’s the restriction we put on the credit in the accounting method? Explain how this impacts the sum of amortized cost and actual cost.

The credit cannot be negative.

Therefore, the sum of actual cost of operations has to be larger or equal to the sum of amortized cost.

What is a good amortized cost for Push Pop Multipop? Explain the intuition behind this.

What would be the total amortized cost?

Push = 2

Pop = 0

Multipop = 0

Intuition: When pushing an object, pay $2.

• $1 pays for the PUSH.

• $1 is prepayment for it being popped by either POP or MULTIPOP.

• Since each object has $1, which is credit, the credit can never go negative.

Total amortized cost (= O(n)) is an upper bound on total actual cost.

What is a good amortized cost for setting a bit in INCREMENT? Explain the intuition behind this.

What would be the total amortized cost?

Charge $2 to set a bit to 1.

INTUITION

• $1 pays for setting a bit to 1.

• $1 is prepayment for flipping it back to 0.

• Have $1 of credit for every 1 in the counter.

• Therefore, credit ≥ 0.

Amortized cost of INCREMENT:

• Cost of resetting bits to 0 is paid by credit.

• At most 1 bit is set to 1.

• Therefore, amortized cost ≤ $2.

• For n operations, amortized cost = O(n).

Using aggregate analysis, what is the amortized cost per operation of inserting in a dynamic table? Explain how this was derived.

3. Cost of insertion = 1 ci = actual cost of ith operation If not full, ci = 1 If full, have i−1 items in the table at the start of the i'th operation. Have to copy all i − 1 existing items, then insert ith item ⇒ ci = i

Sum of ci <= n + sum2^j (j0->logn) = n+2n = 3n.

Using Accounting method, what is the amortized cost per operation of inserting in a dynamic table? Explain how this was derived.

Prove the credit never goes negative.

Charge $3 per insertion of x.

• $1 pays for x’s insertion.

• $1 pays for x to be moved in the future.

• $1 pays for some other item to be moved

Prove the credit never goes negative:

• size=m before and size=2m after expansion.

• Assume that the expansion used up all the credit, thus that there is no credit available after the expansion.

• We will expand again after another m insertions.

• Each insertion will put $1 on one of the m items that were in the table just after expansion and will put $1 on the item inserted.

• Have $2m of credit by next expansion, when there are 2m items to move. Just enough to pay for the expansion…