Same weights for different boost graphs

52 views Asked by At

I have just realized that I have not yet understood how to use boost graph library. I have this code:

#include <iostream>
#include <boost/graph/adjacency_list.hpp>

using namespace std;
using namespace boost;

typedef unsigned int WeightType;

typedef adjacency_list<listS, vecS, bidirectionalS,
        no_property, property<edge_weight_t, WeightType>> Graph;

typedef graph_traits<Graph>::vertex_descriptor Vertex;
typedef graph_traits<Graph>::edge_descriptor Edge;

typedef property_map<Graph, edge_weight_t>::type WeightMap;
typedef property_map<Graph, edge_weight_t>::const_type ConstWeightMap;

const WeightType infinity = numeric_limits<WeightType>::max();

int main() {
    Graph g(4);
    Graph g2(4);

    for (uint i = 0; i < 3; ++i) {
        add_edge(i, i+1, i, g);
        add_edge(i, i+1, i*10, g2);
    }

    WeightMap m = get(edge_weight, g);
    WeightMap m2 = get(edge_weight, g2);

    for (auto e : make_iterator_range(edges(g))) {
        cout << m[e] << endl;
    }
    cout << endl;
    for (auto e : make_iterator_range(edges(g))) {
        cout << m2[e] << endl;
    }

}

I would expect an output like: "0 1 2 , 0 10 20". But the output is "0 1 2, 0 1 2". Every graph have its weight property map, doesn't it? Where is my mistake?

1

There are 1 answers

2
tux3 On BEST ANSWER

You made a typo in the second for loop:

for (auto e : make_iterator_range(edges(g))) {

Should be:

for (auto e : make_iterator_range(edges(g2))) {

So you were printing the content of the first graph twice, instead of the first then the second.