Return adjacency matrix of G.
Parameters: | G : graph
nodelist : list, optional
weight : string or None, optional (default=’weight’)
|
---|---|
Returns: | A : SciPy sparse matrix
|
See also
to_numpy_matrix, to_scipy_sparse_matrix, to_dict_of_dicts
Notes
If you want a pure Python adjacency matrix representation try networkx.convert.to_dict_of_dicts which will return a dictionary-of-dictionaries format that can be addressed as a sparse matrix.
For MultiGraph/MultiDiGraph with parallel edges the weights are summed. See to_numpy_matrix for other options.
The convention used for self-loop edges in graphs is to assign the diagonal matrix entry value to the edge weight attribute (or the number 1 if the edge has no weight attribute). If the alternate convention of doubling the edge weight is desired the resulting Scipy sparse matrix can be modified as follows:
>>> import scipy as sp
>>> G = nx.Graph([(1,1)])
>>> A = nx.adjacency_matrix(G)
>>> print(A.todense())
[[1]]
>>> A.setdiag(A.diagonal()*2)
>>> print(A.todense())
[[2]]