graph_tool:将顶点id重新索引为连续整数

时间:2017-09-17 12:53:15

标签: graph-tool

在给定图形上应用顶点滤镜后,我想重新索引新的滤波图形,以便顶点ID是连续的整数。

例如:

from graph_tool import load_graph
g = load_graph("path-to-graph")   # assuming there are `n` nodes
vfilt = g.new_vertex_filter('bool')
# ...modify vfilt so that `k` nodes are filtered out 
g.set_vertex(filter)
new_g = reindex_vertices(g)  # is there such a function?
assert list(map(int, new_g.vertices())) == list(range(n-k))  # 1...n-k

是否有与reindex_vertices中的graph_tool类似的功能?

更新

一个解决方案是:

n2i = {n: i for i, n in enumerate(g.vertices())}  # mapping from old node id to new node id

# then create a new graph and also reindex the edges
new_g = Graph()
new_g.add_edge_list([(n2i[e.source()], n2i[e.target()]) for e in g.edges()])

2 个答案:

答案 0 :(得分:0)

怎么样?
new_g = gt.Graph(new_g, prune = True)

现在new_g会有连续的顶点索引。

答案 1 :(得分:0)

您需要设置vorder函数调用的Graph()参数。 类型为PropertyMap

N = int(g.num_vertices())
newindexes = graph.new_vertex_property("int")
for v in graph.vertices():
    newnames[v] = (int(v)+3)%N    #Hash function to jumble the values
new_g = Graph(g,vorder=newindexes)

您可以将newindexes更改为these datatypes

这将“重新索引”您的新图形。 :)

相关问题