priority-queue

Severe bugs in SimplePriorityQueue example on MSDN

允我心安 提交于 2019-12-10 11:24:14
问题 I need to use a concurrent priority queue, and I was considering adapting the SimplePriorityQueue<TPriority, TValue> sample given in the How to: Add Bounding and Blocking Functionality to a Collection tutorial on MSDN. However, I was surprised at the severity of the bugs that the said sample seems to have. Could someone verify whether these issues are really present? 1) A race hazard exists between TryAdd and ToArray , which can cause an ArgumentException to be thrown from the latter. The

Strange ordering in PriorityQueue in Java [duplicate]

倖福魔咒の 提交于 2019-12-10 10:50:40
问题 This question already has answers here : The built-in iterator for java's PriorityQueue does not traverse the data structure in any particular order. Why? (5 answers) Closed 3 years ago . I am trying to use priority queue to keep an ordered list of integers. in a simple exaple like this: PriorityQueue<Integer> queue = new PriorityQueue<>(); queue.offer(3000); queue.offer(1999); queue.offer(999); for(Integer i : queue) System.out.println(i); This prints 999 3000 1999 This is not what I am

How to add a single map entry to priority queue

僤鯓⒐⒋嵵緔 提交于 2019-12-10 10:34:51
问题 At the moment I have to add the whole map, as shown in the last line. PriorityQueue<Map.Entry<String, Integer>> sortedCells = new PriorityQueue<Map.Entry<String, Integer>>(3, new mem()); Map<String,Integer> pn = new HashMap<String,Integer>(); pn.put("hello", 1); pn.put("bye", 3); pn.put("goodbye", 8); sortedCells.addAll(pn.entrySet()); What if I just wanted to add ("word" 5) If i do sortedCells.add("word",5) I get an argument error. How can I add a single element? 回答1: You should add a Map

Can I access elements of a Priority Que using an iterator?

风格不统一 提交于 2019-12-10 04:17:37
问题 Vectors and Linked Lists Vectors are stored in memory serially, and therefore any element may be accessed using the operator[] , just as in an array. A linked list contains elements which may not be stored continuously in memory, and therefore a random element must be accessed by following pointers, using an iterator. (You probably already knew this.) Advantage of Priority Que Recently I discovered the 'priority queue', which works kind of like a stack, but elements are push() -ed into the

Getting a unique_ptr out of a priority queue

依然范特西╮ 提交于 2019-12-09 18:18:25
问题 I am maintaining a set of unique_ptr instances in a priority_queue . At some point, I want to get the first element and remove it from the queue. However, this always produces a compiler error. See sample code below. int main () { std::priority_queue<std::unique_ptr<int>> queue; queue.push(std::unique_ptr<int>(new int(42))); std::unique_ptr<int> myInt = std::move(queue.top()); return 1; } This produces the following compiler error (gcc 4.8.0): uptrtest.cpp: In function ‘int main()’: uptrtest

Is there a library for C that provides priority queues? [closed]

喜你入骨 提交于 2019-12-09 16:07:51
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 2 years ago . Is there a library for C that provides priority queues? I'm interested in open source libraries that are commonly installed on Linux machines, something of the kind of glib, which provides some data structures. 回答1: Some random link: PQLib libpqueue pqueue-heap-c Edit: In general, books dealing with Linux Kernel

Does java have an indexed minimum priority queue?

你离开我真会死。 提交于 2019-12-09 10:05:09
问题 I need it for an implementation of Dijkstra's algorithm, and I do have my own implementation but documenting my code would be easier with java's own classes. 回答1: No, Java standard library has no such data structure. I think most people use this: http://algs4.cs.princeton.edu/24pq/IndexMinPQ.java.html 回答2: What do you mean 'indexed'? Priority queue doesn't support indexing, unless it won't be queue any more. Java supports standard Priority Queue like C++ STL. It can be found in java.util

Remove an element from the middle of an std::heap

我的未来我决定 提交于 2019-12-09 08:27:41
问题 I'm using a priority queue as a scheduler with one extra requirement. I need to be able to cancel scheduled items. This equates to removing an item from the middle of the priority queue. I can't use std::priority_queue as access to any element other than top is protected. I'm trying to use the algorithm 's heap functions. But I'm still missing the piece I need. When I remove an element I from the middle of the heap I want it to rebuild itself efficiently. C++ provides these heap functions:

initialization for STL priority queue

拈花ヽ惹草 提交于 2019-12-09 06:57:39
问题 I'm still confused about priority queue in STL. Here is the objective I wanna achieve, say: I have a structure called Record, which contains a string word and a int counter. For example: I have many records of these (in the sample program, only 5), now I want to keep top N records(in sample, 3). I know now that I could overload operator < in Record, and put all records in a vector, and then initialize the priority_queue like: priority_queue< Record, vector<Record>, less<Record> > myQ (myVec

Does a binary heap support the decrease-key operation?

拜拜、爱过 提交于 2019-12-09 02:44:31
问题 According to http://en.wikipedia.org/wiki/Heap_%28data_structure%29#Comparison_of_theoretic_bounds_for_variants, it takes Θ(logn) (which translates to O(logn)) to perform the decrease-key operation. However, there seems to be no site that includes a binary heap implementation with a decrease-key operation. Given therefore the lack of implementations on the web, is it possible to perform the decrease-key operation in a binary heap? 回答1: I figured this out: In order to perform a decrease-key in