priority-queue

Changing Java PriorityQueue to a Max PQ [duplicate]

旧巷老猫 提交于 2019-12-03 00:13:22
This question already has an answer here: Change priorityQueue to max priorityqueue 14 answers The Priority Queue implementation in the Java standard library appears to be a min Priority Queue which I found somewhat confusing. In order to turn it into a max one I created a custom comparator object. Comparator<Integer> cmp = new Comparator<Integer>() { public int compare( Integer x, Integer y ) { return y - x; } }; I was wondering if there was a more elegant solution. Essentially I wan't a generic priority queue that could be used to implement Dijkstras etc. I didn't even realise there would be

How to remove element not at top from priority_queue?

…衆ロ難τιáo~ 提交于 2019-12-02 18:01:09
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. The standard priority_queue<T> can be customized through inheritance. It has protected members c and comp that can be referenced in a descendant class. template<typename T> class custom_priority_queue : public std::priority_queue<T, std::vector<T>> { public: bool remove(const T& value) { auto it = std::find(this->c.begin(), this->c.end(), value); if (it != this->c.end()) { this->c.erase(it); std::make_heap(this->c.begin()

Difference between priority queue and a heap

大城市里の小女人 提交于 2019-12-02 17:32:52
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 queue from a heap is it necessary to create a priority queue class and give instructions for building a heap and the queue operations or is it not really necessary to build the class? What I mean is if I have a function to build a heap and functions to do operations like insert, delete, do I need to put all these functions in a class or can I just

How to do an efficient priority update in STL priority_queue?

家住魔仙堡 提交于 2019-12-02 16:38:09
I have a priority_queue of some object: typedef priority_queue<Object> Queue; Queue queue; From time to time, the priority of one of the objects may change - I need to be able to update the priority of that object in the queue in an efficient way. Currently I am using this method which works but seems inefficient: Queue newQueue; while (!queue.empty()) { Object obj=queue.top(); queue.pop(); if (priorityHasChanged(obj)) newQueue.push_back(Object(new_priority)); else newQueue.push_back(obj); } newQueue.swap(queue); // this only works because I actually subclassed the priority_queue // class and

Bug in Microsoft's internal PriorityQueue<T>?

China☆狼群 提交于 2019-12-02 16:03:29
In the .NET Framework in PresentationCore.dll, there is a generic PriorityQueue<T> 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 System.Diagnostics; using MS.Internal; namespace ConsoleTest { public static class ConsoleTest { public static void Main() { PriorityQueue<int> values = new PriorityQueue<int>(6, Comparer<int>.Default); Random random = new Random(88); for (int i = 0; i < 6; i++) values.Push(random.Next(0, 10000000)); int lastValue = int.MinValue; int temp; while

Pair inside priority queue

被刻印的时光 ゝ 提交于 2019-12-02 15:56:00
I am trying to store pairs in priority queue and I am using a compare function that compares second value of each pair. #include<iostream> #include<queue> #include<utility> using namespace std; class CompareDist { public: bool operator()(pair<int,int> n1,pair<int,int> n2) { return n1.second>n2.second; } }; int main() { priority_queue<pair<int,int>,CompareDist> pq; } When I compile this I get an error error: no type named ‘value_type’ in ‘class CompareDist’ What could be the reason.I am new to STL. This is what priority_queue looks like: template< class T, class Container = std::vector<T>,

Creating a python priority Queue

痞子三分冷 提交于 2019-12-02 15:48:24
问题 I would like to build a priority queue in python in which the queue contains different dictionaries with their priority numbers. So when a "get function" is called, the dictionary with the highest priority(lowest number) will be pulled out of the queue and when "add function" is called, the new dictionary will be added to the queue and sorted based on its priority number. Please do help out... Thanks in advance! 回答1: Use the heapq module in the standard library. You don't specify how you

Efficient implementation of binary heaps

不问归期 提交于 2019-12-02 15:42:59
I'm looking for information on how to implement binary heaps efficiently. I feel like there should be a nice article somewhere about implementing heaps efficiently, but I haven't found one. In fact I've been unable to find any resources on the matter of efficient implementation beyond the basics like storing the heap in an array. I'm looking for techniques for making a fast binary heap beyond the ones I describe below. I've already written a C++ implementation that is faster than Microsoft Visual C++'s and GCC's std::priority_queue or using std::make_heap, std::push_heap and std::pop_heap. The

Priority Queue remove items with same priority first one entered

孤人 提交于 2019-12-02 15:32:32
问题 I've got a priority queue created and working that enters items in order and removes them in order. Even if two numbers have the same priority, it removes the one that was entered first. If there are three numbers that have the same priority, it does not remove the first one. How would I go about doing this, or should it do this? Dequeue function: public void deQueue(Animal item) { item = items.elements[0]; items.elements[0] = items.elements[numItems - 1]; numItems--; items.ReheapDown(0,

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

蹲街弑〆低调 提交于 2019-12-02 14:36:56
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? Add() works like an insertWithPriority. You can define priority for the type that you want using the constructor: PriorityQueue(int, java.util.Comparator) look under http://download.oracle.com/javase/1,5.0/docs/api/java/util/PriorityQueue.html The order the Comparator gives will represent the priority in the queue. The default PriorityQueue is implemented with Min-Heap, that is the top element is the