2

I'm using igraph-python. I'm trying to solve a problem called the longest induced path, I'm choosing, in the beginning, a random node , find it's neighbors then choose one from the neighbors based on the value of closeness centrality, delete the others and so on My code is the following

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

when I'm running this code python giving me this error:

Cannot create iterator, invalid vertex id, Invalid vertex id

also there is a problem in the line of finding the neighbours of j as follows:

cannot get neighbors, Invalid vertex id

Traceback:

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
zypro
  • 1,158
  • 3
  • 12
  • 33
  • can you please share the whole traceback so we can see which line the error occurs. – zypro Mar 30 '20 at 20:27
  • @zypro File "E:/inducedpath/indu.py", line 31, in neigh = G.neighbors (j) igraph._igraph.InternalError: Error at c:\projects\python-igraph\vendor\build\igraph\igraph-0.8.0-msvc\src\type_indexededgelist.c:747: cannot get neighbors, Invalid vertex id – ahmad alanaqreh Mar 31 '20 at 08:33
  • @zypro File "E:/inducedpath/indu.py", line 30, in 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 – ahmad alanaqreh Mar 31 '20 at 08:36
  • and you have the whitespace above behind `G.delete_vertices` also like that in your code ?! That will be an issue too. (also behind `G.neighbors`) – zypro Mar 31 '20 at 10:30
  • @zypro sorry I didn't get your question, do you mean the spaces between G.delete_vertices and (neigh)??? if you mean this no it's in my code like this G.delete_vertices(neigh) neigh=G.neighbors(j) – ahmad alanaqreh Mar 31 '20 at 10:40
  • Have a look at this mailinglist: https://lists.nongnu.org/archive/html/igraph-help/2010-06/msg00050.html . Maybe you need to use a `-1` when accessing an index. – zypro Mar 31 '20 at 14:34

1 Answers1

0

As mentioned in the docs you should't use the label of the vertex but the id(s).

Have a look at the answer of this question.

For multiple ids:

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

For a single vertex: G.delete_vertices(x.index)

zypro
  • 1,158
  • 3
  • 12
  • 33