igraph-help
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [igraph] How to read in a large graph (and output a sparse matrix)


From: Raphael C
Subject: Re: [igraph] How to read in a large graph (and output a sparse matrix)
Date: Tue, 2 Aug 2016 15:51:06 +0100

On 2 August 2016 at 10:16, Tamas Nepusz <address@hidden> 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



reply via email to

[Prev in Thread] Current Thread [Next in Thread]