



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




*Each operation runs in O(1) time.
A
B
C
S
Push( S,D ) A
B
C
D
S
Pop( S ) A
B
C
S
MultiPop( S,k )
MultiPop( S, k ) while not Stack-Empty( S ) and k ≠ 0 do Pop( S ) k ← k-
A
B
C
D
S
MultiPop( S, 3) A
B
S
E
A S
MultiPop( S,n )
S
A
A (^) ν
2 1
*Aggregate Method: Each object can be popped at most once for each time it is pushed.
A
B
C
S Push( S,B )
A S S MultiPop( S,2 )
S Push( S,A )
A A
B
Push( S,C )
S S Push( S,B ) MultiPop( S,5 )
A
B
S
Amortized cost
Actual cost
Change in potential due to operation
Push( S,A )
Push( S,B )
Push( S,C )
MultiPop( S,2 )
Push( S,B )
MultiPop( S,5 ) EmptyStack
A
B
C
S
A S S S
A A
B
S S
A
B
S
Φ = size of S
0
Push( S,A )
Push( S,B )
Push( S,C )
MultiPop( S,2 )
Push( S,B )
EmptyStack MultiPop( S,5 )
1 2 3 1 2 0
3 2 1 0
Flips every call
Flips every fourth call
Flips every other call
3 2 1 0
...