priority-queue

C++ priority_queue with lambda comparator error

纵然是瞬间 提交于 2019-11-27 17:59:14
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 priority_queue<int , vector<int>, greater<int>> pq; Works as expected. Any ideas what I'm doing wrong? Is

Change priorityQueue to max priorityqueue

血红的双手。 提交于 2019-11-27 16:57:52
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? 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() provides a Comparator that would sort the elements in the PriorityQueue in a the oposite order to their natural

When would I use a priority queue? [closed]

ε祈祈猫儿з 提交于 2019-11-27 14:35:13
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 6 years ago . The only example of using the priority queue I know of, is the Dijkstra's Algorithm (for calculating minimum cost) In what other situations would it be useful? 回答1: Here's a practical example - for a business application: You're running a hospital and patients are coming in. There

Java PriorityQueue with fixed size

纵饮孤独 提交于 2019-11-27 12:20:50
I am calculating a large number of possible resulting combinations of an algortihm. To sort this combinations I rate them with a double value und store them in PriorityQueue. Currently, there are about 200k items in that queue which is pretty much memory intesive. Acutally, I only need lets say the best 1000 or 100 of all items in the list. So I just started to ask myself if there is a way to have a priority queue with a fixed size in Java. I should behave like this: Is the item better than one of the allready stored? If yes, insert it to the according position and throw the element with the

What's faster: inserting into a priority queue, or sorting retrospectively?

社会主义新天地 提交于 2019-11-27 09:42:07
问题 What's faster: inserting into a priority queue, or sorting retrospectively? I am generating some items that I need to be sorted at the end. I was wondering, what is faster in terms of complexity: inserting them directly in a priority_queue or a similar data structure, or using a sort algorithm at end? 回答1: Inserting n items into a priority queue will have asymptotic complexity O( n log n ) so in terms of complexity, it’s not more efficient than using sort once, at the end. Whether it’s more

Java - PriorityQueue vs sorted LinkedList

六眼飞鱼酱① 提交于 2019-11-27 06:40:35
问题 Which implementation is less "heavy": PriorityQueue or a sorted LinkedList (using a Comparator)? I want to have all the items sorted. The insertion will be very frequent and ocasionally I will have to run all the list to make some operations. 回答1: A LinkedList is the worst choice. Either use an ArrayList (or, more generally, a RandomAccess implementor), or PriorityQueue . If you do use a list, sort it only before iterating over its contents, not after every insert. One thing to note is that

declaring a priority_queue in c++ with a custom comparator

早过忘川 提交于 2019-11-27 06:10:20
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<Node, vector<Node>, bool Compare()> openSet; priority_queue<Node, vector<Node>, Compare<Node, Node>> openSet; How

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

放肆的年华 提交于 2019-11-27 05:42:19
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 elements from the PriorityQueue Store the kth smallest value as a local variable Push removed k

How to implement O(logn) decrease-key operation for min-heap based Priority Queue?

瘦欲@ 提交于 2019-11-27 05:10:17
问题 I am working on an application that demonstrates the Djikstra's algorithm , and to use it, I need to restore the heap property when my elements' value is decreased. The problem regarding the complexity is that when the algorithm changes the value of an element, that element's index in the internal structure (heap in this case) used for the priority queue is unknown . As such, I currently need to do an O(n) search, in order to recover the index, before I can perform an actual decrease-key on

Limited concurrency level task scheduler (with task priority) handling wrapped tasks

我的未来我决定 提交于 2019-11-27 04:43:23
问题 I'm having a hard time finding a task scheduler on which I can schedule prioritised tasks but can also handle "wrapped" tasks. It is something like what Task.Run tries to solve, but you cannot specify a task scheduler to Task.Run . I have been using a QueuedTaskScheduler from the Parallel Extensions Extras Samples to solve the task priority requirement (also suggested by this post). Here is my example: class Program { private static QueuedTaskScheduler queueScheduler = new QueuedTaskScheduler