I have a priority_queue, and I want to modify some of it\'s contents (the priority value), will the queue be resorted then?
It depends if it resorts on push/pop (mor
Unfortunately, the std::priority_queue
class doesn't support the increase/decrease_key
operations that you're looking for. Of course it's possible to find the element within the heap you want to update, and then call make_heap
to restore the binary heap invariants, but this can't be done as efficiently as it should be with the std::
container/algorithms. Scanning the heap to find the item is O(N)
and then make_heap
is O(N)
on top of that - it should be possible to do increase/decrease_key
in O(log(N))
for binary heaps that properly support updates.
Boost provides a set of priority queue implementations, which are potentially more efficient than the std::priority_queue
(pairing heaps, Fibonacci heaps, etc) and also offer mutability, so you can efficiently perform dynamic updates. So all round, using the boost containers is potentially a much better option.
priority_queue
copies the values you push into it. Your assignment at the end there will have zero effect on the order of the priority queue, nor the values stored inside of it.
I stumbled on this issue while considering the use of priority queues for an A* algorithm.
Basically, C++ priority queues are a very limited toy.
Dynamically changing the priority of a queued element requires to perform a complete reconstruction of the underlying heap manually, which is not even guaranteed to work on a given STL implementation and is grossly inefficient.
Besides, reconstructing the heap requires butt-ugly code, which would have to be hidden in yet another obfuscated class/template.
As for so many other things in C++, you'll have to reinvent the wheel, or find whatever fashionable library that reinvented it for you.
Okay, after searching a bit I found out how to "resort" queue, so after each priority value change you need to call:
std::make_heap(const_cast<Type**>(&queue.top()),
const_cast<Type**>(&queue.top()) + queue.size(),
ComparerClass());
And queue must be then
std::priority_queue<Type*,vector<Type*>,ComparerClass> queue;
Hope this helps.