Search code examples
c++boostboost-graphboost-property-map

Iterating through edges with custom defined edge properties of boost::adjacency_list


I'm working on a simulation program which uses boost::adjacency_list to represent a graph. The edges have costume designed properties

struct edge_data{
    const float linear_cost, const_cost;
    std::queue<*Agent> agents;

    edge_data(float lin, float con) : linear_cost(lin),const_cost(con) {}
};

After trying several ways, I haven't found a way to create a property_map which returns the full EdgeData for each edge, or any other way to iterate through and modify edges. Is it possible, and if so, how?


Solution

  • You ask the edge bundle from the graph, either

    • using the boost::edge_bundle_t property map:

      auto pmap = boost::get(boost::edge_bundle, my_graph);
      edge_data& data = pmap[my_edge_descriptor];
      
    • using the convenience accessors:

      edge_data& data = mygraph[my_edge_descriptor];
      
    • or using specific property maps for a sub-property:

      auto costmap = boost::get(&edge_data::linear_cost, my_graph);
      float cost = costmap[my_edge_descriptor];