N items are stored in a sorted doubly linked list. For a delete operation, a pointer is provided to the record to be deleted. For a decrease-key operation, a pointer is provided to the record on which the operation is to be performed. An algorithm performs the following operations on the list in this order: Θ(N) delete, O(log N) insert, O(log N) find, and Θ(N) decrease-key. What is the time complexity of all these operations put together?
N items are stored in a sorted doubly linked list. For a delete operation, a pointer is provided to the record to be deleted. For a decrease-key operation, a pointer is provided to the record on which the operation is to be performed. An algorithm performs the following operations on the list in this order: Θ(N) delete, O(log N) insert, O(log N) find, and Θ(N) decrease-key. What is the time complexity of all these operations put together? Correct Answer O(N<sup>2</sup>)
Here, it is given that pointer is provided to the record on which the decrease key operation is to be performed. So, time complexity of decrease key operation is Θ (1)
For insert – Θ (N), we need to insert the element at the end of the sorted list.
For delete – Θ (1) time is needed, as pointer is directly given
For decrease key – Θ (N), because after decreasing the key value, all the elements must be sorted again.
Now combining all the operations together:
For delete = Θ (N)* Θ (1)
For insert = Θ (log N)*Θ (N)
For find = Θ (log N)*Θ (N)
For decrease-key = Θ (N)* Θ (N)
So, overall complexity = Θ (N)* Θ (1 ) + Θ (logN)* Θ (N) + Θ (logN)* Θ (N)+ Θ (N)* Θ (N)
Time complexity of all these operations put together = Θ (N2)