Let denote the number of elements present on the stack after operations (including push and pop).
Let denote the allocated size of the stack after operations. We Define a potential energy function
as follows.
Clearly . The amortized cost of the push operation which does not require an expansion of the stack is as follows.
The amortized cost of the push operation which requires doubling of the array is as follows.
The amortized cost of the pop
From the above we can see that that amortized cost of all the operations is when we use the doubling heuristic for the stack.
(b)
Let the load factor and let . We use the following heuristic to handle expansion and compaction of the array."When (i.e. the array is full) increase the size of the array by times. When (i.e. the array about to under flow) reduce the size of the array by half ()".
We define the following potential function and finally show that the amortized complexity of each of
the operations push, pop is .
We now prove that the amortized cost of each operation is bounded by a constant. From the previous problem (since the potential function is same as the previous problem when ) we can observe that push operation when (expansion) has an amortized cost of . We can also observe that whenever expansion does not occur the amortized cost of the push operation is . So we will concentrate on the other possible situations. We use the notation to indicate the load factor of the array after operations on the array.
Amortized cost of push when
Amortized cost of push when ,
Amortized cost of pop when
Amortized cost of pop when
Amortized cost of pop when and
Amortized cost of pop when with compaction
If the operation is a pop and we find that the load factor then we apply compaction. Compaction reduces the size of the allocated array by . So after compaction . The amortized cost of the pop and compaction is as follows.We have proved that the amortized cost of both push and pop with all possible values of the load factor to be .
No comments:
Post a Comment