priority-queue

How to add a single map entry to priority queue

点点圈 提交于 2019-12-06 10:56:50
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? You should add a Map.Entry object and not just ("word", 5) because the generic type of your priority queue is Map.Entry<String,

What is the fastest way to get k smallest (or largest) elements of array in Java?

泪湿孤枕 提交于 2019-12-06 07:52:37
问题 I have an array of elements (in the example, these are simply integers), which are compared using some custom comparator. In this example, I simulate this comparator by defining i SMALLER j if and only if scores[i] <= scores[j] . I have two approaches: using heap of the current k candidates using array of the current k candidates I update the upper two structures in the following way: heap: methods PriorityQueue.poll and PriorityQueue.offer , array: index top of the worst among top k

Simple priority queue in C# - what will be better than List with custom Sorter : IComparer?

丶灬走出姿态 提交于 2019-12-06 07:18:27
问题 I would like to implement a priority queue which would inject my objects - Nodes to the queue with respect to one field - f . I have already written List with custom comparer but this would require from me to: enqueue - sort the List after each insertion dequeue - remove the last(instead of first for performance) like so myList.RemoveAt(myList.Count - 1); My List should be always sorted according to some field (here I need it to be sorted by f ). I also need the ability to add and dequeue the

How to use priority in celery task.apply_async

。_饼干妹妹 提交于 2019-12-06 05:33:13
问题 I have a test queue in celery and I have defined a task for it: @celery_app.task(queue='test', ignore_result=True) def priority_test(priority): print(priority) which just print the argument.I want to set the priority attribute which is defined here for appy_async . So, I wrote a for loop like this: for i in range(100): priority_test.apply_async((i%10,), queue="test", priority=i%10) I excpected to see some result like this: [2017-12-26 17:21:37,309: WARNING/ForkPoolWorker-1] 10 [2017-12-26 17

Creating Min Heap from STL Priority Queue

不问归期 提交于 2019-12-06 04:32:46
问题 I am creating a min heap from stl priority queue. Here is my class which I am using. class Plane { private : int id ; int fuel ; public: Plane():id(0), fuel(0){} Plane(const int _id, const int _fuel):id(_id), fuel(_fuel) {} bool operator > (const Plane &obj) { return ( this->fuel > obj.fuel ? true : false ) ; } } ; In main I instantiate an object thus. priority_queue<Plane*, vector<Plane*>, Plane> pq1 ; pq1.push(new Plane(0, 0)) ; I am getting an error from xutility which I am unable to

Removing top of PriorityQueue?

走远了吗. 提交于 2019-12-06 03:37:18
问题 Assume that I am using the PriorityQueue class from Java.util. I want to remove the largest number from the PriorityQueue pq, which we assume is at the head of the queue. Will the following work? // 1 int head = pq.peek(); pq.dequeue(head); // 2 int head = pq.dequeue(pq.peek()); Would this work the same for non-primitives as well? 回答1: Queue#peek and Queue#element return the head value of the queue, Queue#poll and Queue#remove return and remove it. It looks like int head = pq.poll(); is what

Using a container/heap to implement a priority queue

好久不见. 提交于 2019-12-06 02:47:14
问题 In the big picture, I'm trying to implement Dijkstra's algorithm using a priority queue. According to members of golang-nuts, the idiomatic way to do this in Go is to use the heap interface with a custom underlying data structure. So I have created Node.go and PQueue.go like so: //Node.go package pqueue type Node struct { row int col int myVal int sumVal int } func (n *Node) Init(r, c, mv, sv int) { n.row = r n.col = c n.myVal = mv n.sumVal = sv } func (n *Node) Equals(o *Node) bool { return

Deletion in binary heap

久未见 提交于 2019-12-06 02:30:04
问题 I am just trying to learn binary heap and have a doubt regarding doing delete operation in binary heap. I have read that we can delete an element from binary heap and we need to reheapify it. But at the following link, it says unavailable: http://en.wikibooks.org/wiki/Data_Structures/Tradeoffs Binary Search AVL Tree Binary Heap (min) Binomial Queue (min) Find O(log n) O(log n) unavailable unavailable Delete element O(log n O(log n) unavailable unavailable I am little confused about it. Thanks

Data structure that always keeps n-best elements

六月ゝ 毕业季﹏ 提交于 2019-12-05 21:05:37
问题 I need a data structure that always holds the n largest items inserted so far (in no particular order). So, if n is 3, we could have the following session where I insert a few numbers and the content of the container changes: [] // now insert 1 [1] // now insert 0 [1,0] // now insert 4 [1,0,4] // now insert 3 [1,4,3] // now insert 0 [1,4,3] // now insert 3 [4,3,3] You get the idea. What's the name of the data structure? What's the best way to implement this? Or is this in some library? I am

Perfect powers of numbers which can fit in 64 bit size integer (using priority queues)

旧街凉风 提交于 2019-12-05 16:17:12
How can we print out all perfect powers that can be represented as 64-bit long integers: 4, 8, 9, 16, 25, 27, .... A perfect power is a number that can be written as ab for integers a and b ≥ 2. It's not a homework problem, I found it in job interview questions section of an algorithm design book. Hint, the chapter was based on priority queues. Most of the ideas I have are quadratic in nature, that keep finding powers until they stop fitting 64 bit but that's not what an interviewer will look for. Also, I'm not able to understand how would PQ's help here. Using a small priority queue, with one