Amortized Analysis

What is Amortized Analysis?

Sometimes, an operation looks expensive. But if we look at many operations together, the cost is not so bad. That’s where amortized analysis helps.

It tells us the average cost per operation, over a series of operations. It’s not the worst-case. It's not the best-case. It’s the long-term average.

Why Use Amortized Analysis?

Let’s say you are buying bus tickets every day. One day, you buy a 30-day pass. It’s expensive that day, but cheap in the long run. On average, your cost per ride is low.

Computers work the same way. Some operations are costly, but not always. If we average over time, things look better.

Real-Life Example: A Piggy Bank

Imagine you have a piggy bank. You drop coins every day. Once it’s full, you break it and buy a new one. Breaking the piggy bank is costly. But you don’t do it every day.

Over many days, the average cost per day is small. This is like amortized analysis.

Computer Science Example: Dynamic Array

A dynamic array is like a resizable list. In Java or Python, it’s like ArrayList or list.

When it gets full, it doubles its size. That doubling step is expensive.

But most of the time, adding an item is quick. Only sometimes it’s slow.

So, the average cost per add is low.

How It Works: Cost Over Time

Suppose adding to the array takes:

But resizing happens rarely. After copying n items, we get space for n more.

So, if we do many adds, the average cost is about 2 units per add.

That’s amortized analysis.

Three Methods of Amortized Analysis

  1. Aggregate Method: Total cost divided by total number of operations.
  2. Accounting Method: Charge extra for cheap operations. Save the extra for later use.
  3. Potential Method: Use a potential function to track "stored energy" or cost.

Example Using Aggregate Method

Suppose we perform 8 operations. Let’s say the total cost is 16.

Then the amortized cost is 16 ÷ 8 = 2.

Example Using Accounting Method

We add items to a dynamic array. Normal add costs 1. But we charge 3 for every add.

The extra 2 is saved for when resizing happens.

When we double the array, we use the saved credits to pay for copying.

Example Using Potential Method

Think of a potential as stored cost. Like water in a tank.

Some operations increase the potential. Some use it.

We define a function that calculates the potential after each step.

Amortized cost = actual cost + change in potential.

When to Use Amortized Analysis?

Use it when some operations are rare but expensive.

Good examples are:

Summary

Fun Tip

Think of amortized cost like paying for a gym membership. One-time payment, but you use it many times.

The average cost per workout is low!