priority-queue

Change priority of items in a priority queue

五迷三道 提交于 2019-12-20 10:46:33
问题 Using Scala 2.9 to implement a kind of Dijkstra algorithm (pseudo code) val queue = new PriorityQueue queue.insert(...) while (!queue.isEmpty) { val u = queue.extractMin queue.foreach { v => if (condition(u, v)) queue.decreaseKey(v, newPriority) } } I'd like to change priority of an item in Scala's collection.mutable.PriorityQueue . Therefore tried to remove item change priority reinsert into queue. But I can't find a method to either update priority or remove a specific item (not necessarily

Why is using a std::multiset as a priority queue faster than using a std::priority_queue?

二次信任 提交于 2019-12-20 10:44:52
问题 I try to replace std::multiset with std::priority_queue. But I was dissapointed with the speed results. Running time of the algorithm increase by 50%... Here are the corresponding commands: top() = begin(); pop() = erase(knn.begin()); push() = insert(); I am surprised with the speed of priority_queue implementation, I expected different results (better for PQ)... Conceptually, the multiset is being used as a priority queue. Why are the priority queue and the multiset have such different

stl priority_queue of C++ with struct

≯℡__Kan透↙ 提交于 2019-12-20 10:43:35
问题 How can we use STL priority_queue for struct ? Any illustration of pushing & popping , where struct has multiple data-types? Say : struct thing { int a; char b;} glass[10]; . Now how can i put this struct on priority_queue using 'int a' for ordering ? 回答1: Here is a slightly modified answer to your original question, which you deleted for no apparent reason. The original contained enough information for you to figure this out, but here it goes: provide a less than comparison that uses the int

Changing Java PriorityQueue to a Max PQ [duplicate]

安稳与你 提交于 2019-12-20 10:27:30
问题 This question already has answers here : Change priorityQueue to max priorityqueue (14 answers) Closed 3 years ago . 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.

Difference between priority queue and a heap

↘锁芯ラ 提交于 2019-12-20 08:49:06
问题 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

Difference between std::set and std::priority_queue

随声附和 提交于 2019-12-20 08:38:12
问题 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 other (or, what kind of situations call for the one or the other?)? While I know that the underlying structures are different, I am not as much interested in the difference in their implementation as I am in the comparison their performance and

Bug in Microsoft's internal PriorityQueue<T>?

半城伤御伤魂 提交于 2019-12-20 08:31:17
问题 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 =

Wrong order in java.util.PriorityQueue and specific Comparator

爱⌒轻易说出口 提交于 2019-12-20 07:47:14
问题 i am very confused with this little example of java.util.PriorityQueue and my own Comparator: In this code i get a wrong order in the queue. The result is: 5,8,7 instead of 5,7,8 Is there anything wrong with my Comparator<Vertex> ? Thank you for your help. public class Test { public static void main(String[] args) { PriorityQueue<Vertex> priorityQueue = new PriorityQueue<Vertex>(new Comparator() { @Override public int compare(Object o1, Object o2) { Vertex u = (Vertex) o1; Vertex v = (Vertex)

Transform a std::multimap into std::priority_queue

半世苍凉 提交于 2019-12-20 07:36:35
问题 I have a function written using std::multimap which is excruciatingly slow precisely because of the std::multimap . After analysis, I realized that I am only using the std::multimap as a heap , so I am trying to substitute it with a std::priority_queue which only allows for heap operations, in the hope it will be faster for this usage. Of course, the element type of std::priority_queue would need to be std::pair<mmKey, mmValue> , and then I would pass a custom comparator to the std::priority

AsyncTask on Executor and PriorityBlockingQueue implementation issue

坚强是说给别人听的谎言 提交于 2019-12-20 07:18:31
问题 On the wave of this SO question and with many hints from another one, I'm trying to implement an AsyncTask variant with tasks that can be prioritized. In my CustomAsyncTask class I have: public abstract class CustomAsyncTask<Params, Progress, Result> { private static int CORE_POOL_SIZE = 1; private static int MAXIMUM_POOL_SIZE = 1; private static final int KEEP_ALIVE = 1; private static final ThreadFactory sThreadFactory = new ThreadFactory() { private final AtomicInteger mCount = new