6

I have a Boost Graph with VertexList=vecS.

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

Now I want to iterate through my vertices and remove those that have a specific property. How can I do this?

The problem is whenever I call remove_vertex, the iterator to the vertices in the graph along with the vertex descriptors are invalidated.

Kirill V. Lyadvinsky
  • 97,037
  • 24
  • 136
  • 212
Dat Chu
  • 10,822
  • 13
  • 58
  • 82

3 Answers3

3

May be, before iteration you can make special "Trash" vertex, during iteration you connect all nodes-for-deletion to that Trash-vertex and, after iteration, delete all "Trash-connected" vertices?

al.zatv
  • 39
  • 3
3

Your edges are stored in an std::vector. If you have N vertices, then all vertices are numbered from 0 to N. If you delete one, then your vertices will be renumbered from o to N-1. Therefor, your descriptor will be invalidated.

However, there might be a work arround : – iterate from N down to 0 – test your node and delete it if necessary

This supposes (and I'm not sure, but rather confident) that it will only renumber the vertices after the one you've just deleted.

If you do this manipulation a lot, it might be rather slow depending on your graph's size.

If that approach doesn't work, you'll have to build a new graph from the old one (by pre-computing how many vertices and edges you will have, this might actually be reasonnably fast).

So, sorry, no real answer, but I hope you'll manage to get something out of it.

Tristram Gräbener
  • 9,601
  • 3
  • 34
  • 50
  • This would be nice if you just operated on the vector. But `adjacency_list` manages it and needs to reindex the edges anyways. It might choose to reallocate the vector after `n` removals (again invalidating _all_ iterators). Finally, reverse iterating `vertices(graph)` isn't guaranteed to be equivalent to reverse iterating the internal vector of vertices – sehe Feb 07 '15 at 00:58
1

I don't think it is possible (in a reasonable time) with vecS as a template parameter. Look what Boost documentation says:

If the VertexList template parameter of the adjacency_list was vecS, then all vertex descriptors, edge descriptors, and iterators for the graph are invalidated by this operation. <...> If you need to make frequent use of the remove_vertex() function the listS selector is a much better choice for the VertexList template parameter.

In case of listS the iterators are not invalidated by calling remove_vertex unless the iterator is pointing to the actual vertex that was removed.

Kirill V. Lyadvinsky
  • 97,037
  • 24
  • 136
  • 212
  • The problem with listS is that I cannot get connected_components to work with it. http://stackoverflow.com/questions/3183186/perform-connected-components-with-boost-adjacency-list-where-vertexlistlists – Dat Chu Jul 06 '10 at 16:34