-1

I am working on a network using graph-tool.

Within the set of all vertices, there are specific groups of vertices with a well defined order that I would like to keep track of. Until now, I have been maintaining an external data structure with references to vertices in their correct order. However, when a vertex is deleted, all vertices with an index greater than the one that was deleted are reindexed, which destroys the references that I have been keeping in my external data structure.

What is the correct way to maintain an ordered subset of vertices such that it will not break when (for instance) the zeroth vertex is removed from the graph?

from graph_tool.all import *

graph = Graph(directed=False)
graph.add_vertex(5)

""" fifth_vertex is a reference to the vertex with an index of 5. """
fifth_vertex = graph.add_vertex()
assert graph.vertex_index[fifth_vertex] == 5

""" upon removal of a vertex at index i, all vertices of index > i are reindexed. fifth_vertex no longer references a vertex. """
graph.remove_vertex(graph.vertex(0))


""" assertion error """
assert fifth_vertex in graph.get_vertices()
greedIsGoodAha
  • 103
  • 2
  • 8
  • [Minimal, complete, verifiable example](http://stackoverflow.com/help/mcve) applies here. We cannot effectively help you until you post your MCVE code and accurately describe the problem. We should be able to paste your posted code into a text file and reproduce the problem you described. Give us a representative example, so we can experiment with solutions. – Prune Jul 06 '18 at 00:25
  • I've added a snippet which reproduces the problem, but I don't think that it is necessary. This is a general question about the correct method to maintain an ordered set of vertices, not a problem with a specific piece of code. – greedIsGoodAha Jul 06 '18 at 00:39
  • 1
    @greedIsGoodAha what you call the "fifth_vertex" above is actually the sixth vertex. – Tiago Peixoto Jul 06 '18 at 13:22

1 Answers1

1

Vertex indexes are always in a contiguous range in graph-tool.

To implement what you want, you need to use property maps:

from graph_tool.all import *

g = Graph(directed=False)
g.add_vertex(6)

index = g.vertex_index.copy()  # copies the vertex index as a stand-alone property map

g.remove_vertex(0)

v = find_vertex(g, index, 5)[0]

assert g.vertex_index[v] == 4
assert index[v] == 5
Tiago Peixoto
  • 5,149
  • 2
  • 28
  • 28