priority-queue

PriorityQueue/Heap Update

半世苍凉 提交于 2019-11-26 20:06:38
问题 Does Java have an easy way to reevaluate a heap once the priority of an object in a PriorityQueue has changed? I can't find any sign of it in Javadoc , but there has to be a way to do it somehow, right? I'm currently removing the object then re-adding it but that's obviously slower than running update on the heap. 回答1: You might need to implement such a heap yourself. You need to have some handle to the position of the item in the heap, and some methods to push the item up or down when its

C++ priority_queue with lambda comparator error

你。 提交于 2019-11-26 19:14:50
问题 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<pair<int, int>, int> adjlist_edge; priority_queue< adjlist_edge , vector<adjlist_edge>, [](adjlist_edge a, adjlist_edge b) -> bool { if(a.second > b.second){ return true; } else { return false; } }> adjlist_pq; I know the form of the template definition is correct as

How to iterate over a PriorityQueue?

Deadly 提交于 2019-11-26 16:05:50
问题 for (Event e : pq) doesn't iterate in the priority order. while(!pq.isEmpty()){ Event e = pq.poll(); } This works but empties the queue. 回答1: From the Javadocs: The Iterator provided in method iterator() is not guaranteed to traverse the elements of the PriorityQueue in any particular order. If you need ordered traversal, consider using Arrays.sort(pq.toArray()) . There are probably other equivalent mechanisms. 回答2: You can't traverse a Priority Queue in that order because of the underlying

How to restore the PriorityQueue to its initial state before the method call?

微笑、不失礼 提交于 2019-11-26 11:40:45
问题 I am doing a practice problem Practice IT Kth Smallest This problem is basically you\'re passed in a PriorityQueue and a certain k, and you are to return the kth smallest value in that PriorityQueue. You are also to restore the PriorityQueue to its initial state and can use one stack or queue as an auxiliary data structure. My higher level pseudo thinking is that because the PriorityQueue acts as a min heap already, from Java PriorityQueue, all I really have to do (my algorithm) is: Remove k

PriorityQueue.toString wrong element order

ε祈祈猫儿з 提交于 2019-11-26 10:01:46
问题 I am trying to make a priority queue in java with the nodes with the lowest frequency in priority. However, my comparator is not working and the output is very weird. I believe I need to change my comparator but I am not sure how to change it. Here is my code: public class HuffmanComparator implements Comparator<TreeNodeHuffman> { public int compare(TreeNodeHuffman p1, TreeNodeHuffman p2) { if (p1.frequency < p2.frequency) return -1; if (p1.frequency > p2.frequency) return 1; return 0; } }

Java Priority Queue reordering when editing elements

*爱你&永不变心* 提交于 2019-11-26 09:46:04
问题 I\'m trying to implement Dijkstra\'s algorithm for finding shortest paths using a priority queue. In each step of the algorithm, I remove the vertex with the shortest distance from the priority queue, and then update the distances for each of its neighbors in the priority queue. Now I read that a Priority Queue in Java won\'t reorder when you edit the elements in it (the elements that determine the ordering), so I tried to force it to reorder by inserting and removing a dummy vertex. But this

Is there a PriorityQueue implementation with fixed capacity and custom comparator?

孤者浪人 提交于 2019-11-26 08:08:27
问题 Related questions: Java PriorityQueue with fixed size How do I use a PriorityQueue? get indexes of n smallest elements in an array Scala: Is there a way to use PriorityQueue like I would in Java? I have a very large data set (more than 5 millions items) and I need to get N largest items from it. The most natural way to do it is to use heap/priority queue storing only top N items . There are several good implementations of priority queue for JVM (Scala/Java), namely: scala.collection.mutable

declaring a priority_queue in c++ with a custom comparator

这一生的挚爱 提交于 2019-11-26 07:56:18
问题 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<Node, vector<Node>, Compare> openSet; For some reason, I\'m getting Error: \"Compare\" is not a type name Changing the declaration to priority_queue <Node, vector<Node>, bool Compare> gives me Error: expected a \'>\' I\'ve also tried: priority_queue<Node, vector<Node>, Compare()> openSet; priority_queue

PriorityQueue not sorting on add

和自甴很熟 提交于 2019-11-26 06:44:40
问题 I have a Priority Queue in which I add a Node object to, where the Nodes should be sorted by a value that they contain. For some reason, the priority queue will not sort the Nodes on add. If anyone can see something wrong with this or has any guidance, I appreciate it. Here is a brief example: PriorityQueue<Node> PQ = new PriorityQueue<Node>(); //for each entry create a node and add it to the PriorityQueue for(Entry<Character,Integer> entry : entries){ PQ.add(new Node(entry.getKey(),entry

Updating Java PriorityQueue when its elements change priority

拜拜、爱过 提交于 2019-11-26 04:22:38
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 the simple solution of removing the object, updating the values and reinserting it again, as this is when the priority queue's comparator is put into action. Is there a better way other than just creating a wrapper class around the PriorityQueue to do this? You have to remove and re-insert, as the queue works by putting new elements in the