I was trying to write the code in python using igraph and when i tried to add edges using a while loop this error came up
while(i<k)
g.add_vertices(theInts[i])
i=i+1
g.add_edges([(theInts[i-1],theInts[i])])
I thought that indexing might be a problem so i also included a if statement but that doesnt seems to be the problem.
Please Help!!!
I think this all depends on what
ghas for vertices. If you start off with an emptyg, you only have the vertex0, so if you’re trying to calladd_edgeswith two different vertices, it’s just not going to work. You have to add some more vertices. Of course this all depends on what your graph looks like before the loop, and whatiis.You can display some brief information about your graph with
print. For example,If
istarts at 0, then you will not add any vertices with your loop the first time around. So when you try to add edges, you’re trying to add to vertices that do not exist.If that is not the issue, try printing the edges and see if they match up with what you want.
Also, it might be a bit more helpful to have the complete TraceBack.
EDIT: Based on your comment
So you’re saying you have a structure like this:
And you want to add just vertex 2? I am not sure that you can do that with igraph. It seems to have to have each vertex in order. You can check to see if you have the vertex and then add them if necessary, remembering that these graphs are 0-based. Something like this.
Or if you don’t like maps, you can loop it up.
There is probably a better way to do this though. If this isn’t what you’re looking for, please edit your original question with more detail, and some actual code.