Questions tagged [priority-queue]

A priority queue is an abstract data type (ADT) which defines a container for a set of elements, each of which has an associated "priority". The ADT specifies a method to access the highest priority element, which can be either a minimum or a maximum.

A priority queue is an abstract data type (ADT) which defines a container for a set of elements, each of which has an associated "priority". The ADT specifies a method to access the highest priority element, which can be either a minimum or a maximum. Binary heaps are a common choice from amongst the many possible implementations. Note: the priority queue ADT does not guarantee any particular ordering for the set of elements, so ready access to elements other than the highest priority one is not guaranteed to be efficient or even available.

2613 questions
419
votes
13 answers

How do I use a PriorityQueue?

How do I get a PriorityQueue to sort on what I want it to sort on? Also, is there a difference between the offer and add methods?
Svish
  • 152,914
  • 173
  • 462
  • 620
242
votes
12 answers

Priority queue in .Net

I am looking for a .NET implementation of a priority queue or heap data structure Priority queues are data structures that provide more flexibility than simple sorting, because they allow new elements to enter a system at arbitrary intervals. It is…
Doug McClean
  • 14,265
  • 6
  • 48
  • 70
188
votes
20 answers

Change priorityQueue to max priorityqueue

I have priority queue in Java of Integers: PriorityQueue pq= new PriorityQueue(); When I call pq.poll() I get the minimum element. Question: how to change the code to get the maximum element?
Borut Flis
  • 15,715
  • 30
  • 92
  • 119
150
votes
11 answers

declaring a priority_queue in c++ with a custom comparator

I'm trying to declare a priority_queue of nodes, using bool Compare(Node a, Node b) as the comparator function (which is outside the node class). What I currently have is: priority_queue, Compare> openSet; For some reason, I'm…
Steven Morad
  • 2,511
  • 3
  • 19
  • 25
134
votes
9 answers

How can I create Min stl priority_queue?

The default stl priority queue is a Max one (Top function returns the largest element). Say, for simplicity, that it is a priority queue of int values.
amitlicht
  • 2,868
  • 6
  • 23
  • 27
108
votes
3 answers

Why does Dijkstra's algorithm use decrease-key?

Dijkstra's algorithm was taught to me was as follows while pqueue is not empty: distance, node = pqueue.delete_min() if node has been visited: continue else: mark node as visited if node == target: break …
weeb
  • 1,939
  • 4
  • 18
  • 29
101
votes
4 answers

Difference between std::set and std::priority_queue

Since both std::priority_queue and std::set (and std::multiset) are data containers that store elements and allow you to access them in an ordered fashion, and have same insertion complexity O(log n), what are the advantages of using one over the…
penelope
  • 8,251
  • 8
  • 45
  • 87
98
votes
3 answers

What's the difference between heapq and PriorityQueue in python?

In python there's a built-in heapq algorithm that gives you push, pop, nlargest, nsmallest... etc that you can apply to lists. However, there's also the queue.PriorityQueue class that seems to support more or less the same functionality. What's the…
yelsayed
  • 5,236
  • 3
  • 27
  • 38
88
votes
8 answers

Updating Java PriorityQueue when its elements change priority

I'm trying to use a PriorityQueue to order objects using a Comparator. This can be achieved easily, but the objects class variables (with which the comparator calculates priority) may change after the initial insertion. Most people have suggested…
Marcus Whybrow
  • 19,578
  • 9
  • 70
  • 90
86
votes
3 answers

Bug in Microsoft's internal PriorityQueue?

In the .NET Framework in PresentationCore.dll, there is a generic PriorityQueue class whose code can be found here. I wrote a short program to test the sorting, and the results weren't great: using System; using System.Collections.Generic; using…
MathuSum Mut
  • 2,765
  • 3
  • 27
  • 59
82
votes
7 answers

How does Java's PriorityQueue differ from a min-heap?

Why did they name PriorityQueue if you can't insertWithPriority? It seems very similar to a heap. Are there any differences? If no difference, then why was it named PriorityQueue and not Heap?
Jeff Chen
  • 895
  • 1
  • 7
  • 9
82
votes
6 answers

Difference between priority queue and a heap

It seems that a priority queue is just a heap with normal queue operations like insert, delete, top, etc. Is this the correct way to interpret a priority queue? I know you can build priority queues in different ways but if I were to build a priority…
Mars
  • 4,677
  • 8
  • 43
  • 65
78
votes
4 answers

C++ priority_queue with lambda comparator error

I have the following erroneous code which I am trying to compile in VC2010, but I'm getting the error C2974 this only occurs when I include the lambda expression, so I'm guessing it has something to do with that. typedef pair, int>…
ameer
  • 2,598
  • 2
  • 21
  • 36
65
votes
5 answers

Efficient way to implement Priority Queue in Javascript?

Priority Queues have a priority value and data, for every entry. Thus, when adding a new element to the queue, it bubbles up to the surface if it has a higher priority value than elements already in the collection. When one calls pop, we get the…
sova
  • 5,468
  • 10
  • 40
  • 48
64
votes
5 answers

How to remove element not at top from priority_queue?

In my program I need to delete an element from a priority queue that is not at the top. Can that be done? If not, please suggest a way to do so except creating your own heap.
ishan3243
  • 1,870
  • 4
  • 30
  • 49
1
2 3
99 100