Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Priority Queue remove complexity time

What is the complexity (big-oh) for the remove() function on the Priority Queue class in Java? I can't find anything documented anywhere, I think it's O(n), considering you have to find the element before you remove it and then reshuffle the tree. but I've seen others that disagree and think it's O(logn). Any ideas?

like image 495
samturner Avatar asked Oct 04 '12 01:10

samturner


People also ask

What is the time complexity of removing an item from a priority queue?

In java, there are two remove functions. remove() -> This is to remove the head/root, it takes O(logN) time. remove(Object o) -> This is to remove an arbitrary object. Finding this object takes O(N) time, and removing it takes O(logN) time.

What is the time complexity of priority queue?

To always get the largest element, we use priority queues. The time complexity would be: Creation of priority queue takes O(n) time.

What is remove in priority queue?

The remove() method of PriorityQueue class removes a single instance of the specified element from this queue, only if it is present.

What will happen if remove () operation is performed in priority queue?

Geek, have you ever wondered what will happen if calls of remove() method exceed the elements present in the queue. In this scenario, it will continue to remove the elements that were there, and thereafter it will not find any element to remove priority-wise, so it will throw an exception which is as follows.


1 Answers

The confusion is actually caused by your "remove" function. In java, there are two remove functions.

  1. remove() -> This is to remove the head/root, it takes O(logN) time.

  2. remove(Object o) -> This is to remove an arbitrary object. Finding this object takes O(N) time, and removing it takes O(logN) time.

like image 171
GC001 Avatar answered Oct 02 '22 11:10

GC001