0

I have read many explanations of amortized analysis and how it differs from average-case analysis. However, I have not found a single explanation that showed how, for a particular example for which both kinds of analysis are sensible, the two would give asymptotically different results.

The most wide-spread example of amortized running time analysis shows that appending an element to a dynamic array takes O(1) amortized time (where the running time of the operation is O(n) if the array's length is an exact power of 2, and O(1) otherwise). I believe that, if we consider all array lengths equally likely, then the average-case analysis will give the same O(1) answer.

So, could you please provide an example to show that amortized analysis and average-case analysis may give asymptotically different results?

AlwaysLearning
  • 7,257
  • 4
  • 33
  • 68
  • [Difference between average case and amortized analysis](https://stackoverflow.com/questions/7333376/difference-between-average-case-and-amortized-analysis) discusses the difference, but doesn't discuss when they'd have asymptotically different results – Mooing Duck Jan 26 '23 at 16:02

4 Answers4

1

Consider a dynamic array supporting push and pop from the end. In this example, the array capacity will double when push is called on a full array and halve when pop leaves the array size 1/2 of the capacity. pop on an empty array does nothing.

Note that this is not how dynamic arrays are "supposed" to work. To maintain O(1) amortized complexity, the array capacity should only halve when the size is alpha times the capacity, for alpha < 1/2.

In the bad dynamic array, when considering both operations, neither has O(1) amortized complexity, because alternating between them when the capacity is near 2x the size can produce Ω(n) time complexity for both operations repeatedly.

However, if you consider all sequences of push and pop to be equally likely, both operations have O(1) average time complexity, for two reasons:

  1. First, since the sequences are random, I believe the size of the array will mostly be O(1). This is a random walk on the natural numbers.

  2. Second, the array will be near size a power of 2 only rarely.

This shows an example where amortized complexity is strictly greater than average complexity.

jbapple
  • 3,297
  • 1
  • 24
  • 38
  • Yes, so in this case the distribution for the average-case analysis is very different than the sequence considered for the amortized analysis. However, this is not a regular case for amortized analysis, because all operations in the sequence exhibit the worst case. – AlwaysLearning Jan 28 '23 at 17:10
0

They never have different asymptotically different results. average-case means that weird data might not trigger the average case and might be slower. asymptotic analysis means that even weird data will have the same performance. But on average they'll always have the same complexity.

Where they differ is the worst-case analysis. For algorithms where slowdowns come every few items regardless of their values, then the worst-case and the average-case are the same, and we often call this "asymptotic analysis". For algorithms that can have slowdowns based on the data itself, the worst-case and average-case are different, and we do not call either "asymptotic".

Mooing Duck
  • 64,318
  • 19
  • 100
  • 158
0

In "Pairing Heaps with Costless Meld", the author gives a priority queue with O(0) time per meld. Obviously, the average time per meld is greater than that.

jbapple
  • 3,297
  • 1
  • 24
  • 38
0

Consider any data structure with worst-case and best-case inserts and removes taking I and R time. Now use the physicist's argument and give the structure a potential of nR, where n is the number of values in the structure. Each insert increases the potential by R, so the total amortized cost of an insert is I+R. However, each remove decreases the potential by R. Thus, each removal has an amortized cost of R-R=0!

The average cost is R; the amortized cost is 0; these are different.

jbapple
  • 3,297
  • 1
  • 24
  • 38