On 2 August 2016 at 10:16, Tamas Nepusz <[email protected]> wrote:
>> Is there a function to write a sparse adjacency matrix of a graph to a
>> file? I see  "write_adjacency" but the docs don't indicate it gives a
>> sparse matrix.
> It won't - it would give you a dense matrix. Producing a SciPy sparse
> matrix with a single command is not possible (because we would have to
> link igraph to SciPy at compile time if we wanted to do that), but
> it's only a few lines of code:
>
> import numpy as np
> from scipy.sparse import coo_matrix
>
> sources, targets = zip(*graph.get_edgelist())
> vcount = graph.vcount()
> data = np.array([1] * graph.ecount())
> rows = np.array(sources, dtype=np.int32, copy=False)
> cols = np.array(targets, dtype=np.int32, copy=False)
> matrix = coo_matrix((data, (rows, cols)), shape=(vcount, vcount))
>
> If your graph is undirected, then this will store only the upper half
> of it, so you also need to do this:
>
> matrix += matrix.T
>
> (This will probably convert the matrix into compressed sparse row
> format as a side-effect).
>

Unfortunately coo_matrix appears to be a very space inefficient
format.  It seems to take about three times the RAM of the original
graph.

Unless there is some other way to output the adjacency matrix of an
igraph graph this looks like a dead end sadly.

Raphael

_______________________________________________
igraph-help mailing list
[email protected]
https://lists.nongnu.org/mailman/listinfo/igraph-help

Reply via email to