Question: Amortized cost analysis: If we use two queues data queue and working queue to implement a stack s by the following way. Suppose that both

"Amortized cost analysis:
If we use two queues data queue and working queue to implement a stack s by the
following way. Suppose that both queues have no size limit.
s.push(item)
1. enQueue the item into the data queue.
s.pop()
1. move all the items except the last one from the data queue to the working queue
2. deQueue from the data queue and return
3. now the names of the data queue and the working queue are swapped. That is, working queue -> data queue and data queue -> working queue
(a) Suppose that we would like to analyze the amortized costs using the accounting method. Assume there are k items in the stack before the i-th op XB10 eration. If the i-th operation is a pop operation and we assign 2(k 1) amortized cost to it, then how much amortized cost for a push operation should be assigned? Justify your answer.
(b) If the potential method is used, please define a potential function so that the amortized costs for push and pop are constant time and linear time respectively. Assume that there are k items in the stack before the i-th operation. Justify your answer."

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!