priority-queue

What is the difference between binary heaps and binomial heaps?

纵然是瞬间 提交于 2019-11-28 19:35:09
I need to know the main difference between binary and binomial heaps regardless of the their structure difference that binary heaps can have only two child (tree representation) and binomial heaps can have any number of children. I am actually just wondering that what so special in organizing the binomial tree structure in such a way that the first child have on one node second have two third have four and so on? What if, if we use some normal tree for heaps without restriction of two child and then apply the union procedure and just make one heap the left child of the other heaps? The key

How to implement Priority Queues in Python?

[亡魂溺海] 提交于 2019-11-28 19:16:57
Sorry for such a silly question but Python docs are confusing.. . Link 1: Queue Implementation http://docs.python.org/library/queue.html It says thats Queue has a contruct for priority queue. But I could not find how to implement it. class Queue.PriorityQueue(maxsize=0) Link 2: Heap Implementation http://docs.python.org/library/heapq.html Here they says that we can implement priority queues indirectly using heapq pq = [] # list of entries arranged in a heap entry_finder = {} # mapping of tasks to entries REMOVED = '<removed-task>' # placeholder for a removed task counter = itertools.count() #

When should I use a TreeMap over a PriorityQueue and vice versa?

拥有回忆 提交于 2019-11-28 18:44:31
问题 Seems they both let you retrieve the minimum, which is what I need for Prim's algorithm, and force me to remove and reinsert a key to update its value. Is there any advantage of using one over the other, not just for this example, but generally speaking? 回答1: Generally speaking, it is less work to track only the minimum element, using a heap. A tree is more organized, and it requires more computation to maintain that organization. But if you need to access any key, and not just the minimum, a

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

被刻印的时光 ゝ 提交于 2019-11-28 16:34:06
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? 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 efficient in practice really depends. You need to test. In fact, in practice, even continued insertion into a

A priority queue which allows efficient priority update?

自古美人都是妖i 提交于 2019-11-28 15:13:16
问题 UPDATE : Here's my implementation of Hashed Timing Wheels. Please let me know if you have an idea to improve the performance and concurrency. (20-Jan-2009) // Sample usage: public static void main(String[] args) throws Exception { Timer timer = new HashedWheelTimer(); for (int i = 0; i < 100000; i ++) { timer.newTimeout(new TimerTask() { public void run(Timeout timeout) throws Exception { // Extend another second. timeout.extend(); } }, 1000, TimeUnit.MILLISECONDS); } } UPDATE : I solved this

Java - PriorityQueue vs sorted LinkedList

六眼飞鱼酱① 提交于 2019-11-28 13:19:43
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. 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 the PriorityQueue iterator does not provide the elements in order; you'll actually have to remove the

Making a Java PriorityQueue into a stable priority queue

╄→尐↘猪︶ㄣ 提交于 2019-11-28 12:25:51
I'm trying to implement a stable (first in first out) priority queue in Java. Supposing that the key is a name and the value is an age, I know I can make an unstable priority queue like this: Queue<Map.Entry<String, Integer>> pq = new PriorityQueue<Map.Entry<String, Integer>>(100, ageComparator); This does pretty much everything that I need it to, except that it doesn't maintain order of key-value pairs as I insert them (or remove them). I've found a "work around" by making a LinkedList, which offers essentially all of the same functionality, except that it doesn't include a constructor with a

Is there an alternative to Dictionary/SortedList that allows duplicates? [duplicate]

拥有回忆 提交于 2019-11-28 06:59:19
问题 Possible Duplicate: C# Sortable collection which allows duplicate keys Basically I'd like to make a Dictionary work with duplicate keys without going into custom comparer implementations. There is an idea of: Dictionary<key, List<value>> but it still has some overhead. I wish Dictionary had "AllowDuplicates". 回答1: If you're using .NET 3.5 then Lookup is probably what you're after. 回答2: .NET 2.0: PowerCollections contains the OrderedMultiDictionary . 回答3: You still can use SortedList and try

STL Priority Queue on custom class

与世无争的帅哥 提交于 2019-11-28 05:06:05
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*>,less<vector<Node*>::value_type> > nodesToCheck; What am I missing and/or doing wrong? rlbond less<vector

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

依然范特西╮ 提交于 2019-11-28 03:48: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 it. Moreover, I am not exactly sure about the actual code needed for the operation. I am using the D