priority-queue

Difference between std::set and std::priority_queue

泪湿孤枕 提交于 2019-12-02 14:12:01
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 suitability for various uses. Note: I know about the no-duplicates in a set. That's why I also mentioned

Java: strange order of queue made from priority queue

雨燕双飞 提交于 2019-12-02 14:08:52
问题 I wrote a maze solving program which is supposed to support DFS, BFS, A*, Dijkstra's, and greedy algorithm. Anyway, I chose PriorityQueue for my frontier data structure since I thought a priority can behave like a queue, stack, or priority queue depends on the implementation of the comparator. This is how I implemented my comparator to turn the priority queue into a queue: / Since the "natural ordering" of a priority queue has the least element at the head and a conventional comparator

How can I create Min stl priority_queue?

孤街浪徒 提交于 2019-12-02 13:50:09
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. Use std::greater as the comparison function: std::priority_queue<int, std::vector<int>, std::greater<int> > my_min_heap; One way would be to define a suitable comparator with which to operate on the ordinary priority queue, such that its priority gets reversed: #include <iostream> #include <queue> using namespace std; struct compare { bool operator()(const int& l, const int& r) { return l > r; } }; int main() { priority_queue<int,vector<int>,

Wrong order in java.util.PriorityQueue and specific Comparator

时光毁灭记忆、已成空白 提交于 2019-12-02 12:57:01
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) o2; return Integer.compare(new Integer(u.distance), new Integer(v.distance)); } }); Vertex vertex1 =

Creating a python priority Queue

て烟熏妆下的殇ゞ 提交于 2019-12-02 11:40:19
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! Use the heapq module in the standard library. You don't specify how you wanted to associate priorities with dictionaries, but here's a simple implementation: import heapq class

Does a PriorityQueue allow elements already within the queue to be reordered?

久未见 提交于 2019-12-02 11:03:04
问题 I want to augment or lower the priority of items in a PriorityQueue : for example, I might be downloading a long list of images and suddenly want the thirtieth one to have highest priority. As I understand it, poll() always returns the queue object with the lowest value (as determined by a comparator). If I can lower the value of an item already in a queue (e.g. if this value is determined by an int in the object and I reduce the int value in some other function), will it be returned first by

Serializing a priority queue in scala

╄→尐↘猪︶ㄣ 提交于 2019-12-02 11:01:07
问题 I am trying to serialize a mutable PriorityQueue in scala (2.10) and am getting a NotSerializableException when writing the object to an ObjectOutputStream. I made a simple test case: import java.io.{ByteArrayOutputStream, ObjectOutputStream} import scala.collection.mutable object Test extends App { val pq = new mutable.PriorityQueue[Int]() val oos = new ObjectOutputStream(new ByteArrayOutputStream()) oos.writeObject(pq) } The exception is Exception in thread "main" java.io

Trying to make a priority queue of a custom class using a variable in a struct of that class

梦想的初衷 提交于 2019-12-02 10:33:21
问题 So here are my classes, the goal is to make a priority queue of bnode that go in order, so that the bnode with the symbol with the lowest count has the highest priority. Here's my code: struct symbol { explicit symbol(char av = 0, int ac = 0) : value(av), count(ac) { } char value; // actual symbol, by default 0 (empty) int count; // count of the symbol, by default 0 }; // symbol // compare two symbols // symbol with a lower count is "less than" symbol with a higher count inline bool operator<

AsyncTask on Executor and PriorityBlockingQueue implementation issue

偶尔善良 提交于 2019-12-02 08:35:58
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 AtomicInteger(1); public Thread newThread(Runnable r) { return new Thread(r, "CustomAsyncTask #" + mCount

Iterating through PriorityQueue doesn't yield ordered results

孤街浪徒 提交于 2019-12-02 08:08:05
问题 import java.util.*; class Priority{ public static void main(String args[]){ PriorityQueue<String> queue=new PriorityQueue<String>(); queue.add("Amit"); queue.add("Vijay"); queue.add("Karan"); queue.add("Jai"); queue.add("Rahul"); System.out.println("head:"+queue.element()); System.out.println("head:"+queue.peek()); System.out.println("iterating the queue elements:"); Iterator itr=queue.iterator(); while(itr.hasNext()){ System.out.println(itr.next()); } queue.remove(); queue.poll(); System.out