Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Priority queue structure used?

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 ?

like image 617
John Retallack Avatar asked Apr 10 '10 15:04

John Retallack


People also ask

Which data structure is used in priority queue?

The binary heap is the most efficient method for implementing the priority queue in the data structure.

What is priority queue uses?

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.

Where is priority queue used in real life?

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.

What is priority queue in data structure with example?

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.


1 Answers

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.

like image 131
R Samuel Klatchko Avatar answered Sep 21 '22 03:09

R Samuel Klatchko