Store credit, or 'potential', in the data structure as a whole, not on individual pieces of data.Two-Stack QueueLet
be the data structure after the
operation.
is the "potential energy" ( "bank balance" in credits, for example ) of
.
Let -
![]()
i.e. actual cost + change in potential
![]()
Does this satisfy the Amortization Property?
![]()
![]()
![]()
![]()
![]()
![]()
![]()
if
![]()
The potential,
, must always be at least as large as the initial potential,
.
Usually,
.
Back to indexWhat to choose?... so that
should be large before an expensive operation and much smaller afterward!
is comprised of -
large positive cost + very negative change of potential = a small result.
Let
be the number of elements in S. Suppose S has s elements.
Then -Suppose S has s elements and Dequeue causes x of them to be transferred to T.![]()
![]()
1 + ( s + 1 ) - ( s )
![]()
2
Then -Is![]()
![]()
( pop + transfer ) + ( after ) - ( before )
![]()
( 1 + x ) + ( s - x ) - ( s )
![]()
1
always?
Yes, sinceBy the amortization property -and
.
However, you must always check this condition!![]()
![]()
![]()
![]()
sequence of M Enqueue and Dequeue operations takes at most 2M steps.