简体   繁体   中英

graph-tool: How do I maintain an ordered set of vertices?

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()

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM