Graph.get_adjacency () slow and the yield strange

Consider the graph G object in python-igraph 0.7. If I need the adjacency matrix A of G, H to write A=G.get_adjacency() , but there are two problems:

  • Even if G is sparse with 3000 nodes, A. has been created for a long time on my commercial laptop. Is it possible that creating an adjacency matrix is ​​so expensive?
  • Exit A - is an object of the Matrix, so if I want to work with the numpy module on A, I must first convert it to the list, and then numpy.matrix. Moreover, if A is sparse, I need a third conversion to sparse matrix scipy.

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

+1
source share
1 answer
  • It does not matter whether or not the graph is sparse, because igraph will still create a dense matrix, so that it performs operations O (n 2). (Technically, the matrix is created in the C layer, wherein the initialization of the matrix to all zeros takes O (n 2), and then it is filled units in O (m), where n is the number of vertices and m - number of ribs, but then the matrix is forwarded to the level Python, where it is converted into a matrix object and Python layer has no idea that the matrix significantly sparse, however requires O (n 2) to convert it. on my laptop creating an adjacency matrix for the graph 3000 nodes is about 500 ms and I think it's probably fine.

  • Yes, there is a way to create a sparse matrix of the graph chart, although it's a bit long-winded:

     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))) zip (* graph.get_edgelist ())) 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))) , ys)). T, hstack ((ys, xs)). T 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 in the sparse matrix SciPy in ~ 26 ms on my machine.

+4
source

All Articles