priority_queue cannot update element value

T

thomas

I want to use a priority_queue like STL data structure.
But I found that priority_queue cannot update element value: only pop/
push is supported.

I'm using priority_queue to implement the prim MST algorithm.
So I need the priority_queue to contain the key[] values for each
vertex not included in the final tree. Of course the pop and push
operation is what I need, but I also need to update the weight value.

Can I use priority_queue to make it work?
Or any other way to use existing STL containers to implement the prim
MST algorithm?
 
F

Fei Liu

thomas said:
I want to use a priority_queue like STL data structure.
But I found that priority_queue cannot update element value: only pop/
push is supported.

I'm using priority_queue to implement the prim MST algorithm.
So I need the priority_queue to contain the key[] values for each
vertex not included in the final tree. Of course the pop and push
operation is what I need, but I also need to update the weight value.

Can I use priority_queue to make it work?
Or any other way to use existing STL containers to implement the prim
MST algorithm?

Sounds like you should use a set or map and implement the push/pop
behavior by youself. This will incur some performance penalty because
the set/map push-pop will all perform worst case O(lgN).

Another thing you could do is have 2 separate data structures one is the
priority queue of pointer type and the other can be a simple vector of
the real objects. then you can update weight of the element in the vector.
 
B

Barry

I want to use a priority_queue like STL data structure.
But I found that priority_queue cannot update element value: only pop/
push is supported.

I'm using priority_queue to implement the prim MST algorithm.
So I need the priority_queue to contain the key[] values for each
vertex not included in the final tree. Of course the pop and push
operation is what I need, but I also need to update the weight value.

Can I use priority_queue to make it work?
Or any other way to use existing STL containers to implement the prim
MST algorithm?

I think that's what PQ is designed for.
If you want to use PQ-like container with your requirement,
you can see how PQ is defined in your compiler library implementation,
I think it's easy for you to rewrite a new container with the STL
heap algorithms, while you can supply iterator support for your
container.

Yet another *quick and dirty* way is to inherit PQ as base class, and
add accessor to
the underlying "protected" sequence named 'c'. But keep in mind that
inheriting
from a STL container is not a good design.
 
J

James Kanze

I want to use a priority_queue like STL data structure.
But I found that priority_queue cannot update element value: only pop/
push is supported.

Obviously. Otherwise, it wouldn't be a priority queue (and
arbitrary updates would destroy the necessary ordering).
I'm using priority_queue to implement the prim MST algorithm.
So I need the priority_queue to contain the key[] values for
each vertex not included in the final tree. Of course the pop
and push operation is what I need, but I also need to update
the weight value.

Why? If you update the weight value, you invalidate the
ordering in the priority queue. You invalidate the solution set
you've built so far, and have to start over. So all you have to
do is destruct the existing priority queue, and create a new
one.
Can I use priority_queue to make it work? Or any other way to
use existing STL containers to implement the prim MST
algorithm?

A priority queue seems exactly what is needed to me.
 
J

Jerry Coffin

I want to use a priority_queue like STL data structure.
But I found that priority_queue cannot update element value: only pop/
push is supported.

I'm using priority_queue to implement the prim MST algorithm.
So I need the priority_queue to contain the key[] values for each
vertex not included in the final tree. Of course the pop and push
operation is what I need, but I also need to update the weight value.

Can I use priority_queue to make it work?
Or any other way to use existing STL containers to implement the prim
MST algorithm?

Since you need to do more than just add and remove items, you can't use
a true priority queue. You should be able to use std::make_heap in a
suitable container. IIRC, for Prim's algorithm what you really want is a
Fibonacci heap. Doing a quick glance through the standard, there's
nothing to indicate that it provides a Fibonacci head instead of the
usual binary heap. Prim's algorithm works with binary heaps, but gives
only O(E lg V) instead of the desired O(E + V lg V) -- you normally only
use Prim's algorithm when E is considerably smaller than V, in which
case this is a substantial win.

There's been Fibonacci heap code in the "Pending" area on Boost for
quite a while. Even though that means it's not an official part of the
library, you might find it quite useful. For that matter, a quick check
indicates that the Boost Graph library already includes an
implementation of Prim's algorithm. It looks like the stock
implementation uses a binary heap though...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top