user3658306
user3658306

Reputation: 217

Boost: how to remove all the out-edges for a vertex

In boost graph library, remove_edge will invalidate the edge iterator, so what is the correct way to remove all the out-edge of a vertex, for example, I am trying to remove all the out-edge of vertex 0. The code snippet in below does not work properly.

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

Upvotes: 3

Views: 2878

Answers (1)

sehe
sehe

Reputation: 392911

You would call clear_out_edges on the source vertex for the 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)
    

If you have to support any MutableGraph, there is only clear_vertex.

Upvotes: 4

Related Questions