I am working with a graphing using Boost library.
The graph is defined as follows.
typedef boost::adjacency_list<boost::setS,boost::setS,boost::undirectedS, uint32_t, float> AdjacencyList;
After creating the graph using appropriate data, in a separate function I want to print the adjacent_vertices of each vertex with their appropriate edge weight as computed in the beginning.
The creating part works well but when I want to extract adjacent vertices I dont get the values.
typedef boost::graph_traits<AdjacencyList>::adjacency_iterator AdjacencyIterator;
AdjacencyList::vertex_iterator i, end;
for (boost::tie(i, end) = boost::vertices(adjacency_list); i != end; i++) {
AdjacencyIterator ai, a_end;
boost::tie(ai, a_end) = boost::adjacent_vertices( *i, adjacency_list);
for (; ai != a_end; ai++) {
std::cout << *ai << "\t";
}
}
The Output I get are memory address in Hexademial number.
How can I get vertex indices and the edge weight?
You should access the property bundles, either using the graph's operator[]
with the vertex/edge descriptor, or using the property map:
Using the operator[]
Live On Coliru
#include <boost/graph/adjacency_list.hpp>
#include <iostream>
typedef boost::adjacency_list<boost::setS,boost::setS,boost::undirectedS, uint32_t, float> AdjacencyList;
typedef boost::graph_traits<AdjacencyList>::adjacency_iterator AdjacencyIterator;
int main() {
AdjacencyList adjacency_list;;
boost::add_edge(
boost::add_vertex(10, adjacency_list),
boost::add_vertex(20, adjacency_list),
1.5f,
adjacency_list
);
boost::add_edge(
boost::add_vertex(30, adjacency_list),
boost::add_vertex(40, adjacency_list),
2.5f,
adjacency_list
);
AdjacencyList::vertex_iterator i, end;
for (boost::tie(i, end) = boost::vertices(adjacency_list); i != end; i++) {
AdjacencyIterator ai, a_end;
boost::tie(ai, a_end) = boost::adjacent_vertices(*i, adjacency_list);
for (; ai != a_end; ai++) {
std::cout << adjacency_list[*ai] << "\t";
}
}
}
Output:
10 20 30 40
Using the property map:
boost::property_map<AdjacencyList, boost::vertex_bundle_t>::type pmap = boost::get(boost::vertex_bundle, adjacency_list);
Now you can use boost::get(pmap, vertex_descriptor1)
to access the vertex property bundle