Thank you very much for the immediate reply ! I had read that warning, but forgot it at all when coding. I think I will never forget it from now on.
2015-04-20 23:36 GMT+08:00 Tiago de Paula Peixoto <[email protected]>: > On 20.04.2015 18:19, Bo Wu wrote: > > Hi all, > > > > I found something weird in deleting vertices and edges of a graph. Say I > simply create a graph like: > > > > g = Graph(directed=False) > > g.add_vertex(7) > > g.add_edge(g.vertex(0), g.vertex(1)) > > g.add_edge(g.vertex(1), g.vertex(2)) > > g.add_edge(g.vertex(2), g.vertex(3)) > > g.add_edge(g.vertex(3), g.vertex(4)) > > g.add_edge(g.vertex(2), g.vertex(5)) > > g.add_edge(g.vertex(5), g.vertex(6)) > > > > del_list = [g.vertex(1), g.vertex(3)] > > > > for v in reversed(sorted(del_list)): > > print 'deleting vertex', int(v) > > ## for ve in v.out_edges(): > > ## print 'deleting edges', ve.source(), ve.target() > > ## g.remove_edge(ve) > > g.remove_vertex(v, fast=True) > > > > The above code will delete vertex 3 first, then vertex 1 as expected. > However, in order to keep correct topology, we should delete some related > edges first, like the three commented lines (start with "##"). > > > > But if you uncomment the three lines, weird thing happens: the program > will delete vertex 1 first, then vertex 3. > > > > Of course I can sort the vertex index instead of vertex descriptor to > avoid the problem. But I am wondering where is wrong. Could anyone explain > this? > > I don't observe anything weird. Here I see the following with the lines > uncommented: > > deleting vertex 3 > deleting edges 3 2 > deleting edges 3 4 > deleting vertex 1 > deleting edges 1 0 > deleting edges 1 2 > > First vertex 3, then 1. > > However there is indeed a problem with your code. In the documentation: > > > https://graph-tool.skewed.de/static/doc/quickstart.html#iterating-over-the-neighbourhood-of-a-vertex > > there is this following warning: > > You should never remove vertex or edge descriptors when iterating > over them, since this invalidates the iterators. If you plan to > remove vertices or edges during iteration, you must first store them > somewhere (such as in a list) and remove them only after no iterator > is being used. Removal during iteration will cause bad things to > happen. > > This is what you are trying to do if you uncomment the lines, and it > might cause problems. Don't do that; store the edges in a list first. > > Best, > Tiago > > -- > Tiago de Paula Peixoto <[email protected]> > > > _______________________________________________ > graph-tool mailing list > [email protected] > http://lists.skewed.de/mailman/listinfo/graph-tool > > -- Cheers, Bo Wu
_______________________________________________ graph-tool mailing list [email protected] http://lists.skewed.de/mailman/listinfo/graph-tool
