从for循环图中的节点删除

问题描述 投票:1回答:1

我正在使用igraph-python。我正在尝试解决一个称为最长诱导路径的问题,我首先选择一个随机节点,找到它的邻居,然后根据接近中心性的值从邻居中选择一个,删除其他等等。我的代码如下`来自随机进口randint将networkx导入为nx将numpy导入为np从igraph导入*从numpy导入随机def最大值(a,n):maxpos = a.index(max(a))返回maxpos

G = Graph ()
G.add_vertices(5)
G.add_edges([(0, 1), (0, 2),(1,3),(2,3),(3,4)])

n = G.vcount()
first = random.randint(n)
neigh = G.neighbors(first)
clos = G.closeness(vertices=neigh)
induced_path = first
G.delete_vertices(first)
while len(neigh) > 0:
     pos = maximum(clos, len(clos))
     j= neigh[pos]
     np.append(induced_path,j)
     print(induced_path)
     neigh = np.setdiff1d(neigh,j)
     G.delete_vertices (neigh)
     neigh = G.neighbors (j)
     clos = G.closeness(vertices=neigh)
     G.delete_vertices(j)
     print(len(induced_path))

`

[当我运行此代码python时出现此错误:

Cannot create iterator, invalid vertex id, Invalid vertex id
python igraph
1个回答
0
投票
docs中所述,您不应使用顶点的标签,而应使用id。

看看this question的答案。

对于多个ID:

to_delete_ids = [x.index for x in G.vs] G.delete_vertices(to_delete_ids)

对于单个顶点:G.delete_vertices(x.index)
© www.soinside.com 2019 - 2024. All rights reserved.