简体   繁体   English

graph-tool:如何维护一组有序的顶点?

[英]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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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