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

Weights in a labeled_graph


How do I add weights to this labeled graph that uses add_edge_by_label for use in dijkstra_shortest_paths? Am trying to use an example Thanks

#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/copy.hpp>
#include <boost/graph/labeled_graph.hpp>
#include <boost/graph/graph_utility.hpp>

struct NodeInfo1 { int i; };
struct EdgeInfo1 { int j; };

typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS, NodeInfo1, EdgeInfo1> AList;
typedef boost::labeled_graph<AList, std::string> Graph;

auto TestCopyGraph()
{
    std::string names[3] = { "A", "B", "C" };
      NodeInfo1 props[3] = { {11}, {22}, {33} };
    Graph grid(3, names, props);
    /*auto e =*/ add_edge_by_label("C", "B", EdgeInfo1{17}, grid);

    Graph g1 = grid; // just copy-construct
    return g1;
}

Solution

  • Just anywhere. In the bundled j field?

    auto weight_map = boost::get(&EdgeInfo1::j, g1);
    

    In an external map?

    std::map<Graph::edge_descriptor, double> weights;
    auto weight_map = boost::make_assoc_property_map(weights);
    

    Pass it to dijkstra either directly or as a named parameter as per the documentation (or search my answers for examples).