Question
Consider the following operation along with Enqueue and Dequeue operations on queues, where is a global parameter.
MultiDequeue(Q){
m = k
while (Q is not empty) and (m > 0) {
Dequeue(Q)
m = m - 1
}
}
What is the worst case time complexity of a sequence of queue operations on an initially empty queue?
Official answer and solution
- Official answer
- A
- From
- Official GATE 2013 answer key
Solution
Use aggregate amortized analysis by tracking the lifecycle of each element enqueued in the queue.
Consider an arbitrary sequence of operations performed on an initially empty queue .
- The permissible operations are
Enqueue,Dequeue, andMultiDequeue. - Since each
Enqueueoperation inserts exactly one element into , at most elements can ever be enqueued across all operations. - An element can be removed by a
Dequeueoperation (whether called directly or called insideMultiDequeue) at most once. - Therefore, across the entire sequence of operations, the total number of times
Dequeue(Q)can be executed is at most . - Each
Enqueuetakes time. Outside the while-loop, eachMultiDequeuetakes time to initialize . The while-loop iterations inMultiDequeueexecuteDequeue(Q), each iteration accounting for the removal of one enqueued element. - Since at most removals can occur in total, the cumulative number of while-loop iterations over all operations in the sequence cannot exceed .
Thus, the total time for the entire sequence of operations is upper-bounded by . Since each operation takes at least time, the sequence takes time. Therefore, the worst-case time complexity is , corresponding to option (A).
A
The answer is the one in the official GATE answer key. The solution is this site's own explanation.
Source
- Paper
- GATE 2013 Computer Science and Information Technology (CS)
- Question
- 44 of 65, printed as CS Q.44
- Section
- Computer Science and Information Technology
Report an error in this question
GATE questions are the work of the organising institutes. This site is independent and is not affiliated with them.