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: Tamas Nepusz
Subject: Re: [igraph] How to read in a large graph (and output a sparse matrix)
Date: Tue, 2 Aug 2016 11:16:57 +0200

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

T.



reply via email to

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