kkjoe
kkjoe

Reputation: 795

python igraph cannot add edges

I want to build a graph in Python via package igraph, I can add vertices, but when I add edges, there's a problem I don't know how to fix. I print the vertices and edges above.

g = ig.Graph()
g.add_vertices(dfset)
g.add_edges(edges)

InternalError: Error at type_indexededgelist.c:272: cannot add edges, Invalid vertex id

print(dfset)= [1437218112, 1710990122, 1346433521, 1750346227, 1487721718, 1528123965]

print(edges) = [(1346433521, 1437218112),
 (1528123965, 1710990122),
 (1528123965, 1750346227),
 (1710990122, 1750346227),
 (1750346227, 1346433521),
 (1750346227, 1437218112),
 (1750346227, 1487721718)]

InternalError                             Traceback (most recent call last)
<ipython-input-75-82114955e940> in <module>()
----> 1 g.add_edges(edges)

/software/python/2.7.10/b1/lib/python2.7/site-packages/igraph/__init__.pyc in add_edges(self, es)
    253           endpoints. Vertices are enumerated from zero.
    254         """
--> 255         return GraphBase.add_edges(self, es)
    256 
    257     def add_vertex(self, name=None, **kwds):

InternalError: Error at type_indexededgelist.c:272: cannot add edges, Invalid vertex id

Upvotes: 0

Views: 1518

Answers (2)

Corley Brigman
Corley Brigman

Reputation: 12371

sascha's answer covers most of it. some additional information which might be useful:

from the documentation:

add_vertices(n)
source code 
Adds some vertices to the graph.

Parameters:
n - the number of vertices to be added, or the name of a single vertex to be added, or an iterable of strings, each corresponding to the name of a vertex to be added. Names will be assigned to the name vertex attribute.
Overrides: GraphBase.add_vertices

igraph stores vertices and edges as a straight array. Vertex and edge IDs are their offset into that array; that's how all indexing is done.

Unlike networkx, when you added a list of IDs, they got converted to strings, and put in the name attribute of several new vertices, which should be 0 through 5. Edges must use the actual vertex IDs, not user-assigned attributes.

Upvotes: 1

sascha
sascha

Reputation: 33522

One of the lines in your stack-trace reads:

Vertices are enumerated from zero

So make sure, your approach of adding edges with arbitrary integers is working (like in networkx, which can use anything hashable).

It seems (to me), you need to do that mapping yourself (e.g. simple dict; or by using attribute-support within the library, which is mentioned in the docs), so that you call the add_edges() function with values in range(0,n_vertices) only!

Small excerpt from the docs:

igraph uses vertex and edge IDs in its core. These IDs are integers, starting from zero, and they are always continuous at any given time instance during the lifetime of the graph. This means that whenever vertices and edges are deleted, a large set of edge and possibly vertex IDs will be renumbered to ensure the continuiuty.

Upvotes: 1

Related Questions