gpt4 book ai didi

c++ - 使用 Boost adjacency_list 执行 connected_components where VertexList=listS

转载 作者:太空狗 更新时间:2023-10-29 22:53:04 26 4
gpt4 key购买 nike

我在一个项目中使用了 Boost Graph Library,它被声明为:

typedef adjacency_list <listS, listS, undirectedS, TrackInformation, LinkInformation> TracksConnectionGraph;

在我必须在我的图表上调用 connected_components 之前,一切都很顺利。

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);

问题似乎是,如果 VertexList=listS,我没有将 vertex_index 作为顶点的属性。这使得 connected_components 给我这样的错误:

/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&]':

所以问题是:如何将 vertex_index 添加为顶点的属性?

如果我添加它,是否意味着每当我调用 add_vertex、remove_vertex 等时,我必须为每个顶点更新此信息?

最佳答案

您可以添加 vertex_index属性到图形类型的定义(在 adjacency_list 的顶点属性模板参数中,将 TrackInformation 更改为 property<vertex_index_t, size_t, TrackInformation> )。在调用算法之前,您需要使用循环填充属性映射,例如:

size_t index = 0;
BGL_FORALL_VERTICES(v, tracks_connection_graph_, TracksConnectionGraph) {
put(vertex_index, g, v, index++);
}

关于c++ - 使用 Boost adjacency_list 执行 connected_components where VertexList=listS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3183186/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com