graphtool:如何保持顶点的有序集合?

2024-04-19 11:51:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用图形工具在网络上工作。你知道吗

在所有顶点的集合中,有一些特定的顶点组,它们具有良好定义的顺序,我希望跟踪它们。到目前为止,我一直在维护一个外部数据结构,以正确的顺序引用顶点。但是,删除某个顶点时,索引大于已删除索引的所有顶点都将重新建立索引,这会破坏我在外部数据结构中保留的引用。你知道吗

什么是保持顶点有序子集的正确方法,以便在(例如)从图中删除第0个顶点时不会中断?你知道吗

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

Tags: 工具of网络add图形数据结构index顺序
1条回答
网友
1楼 · 发布于 2024-04-19 11:51:51

在图形工具中,顶点索引总是在一个连续的范围内。你知道吗

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

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

相关问题 更多 >