While searching for some functions in C++ standard library documentation I read that push and pop for priority queues needs constant time.
http://www.cplusplus.com/reference/stl/priority_queue/push/
Constant (in the priority_queue). Although notice that push_heap operates in logarithmic time.
My question is what kind of data structure is used to maintain a priority queue with O(1) for push and pop ?
The binary heap is the most efficient method for implementing the priority queue in the data structure.
Operating Systems: Priority queues are used to select the next process to run, ensuring high-priority tasks run before low-priority ones. It is also applied for load balancing, and interrupt handling.
The hospital emergency queue is an ideal real-life example of a priority queue. In this queue of patients, the patient with the most critical situation is the first in a queue, and the patient who doesn't need immediate medical attention will be the last.
In a priority queue, generally, the value of an element is considered for assigning the priority. For example, the element with the highest value is assigned the highest priority and the element with the lowest value is assigned the lowest priority.
I assume you are referring to cplusplus.com's page.
Earlier on the page it says:
This member function effectively calls the member function push_back of the underlying container object, and then calls the push_heap algorithm to keep the heap property of priority_queues.
So, after the O(1)
push, the data structure has lost its heap property invariant and will then always follow that with an O(log N)
call to restore that property. In other words, the O(1)
operation is only one part of the entire operation; the full operation is O(1) + O(log N)
which is the same as O(log N)
.
I guess the reason they mention that is that priority queue is an adapter and they are trying to emphasize the difference between what the underlying container does vs what the adapter does.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With