Graph.get_adjacency() is slow and the output is strange
Question: 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:

  1. Even if G is sparse with 3000 nodes, A is generated in a long time on my commercial laptop. Is it possible that the creation of the adjacency matrix is so expensive?
  2. The output A is a Matrix object, so if I want to operate with the numpy module on A, I have to convert it first in a list and then in a numpy.matrix. Moreover if A is sparse I need a third conversion in a sparse scipy matrix.

Is there in Igraph any way to obtain a scipy.sparse matrix of a sparse graph
in a reasonable time?


Answer:   1. 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.

  2. 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.

