Reputation: 11
I'm looking for a concise (and fast!) of retrieving vertex attributes from an igraph.Graph
without iterating through each edge. For the most basic example, I want a list of all source nodes in the edge list. The best I can find is:
g = ig.Graph.full(100)
sourcelist = [e.source for e in g.es]
Is there something faster? Speed really matters in this application. Bonus points if it can come as a np array. Thanks.
Upvotes: 0
Views: 307
Reputation: 48061
Getting the edgelist and extracting the first item of each tuple is going to be faster:
>>> timeit('[e.source for e in g.es]', 'from igraph import Graph; g = Graph.Full(100)', number=1000)
1.4157595419999893
>>> timeit('[e[0] for e in g.get_edgelist()]', 'from igraph import Graph; g = Graph.Full(100)', number=1000)
0.2647179579999914
Unfortunately there's still a bit of overhead due to the construction of a tuple for each edge in g.get_edgelist()
, and you won't end up with a NumPy array unless you wrap the expression in array()
. In theory it is possible to use ctypes
to call into igraph's C core directly and get a raw igraph_vector_int_t
, which you can then wrap into a NumPy array with zero copies and extra tuples being made, but it is going to be fragile because you need to know how the internal fields of an igraph_vector_int_t
are laid out.
Upvotes: 2