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.
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.
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.
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.
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.
Suppose we perform 8 operations. Let’s say the total cost is 16.
Then the amortized cost is 16 ÷ 8 = 2.
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.
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.
Use it when some operations are rare but expensive.
Good examples are:
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!