BGL:listS和index_map中的顶点

BGL: vertices in listS and index_map

本文关键字:map 顶点 index listS BGL      更新时间:2023-10-16

我是BGL的新手,几天来我一直在努力实现一个带有顶点移除的图,直到我发现了一个特别有用的SO问题(这个问题),这才让我度过了一天。

现在我的代码是这样构造的(下面的简化代码):

struct NodeProperties
{
int32_t data;
};
typedef property<edge_weight_t, uint32_t> EdgeProperties;
typedef adjacency_list<listS, listS, bidirectionalS,
property<vertex_index_t, int, NodeProperties>,
EdgeProperties> Graph;
/* In the graph initialization function */
Graph graph;
/* many add_vertex() and add_edge() here */
/* Assign indices */
int i = 0;
BGL_FORALL_VERTICES(v, graph, Graph) {
get(vertex_index, graph)[v] = i++;
}
/* Make many manipulations, in particular, edge/vertex removal according 
to certain criteria, NO add_vertex (though some edge are created) */
/* In another function */
vector<int32_t> component(num_vertices(graph));
int num = connected_components(graph, make_iterator_property_map(component.begin(), get(vertex_index, graph), component[0]));

就我目前所了解的情况而言,对顶点使用listS可以防止boost将每个节点的位置用作索引,因此我必须使用一种"添加属性"自己提供这种索引。

我对此很满意,但上面的代码并没有起到的作用——在connected_components行出现分段错误——,除非我重新进行索引分配。这样做会让一切都完美,但在我创造的心理画面中毫无意义。

有人会吗

  • 给我一个很好的参考,用外行的术语解释所有这些index_map"诡计"?好吧,有很多官方文档,但它们对我来说太复杂了(我是一名C程序员)。我计划学习高级C++,但我目前必须为我的工作实现这个图算法,而且在开始真正的代码之前,我不能花3个月的时间学习C++。。。(我已经花了大约10个小时完成了上面的代码,我本可以在这么长的时间内轻松地在C中重新实现上面的所有内容…)
  • 解释我为什么要这样做(或者我在这里做错了什么)

提前感谢,祝您今天愉快!

R

connected_contensions函数构造了一个默认的color_map,其大小为num_vertices(g),在图形中小于指定的最大顶点索引。当算法尝试为索引大于num_vertices(g)的顶点之一写入颜色时,将访问无效内存。

重新分配所有索引时,它们都在num_vertices(g)范围内。

要快速引用属性,请阅读http://www.boost.org/doc/libs/1_63_0/libs/graph/doc/quick_tour.html.