Python 从for循环中的图形中删除节点

Python 从for循环中的图形中删除节点,python,igraph,Python,Igraph,我正在使用igraphpython。 我试图解决一个叫做“最长诱导路径”的问题,我首先选择一个随机节点,找到它的邻居,然后根据贴近度中心值从邻居中选择一个,删除其他节点,依此类推 我的代码如下 from random import randint import networkx as nx import numpy as np from igraph import * from numpy import random def maximum(a, n): maxpos = a.index ( m

我正在使用igraphpython。 我试图解决一个叫做“最长诱导路径”的问题,我首先选择一个随机节点,找到它的邻居,然后根据贴近度中心值从邻居中选择一个,删除其他节点,依此类推 我的代码如下

from random import randint
import networkx as nx
import numpy as np
from igraph import *
from numpy import random
def maximum(a, n):
maxpos = a.index ( max ( a ) )
return 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
此外,在寻找j的邻域时存在如下问题:

cannot get neighbors, Invalid vertex id
回溯:

File "E:/inducedpath/indu.py", line 30, in <module> G.delete_vertices(neigh) igraph._igraph.InternalError: Error at c:\projects\python-igraph\vendor\build\igraph\igraph-0.8.0-msvc\src\iterators.c:764: Cannot create iterator, invalid vertex id, Invalid vertex id
文件“E:/inducedpath/indu.py”,第30行,在G.delete_顶点(neigh)igraph.\u igraph.InternalError:c:\projects\python igraph\vendor\build\igraph\igraph-0.8.0-msvc\src\iterators处出错。c:764:无法创建迭代器,顶点id无效,顶点id无效
如中所述,不应使用顶点标签,而应使用id

看看你的答案

对于多个ID:

to_delete_ids = [x.index for x in G.vs]
G.delete_vertices(to_delete_ids)
对于单个顶点:
G.delete\u顶点(x.index)

如中所述,您不应使用顶点的标签,而应使用id

看看你的答案

对于多个ID:

to_delete_ids = [x.index for x in G.vs]
G.delete_vertices(to_delete_ids)
对于单个顶点:
G.delete\u顶点(x.index)

谢谢您的回复,但我仍然需要在任何地方都执行相同的操作,例如,如果我想找到顶点的邻居,我应该以相同的方式执行吗,neigh=G.neights(x.index)??我的意思是如何为我工作中的所有内容、邻居、要删除的顶点保留顶点索引,将顶点指定给变量,我是否也应该这样做???更新您的问题并添加代码,然后我可能会帮助您;-)你现在可以检查一下吗:)谢谢你的回复,但是我仍然需要在任何地方都做同样的事情吗?例如,如果我想找到顶点的邻居,我应该用同样的方法,neigh=G.neights(x.index)??我的意思是如何为我工作中的每件事保留顶点的索引,邻居,要删除的顶点,将顶点指定给变量,我是否也应该这样做???更新您的问题并添加代码,然后我可能会帮助您;-)您现在可以检查一下吗:)您可以共享整个回溯,这样我们就可以看到错误发生在哪一行。@zypro文件“E:/inducedpath/indu.py”,第31行,在neigh=G.neights(j)中igraph.\u igraph.InternalError:c:\projects\python igraph\vendor\build\igraph\igraph-0.8.0-msvc\src\type\u indexedgelist处出错。c:747:无法获取邻居,顶点无效id@zypro文件“E:/inducedpath/indu.py”,第30行,位于G.delete_顶点(neigh)中igraph.\u igraph.InternalError:c:\projects\python igraph\vendor\build\igraph\igraph-0.8.0-msvc\src\iterators处出错。c:764:无法创建迭代器,无效的顶点id,无效的顶点id,并且您在
G后面有上面的空格。删除\u顶点
也像代码中的那样?!这也将是一个问题。(也是在
G.neights
后面)@zypro对不起,我没听清你的问题,你是说G.delete_顶点和(neigh)之间的空格吗???如果你的意思是否定的,我的代码中是这样的:G.delete_顶点(neigh)neigh=G.neights(j)你能分享整个回溯吗?这样我们就可以看到错误发生在哪一行。@zypro文件“E:/inducedpath/indu.py”,第31行,在neigh=G.neights(j)中igraph.\u igraph.InternalError:c:\projects\python igraph\vendor\build\igraph\igraph-0.8.0-msvc\src\type\u indexedgelist处出错。c:747:无法获取邻居,顶点无效id@zypro文件“E:/inducedpath/indu.py”,第30行,位于G.delete_顶点(neigh)中igraph.\u igraph.InternalError:c:\projects\python igraph\vendor\build\igraph\igraph-0.8.0-msvc\src\iterators处出错。c:764:无法创建迭代器,无效的顶点id,无效的顶点id,并且您在
G后面有上面的空格。删除\u顶点
也像代码中的那样?!这也将是一个问题。(也是在
G.neights
后面)@zypro对不起,我没听清你的问题,你是说G.delete_顶点和(neigh)之间的空格吗???如果你的意思是否定的,我的代码中是这样的:G.delete_顶点(neigh)neigh=G.neights(j)