Artavazd
Artavazd

Reputation: 175

Amortized analysis on Operations of stack

Let's Suppose we perform a sequence of operations (i.e., top, push, and pop) on an initially empty stack so that the stack size does not exceed k. After every k operations, we make a copy of the entire stack for backup purposes. How can I use the accounting method to prove that each stack operation will have O(1) amortized complexity regardless of whether the stack is copied or not?

Upvotes: 0

Views: 416

Answers (1)

MBo
MBo

Reputation: 80325

You have k simple operations and one copy operation that takes at most k time.

Together you make p elementary operations, and p < k + k = 2*k

So amortized complexity per operation is p/k <= 2*k/k = 2 = O(1)

Upvotes: 1

Related Questions