Return the graph adjacency matrix as a SciPy sparse matrix.
Parameters : | G : graph
nodelist : list, optional
dtype : NumPy data-type, optional
weight : string or None optional (default=’weight’)
format : str in {‘bsr’, ‘csr’, ‘csc’, ‘coo’, ‘lil’, ‘dia’, ‘dok’}
|
---|---|
Returns : | M : SciPy sparse matrix
|
Notes
The matrix entries are populated using the edge attribute held in parameter weight. When an edge does not have that attribute, the value of the entry is 1.
For multiple edges the matrix values are the sums of the edge weights.
When does not contain every node in , the matrix is built from the subgraph of that is induced by the nodes in .
Uses coo_matrix format. To convert to other formats specify the format= keyword.
References
[R204] | (1, 2) Scipy Dev. References, “Sparse Matrices”, http://docs.scipy.org/doc/scipy/reference/sparse.html |
Examples
>>> G = nx.MultiDiGraph()
>>> G.add_edge(0,1,weight=2)
>>> G.add_edge(1,0)
>>> G.add_edge(2,2,weight=3)
>>> G.add_edge(2,2)
>>> S = nx.to_scipy_sparse_matrix(G, nodelist=[0,1,2])
>>> print(S.todense())
[[0 2 0]
[1 0 0]
[0 0 4]]