Consider a graph object G in python-igraph 0.7. If I want the adjacency matrix A of G, I have to write A=G.get_adjacency()
, but there are two problems:
Is there in Igraph any way to obtain a scipy.sparse matrix of a sparse graph in a reasonable time?
It does not matter whether the graph is sparse or not because igraph will still create a dense matrix so it's an O(n2) operation. (Technically, the matrix itself is created in the C layer where the initialization of the matrix to all zeroes takes O(n2) and then it is filled with ones in O(m) where n is the number of vertices and m is the number of edges -- but then the matrix is forwarded to the Python layer where it is converted into a Matrix object, and the Python layer has no idea that the matrix is essentially sparse so it takes O(n2) to convert it, On my laptop, creating the adjacency matrix for a graph with 3000 nodes is around 500 msec and I think this is probably normal.
Yes, there is a way to create a sparse matrix out of an igraph graph straight away, although it's a bit verbose:
from scipy.sparse import coo_matrix
from numpy import hstack, ones
def graph_to_sparse_matrix(graph):
xs, ys = map(array, zip(*graph.get_edgelist()))
if not graph.is_directed():
xs, ys = hstack((xs, ys)).T, hstack((ys, xs)).T
else:
xs, ys = xs.T, ys.T
return coo_matrix((ones(xs.shape), (xs, ys)))
This version converts the same graph to a SciPy sparse matrix in ~26 msec on my machine.