Professional Documents
Culture Documents
Mansi A. Radke
The canteen chai example!
• 1 person 10 minutes in the worst case
• 100 minutes?
• What when the tea pot is full? It can serve like 20 people!
• Accounting method
• Potential method
Aggregate analysis
• We determine an upper bound on the total cost of sequence of n
operations. The average cost per operation is then T(n)/n
• If you start with an empty stack, and you call MULTIPOP n times, it will empty the
stack in the first call and rest of the calls will be O(1)
• If pop, multipop and push all are combined, total cost would be O(n)
00000100 00001100
00000101 00001101 The cost of each INCREMENT operation is linear in number
00001110 of bits flipped
00000110
00001111 In the worst case 01111111 is incremented to 10000000 for
00000111 k = 8.
00010000
So, for n operations, we have O(n*k)
Consider a sequence of n INCREMENT
operations
A[0] flips every time • So total number of flips =
A[1] flips n/2 times
A[2] flips n/4 times
Like a normal stack we implement an ordered stack as a double linked list ( maintaining a
pointer to the top element).
(a) What is the worst-case running time of each of the operations Init, Pop, Push,
and Destroy?
(b) Argue that the amortized running time of all operations is O(1).
Thank you !
Any Questions?