priority-queue

Implementing a priority queue that can be iterated over in C++

拈花ヽ惹草 提交于 2019-12-04 13:07:57
问题 I need to implement a priority queue for a project, but the STL's priority_queue is not indicated since we need to iterate over all elements and remove them randomly. We are thinking about using the STL's set for this, wrapping it in a class to make it an ADT. Is there a smarter solution for this? How can we make it so some of set 's public member functions can be used publicly? We're interested in iterators, etc. Apparently deriving the STL is unwise because of the lack of virtual

Priority queue with Pointers and Comparator C++

跟風遠走 提交于 2019-12-04 12:24:53
问题 I just started to learn C++, Half of the time I don't know what am I doing, spend hours and hours searching on Google and blindly put codes inside my project, this might be a basic question, but I just can't seems to get it right. This is the requirement of my assignment, I need to have these: in the Edge class: public: bool operator()(Edge*, Edge*) in the Graph class: private: priority_queue<Edge*, vector<Edge*>, Edge> edges I have problem declaring the priority_queue. Details: If I directly

Creating Min Heap from STL Priority Queue

丶灬走出姿态 提交于 2019-12-04 09:46:39
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 understand. d:\microsoft visual studio 10.0\vc\include\xutility(674): error C2064: term does not evaluate to

Using a container/heap to implement a priority queue

只谈情不闲聊 提交于 2019-12-04 09:34:33
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 n.row == o.row && n.col == o.col } And PQueue.go: // PQueue.go package pqueue import "container/vector"

built-in max heap API in Python

你说的曾经没有我的故事 提交于 2019-12-04 08:55:17
问题 Default heapq is min queue implementation and wondering if there is an option for max queue? Thanks. I tried the solution using _heapify_max for max heap, but how to handle dynamically push/pop element? It seems _heapify_max could only be used during initialization time. import heapq def heapsort(iterable): h = [] for value in iterable: heapq.heappush(h, value) return [heapq.heappop(h) for i in range(len(h))] if __name__ == "__main__": print heapsort([1, 3, 5, 7, 9, 2, 4, 6, 8, 0]) Edit,

Removing top of PriorityQueue?

南楼画角 提交于 2019-12-04 07:52:29
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? 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 you want. And: it will only work for non-primitive values because a queue will store objects only. The trick

Efficient implementation of binary heaps

删除回忆录丶 提交于 2019-12-04 07:25:44
问题 I'm looking for information on how to implement binary heaps efficiently. I feel like there should be a nice article somewhere about implementing heaps efficiently, but I haven't found one. In fact I've been unable to find any resources on the matter of efficient implementation beyond the basics like storing the heap in an array. I'm looking for techniques for making a fast binary heap beyond the ones I describe below. I've already written a C++ implementation that is faster than Microsoft

How can I create Min stl priority_queue?

强颜欢笑 提交于 2019-12-04 07:20:03
问题 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. 回答1: Use std::greater as the comparison function: std::priority_queue<int, std::vector<int>, std::greater<int> > my_min_heap; 回答2: 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

Why do I get a ConcurrentModificationException?

喜夏-厌秋 提交于 2019-12-04 07:11:06
问题 Why do I get a ConcurrentModificationException at the specified location in my code? I cannot figure out what I am doing wrong... The removeMin() method is being used to locate the min in the list pq , remove it, and return its value import java.util.Iterator; import java.util.LinkedList; public class test1 { static LinkedList<Integer> list = new LinkedList<Integer>(); public static void main(String[] args) { list.add(10); list.add(4); list.add(12); list.add(3); list.add(7); System.out

Deletion in binary heap

元气小坏坏 提交于 2019-12-04 05:20:32
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 in advance for all of the clarifications. Fred Foo Binary heaps and other priority queue structures