Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 图形工具:如何维护一组有序的顶点?_Python_Graph Theory_Graph Tool - Fatal编程技术网

Python 图形工具:如何维护一组有序的顶点?

Python 图形工具:如何维护一组有序的顶点?,python,graph-theory,graph-tool,Python,Graph Theory,Graph Tool,我正在使用图形工具在网络上工作 在所有顶点的集合中,有一些特定的顶点组,它们具有定义良好的顺序,我希望跟踪它们。到目前为止,我一直在维护一个外部数据结构,以正确的顺序引用顶点。但是,删除顶点时,索引大于已删除顶点的所有顶点都将重新编制索引,这将破坏我一直保留在外部数据结构中的引用 什么是保持顶点有序子集的正确方法,以便在(例如)从图中删除第零个顶点时不会中断 from graph_tool.all import * graph = Graph(directed=False) graph.add

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

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

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

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

适用于这里。在您发布MCVE代码并准确描述问题之前,我们无法有效地帮助您。我们应该能够将您发布的代码粘贴到文本文件中,并重现您描述的问题。给我们一个有代表性的例子,这样我们就可以尝试解决方案。我添加了一个片段,重现了这个问题,但我认为没有必要。这是一个关于维护一组有序顶点的正确方法的一般性问题,而不是一段特定代码的问题。@greedIsGoodAha上面所说的“第五个顶点”实际上是第六个顶点。