Reputation: 1180
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!!!
Upvotes: 2
Views: 9473
Reputation: 66
I think this all depends on what g
has for vertices. If you start off with an empty g
, you only have the vertex 0
, so if you're trying to call add_edges
with 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 what i
is.
You can display some brief information about your graph with print
. For example,
>>> import igraph
>>> graph = igraph.Graph()
>>> print graph
Undirected graph (|V| = 1, |E| = 0)
If i
starts 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.
>>> graph.add_vertices(0)
<igraph.Graph object at 0xcea850>
>>> print graph
Undirected graph (|V| = 1, |E| = 0)
>>> graph.add_edges([(0, 1)])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
igraph.core.InternalError: Error at type_indexededgelist.c:245: cannot add edges, Invalid vertex id
If that is not the issue, try printing the edges and see if they match up with what you want.
>>> graph.add_vertices(5)
<igraph.Graph object at 0xcea850>
>>> print graph
Undirected graph (|V| = 6, |E| = 3)
>>> graph.add_edges([(1, 1), (2, 3), (3, 5)])
<igraph.Graph object at 0xcea850>
>>> graph.get_edgelist()
[(1, 1), (2, 3), (3, 5)]
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:
>>> graph = igraph.Graph()
>>> print graph
Undirected graph (|V| = 1, |E| = 0)
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.
>>> vertices = 1, 2, 13, 4, 21, 5
>>> map_graph = igraph.Graph()
>>> print map_graph
Undirected graph (|V| = 1, |E| = 0)
>>> map_graph.add_vertices(max(vertices))
<igraph.Graph object at 0xceaa50>
>>> print map_graph
Undirected graph (|V| = 22, |E| = 0)
>>> map(map_graph.add_edges, zip(vertices, vertices[1:]))
[<igraph.Graph object at 0xceaa50>, <igraph.Graph object at 0xceaa50>, <igraph.Graph object at 0xceaa50>, <igraph.Graph object at 0xceaa50>, <igraph.Graph object at 0xceaa50>]
>>> print map_graph
Undirected graph (|V| = 22, |E| = 5)
>>> map_graph.get_edgelist()
[(1, 2), (2, 13), (4, 13), (4, 21), (5, 21)]
Or if you don't like maps, you can loop it up.
>>> vertices = 1, 2, 13, 4, 21, 5
>>> loop_graph = igraph.Graph()
>>> print loop_graph
Undirected graph (|V| = 1, |E| = 0)
>>> loop_graph.add_vertices(max(vertices))
<igraph.Graph object at 0xcea950>
>>> print loop_graph
Undirected graph (|V| = 22, |E| = 0)
>>> for pair in zip(vertices, vertices[1:]):
... loop_graph.add_edges(pair)
...
<igraph.Graph object at 0xcea950>
<igraph.Graph object at 0xcea950>
<igraph.Graph object at 0xcea950>
<igraph.Graph object at 0xcea950>
<igraph.Graph object at 0xcea950>
>>> print loop_graph
Undirected graph (|V| = 22, |E| = 5)
>>> loop_graph.get_edgelist()
[(1, 2), (2, 13), (4, 13), (4, 21), (5, 21)]
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.
Upvotes: 5