9

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.

2 Answers 2

14

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);
2
  • 1
    Instead of boost::get and boost::put functions, int weight = g[ed.first].weight; and g[ed.first].weight = weight + weightToAdd; should also work respectively, if you define edge properties as a struct with the element weight Commented Sep 13, 2018 at 11:09
  • 1
    @agy is it easily possible to use builtin algorithms with weights in an edge property struct instead of the edge_weight_t()?
    – lucidbrot
    Commented Dec 19, 2020 at 16:23
4

An alternate solution would be to use property maps. Here's an example.

// Edge weight.
typedef boost::property<boost::edge_weight_t, int> EdgeWeightProperty;

// Graph.
typedef boost::adjacency_list< boost::listS,
                               boost::vecS,
                               boost::undirectedS,
                               boost::no_property,
                               EdgeWeightProperty > Graph;

// Vertex descriptor.
typedef typename boost::graph_traits<Graph>::vertex_descriptor Vertex;

// The Graph object
Graph g;

// Populates the graph.
Vertex v1 = boost::add_vertex(g);
Vertex v2 = boost::add_vertex(g);
Vertex v3 = boost::add_vertex(g);
boost::add_edge(v1, v2, EdgeWeightProperty(2), g);
boost::add_edge(v1, v3, EdgeWeightProperty(4), g);
boost::add_edge(v2, v3, EdgeWeightProperty(5), g);

// The property map associated with the weights.
boost::property_map < Graph,
                      boost::edge_weight_t >::type EdgeWeightMap = get(boost::edge_weight, g);

// Loops over all edges and add 10 to their weight.
boost::graph_traits< Graph >::edge_iterator e_it, e_end;
for(std::tie(e_it, e_end) = boost::edges(g); e_it != e_end; ++e_it)
{
  EdgeWeightMap[*e_it] += 10;
}

// Prints the weighted edgelist.
for(std::tie(e_it, e_end) = boost::edges(g); e_it != e_end; ++e_it)
{
  std::cout << boost::source(*e_it, g) << " "
            << boost::target(*e_it, g) << " "
            << EdgeWeightMap[*e_it] << std::endl;
}
2
  • 1
    What if my weight is a function of the source and end vertices of the edge?
    – Jim
    Commented Aug 30, 2017 at 16:43
  • @Jim Do you mean: What if the weights were a function of some variable (ex. state) of the vertices? Because right now, since each edge is attached to two nodes, the weight is technically a function both vertices. And if the order "source/target" were important, you could use bidirectional graphs.
    – JosephD
    Commented Aug 31, 2017 at 20:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.