priority-queue

priority queue with limited space: looking for a good algorithm

两盒软妹~` 提交于 2019-11-27 04:34:40
问题 This is not a homework. I'm using a small "priority queue" (implemented as array at the moment) for storing last N items with smallest value. This is a bit slow - O(N) item insertion time. Current implementation keeps track of largest item in array and discards any items that wouldn't fit into array, but I still would like to reduce number of operations further. looking for a priority queue algorithm that matches following requirements: queue can be implemented as array, which has fixed size

Efficiency of the STL priority_queue

痞子三分冷 提交于 2019-11-27 04:28:30
I have an application (C++) that I think would be well served by an STL priority_queue . The documentation says: Priority_queue is a container adaptor, meaning that it is implemented on top of some underlying container type. By default that underlying type is vector, but a different type may be selected explicitly. and Priority queues are a standard concept, and can be implemented in many different ways; this implementation uses heaps. I had previously assumed that top() is O(1) , and that push() would be a O(logn) (the two reasons I chose the priority_queue in the first place) - but the

Implementing Java Comparator

无人久伴 提交于 2019-11-27 02:45:08
问题 I am trying to write an algorithm which utilizes a min-priority queue, so I looked around on google and found the PriorityQueue. It seems that in order to use it, though, I am going to need to tell it how I want it to prioritize, and that the way to do this is with a comparator (I want to compare specific data fields of my "Node1" objects). More googling presented the idea of creating a new comparator which implements Comparator but overrides the compare method. What I am trying is this (and

Java Priority Queue reordering when editing elements

£可爱£侵袭症+ 提交于 2019-11-27 01:49:20
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 doesn't seem to be working, and I'm stuck trying to figure it out. This is the code for the vertex object

STL Priority Queue on custom class

让人想犯罪 __ 提交于 2019-11-27 00:47:22
问题 I'm having a lot of trouble getting my priority queue to recognize which parameter it should sort by. I've overloaded the less than operator in my custom class but it doesn't seem to use it. Here's the relevant code: Node.h class Node { public: Node(...); ~Node(); bool operator<(Node &aNode); ... } Node.cpp #include "Node.h" bool Node::operator<(Node &aNode) { return (this->getTotalCost() < aNode.getTotalCost()); } getTotalCost() returns an int main.cpp priority_queue<Node*, vector<Node*>

Why does Dijkstra's algorithm use decrease-key?

久未见 提交于 2019-11-26 23:48:15
问题 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 for each neighbor of node: pqueue.insert(distance + distance_to_neighbor, neighbor) But I've been doing some reading regarding the algorithm, and a lot of versions I see use decrease-key as opposed to insert. Why is this, and what are the differences between the two approaches? 回答1: The

PriorityQueue not sorting on add

假如想象 提交于 2019-11-26 22: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.getValue(), true)); } here is the node's compareTo method: @Override public int compareTo(Node n) { if(n

Change priorityQueue to max priorityqueue

随声附和 提交于 2019-11-26 22:29:43
问题 I have priority queue in Java of Integers: PriorityQueue<Integer> pq= new PriorityQueue<Integer>(); When I call pq.poll() I get the minimum element. Question: how to change the code to get the maximum element? 回答1: How about like this: PriorityQueue<Integer> queue = new PriorityQueue<>(10, Collections.reverseOrder()); queue.offer(1); queue.offer(2); queue.offer(3); //... Integer val = null; while( (val = queue.poll()) != null) { System.out.println(val); } The Collections.reverseOrder()

How to implement PriorityBlockingQueue with ThreadPoolExecutor and custom tasks

天涯浪子 提交于 2019-11-26 22:20:53
问题 I've searched a lot but could not find a solutuion to my problem. I have my own class, BaseTask , that uses a ThreadPoolExecutor to handle tasks. I want task prioritization, but when I try to use a PriorityBlockingQueue I get ClassCastException because the ThreadPoolExecutor wraps my Tasks into a FutureTask object. This obviously makes sense because the FutureTask does not implement Comparable , but how would I go on to solve the priority problem? I've read that you could override newTaskFor(

PriorityQueue.toString wrong element order

倖福魔咒の 提交于 2019-11-26 21:02:59
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; } } public class TreeNodeHuffman { public static void main(String[] args) { HuffmanComparator compare = new