问题
I use Boost Graph Library in a project and it is declared as:
typedef adjacency_list <listS, listS, undirectedS, TrackInformation, LinkInformation> TracksConnectionGraph;
Things are going fine until I have to call connected_components on my graph.
typedef std::map<TracksConnectionGraph::vertex_descriptor, TracksConnectionGraph::vertices_size_type> component_type;
component_type component;
boost::associative_property_map< component_type > component_map(component);
int num_components = connected_components(tracks_connection_graph_, component_map);
The problem seems to be that if the VertexList=listS, I do not have vertex_index as a property of my vertex. This makes connected_components give me errors like theses:
/usr/local/include/boost-1_39/boost/property_map.hpp: In member function 'R boost::iterator_property_map::operator[](typename boost::property_traits::key_type) const [with RandomAccessIterator = __gnu_cxx::__normal_iterator , IndexMap = boost::adj_list_vertex_property_map, boost::detail::error_property_not_found, const boost::detail::error_property_not_found&, boost::vertex_index_t>, T = boost::default_color_type, R = boost::default_color_type&]':
So the question is: how do I add vertex_index as a property of my vertices?
If I add it, does it mean that whenever I call add_vertex, remove_vertex and such, I have to update this information for each vertex?
回答1:
You can add a vertex_index
property to the definition of your graph type (in the vertex property template argument to adjacency_list
, change TrackInformation
to property<vertex_index_t, size_t, TrackInformation>
). Before calling the algorithm, you will need to fill in the property map using a loop such as:
size_t index = 0;
BGL_FORALL_VERTICES(v, tracks_connection_graph_, TracksConnectionGraph) {
put(vertex_index, g, v, index++);
}
来源:https://stackoverflow.com/questions/3183186/perform-connected-components-with-boost-adjacency-list-where-vertexlist-lists