繁体   English   中英

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

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

我正在使用图形工具在网络上工作。

在所有顶点的集合中,有一些特定的顶点组,它们具有明确的顺序,我想跟踪这些顶点。 到目前为止,我一直在维护外部数据结构,并以正确的顺序引用顶点。 但是,删除顶点后,所有索引大于被删除索引的顶点都会被重新索引,这将破坏我一直保存在外部数据结构中的引用。

维护顶点的有序子集,以使其在(例如)从图形中删除第零个顶点时不会中断的正确方法是什么?

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

顶点索引在图形工具中始终处于连续范围内。

要实现所需的功能,您需要使用属性映射:

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