How to remove element not at top from priority_queue?

The standard priority_queue<T> can be customized through inheritance. It has protected members c and comp that can be referenced in a descendant class. template<typename T> class custom_priority_queue : public std::priority_queue<T, std::vector<T>> { public: bool remove(const T& value) { auto it = std::find(this->c.begin(), this->c.end(), value); if (it == this->c.end()) { return false; } if (it == … Read more

What’s the difference between heapq and PriorityQueue in python?

Queue.PriorityQueue is a thread-safe class, while the heapq module makes no thread-safety guarantees. From the Queue module documentation: The Queue module implements multi-producer, multi-consumer queues. It is especially useful in threaded programming when information must be exchanged safely between multiple threads. The Queue class in this module implements all the required locking semantics. It depends … Read more

Why does Dijkstra’s algorithm use decrease-key?

The reason for using decrease-key rather than reinserting nodes is to keep the number of nodes in the priority queue small, thus keeping the total number of priority queue dequeues small and the cost of each priority queue balance low. In an implementation of Dijkstra’s algorithm that reinserts nodes into the priority queue with their … Read more