Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I change the edge weight in a graph using the Boost Graph Library?

I have defined a Graph using the Boost graph library,

typedef boost::property<boost::edge_weight_t, int> EdgeWeightProperty;
typedef boost::adjacency_list<boost::listS, boost::vecS,boost::undirectedS,boost::no_property,EdgeWeightProperty> Graph;

It is fairly straightforward to add edges using

boost::add_edge(vertice1, vertice2, weight, graph);

I have yet to figure out how to change the edge weight once it has been set. One possible solution would be to delete the edge and re-add it with the updated the weight value, however, that seems a bit excessive.

like image 509
Reed Richards Avatar asked Jun 23 '14 13:06

Reed Richards


1 Answers

One solution is to do the following

typedef boost::adjacency_list<boost::setS, boost::vecS, boost::undirectedS,boost::no_property,EdgeWeightProperty> Graph;
typedef Graph::edge_descriptor Edge;
Graph g;
std::pair<Edge, bool> ed = boost::edge(v1,v2,g);
int weight = get(boost::edge_weight_t(), g, ed.first);
int weightToAdd = 10;
boost::put(boost::edge_weight_t(), g, ed.first, weight+weightToAdd);
like image 71
Reed Richards Avatar answered Oct 26 '22 14:10

Reed Richards