提升库:如何删除顶点的所有出边

3
在Boost图形库中,使用remove_edge会使边迭代器失效,因此删除一个顶点的所有出边的正确方法是什么?例如,我想要删除顶点0的所有出边。下面的代码片段无法正常工作。
Graph G(N);
graph_traits <Graph>::out_edge_iterator ei, ei_end;
for (boost::tie(ei, ei_end) = out_edges(0, G); ei != ei_end; ++ei) {
   vertex targ = target(*ei, G);
   cout << "target vtx = " << targ << endl;

   if ( edge(0, targ, G).second != 0 )
     remove_edge(0, targ, G);
}
1个回答

4

您需要在源顶点上调用 clear_out_edges 函数以清除出边(参见http://www.boost.org/doc/libs/1_58_0/libs/graph/doc/adjacency_list.html)。

  • void clear_vertex(vertex_descriptor u, adjacency_list& g)
    

    Removes all edges to and from vertex u. The vertex still appears in the vertex set of the graph.

    The affect on descriptor and iterator stability is the same as that of invoking remove_edge() for all of the edges that have u as the source or target.

  • void clear_out_edges(vertex_descriptor u, adjacency_list& g)
    

    Removes all out-edges from vertex u. The vertex still appears in the vertex set of the graph.

    The affect on descriptor and iterator stability is the same as that of invoking remove_edge() for all of the edges that have u as the source.

    This operation is not applicable to undirected graphs (use clear_vertex() instead).

  • void clear_in_edges(vertex_descriptor u, adjacency_list& g)
    

如果你需要支持任何 MutableGraph,唯一的方法是使用clear_vertex


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接