I'm using the networkx
in python
. I tested the function adjacency_matrix
but I could not understand some of the results. For example:
import networkx as nx
import numpy as np
g = nx.Graph()
g.add_nodes_from([1,2,3])
g.add_edges_from([[1,2],[2,3],[1,3]])
adj = nx.adjacency_matrix(g)
print adj.todense()
# [[0 1 1]
# [1 0 1]
# [1 1 0]]
print adj.__dict__
# {'indices': array([1, 2, 0, 2, 0, 1]), 'indptr': array([0, 2, 4, 6]), 'maxprint'
# : 50, '_shape': (3, 3), 'data': array([1, 1, 1, 1, 1, 1])}
In the result of print adj.__dict__
, what does the indices
and indptr
stand for? I think they are the key information that is used to reconstruct the adjacency matrix, together with the data
attribute. But I cannot figure out how it is achieved.
Thank you all for helping me!
adj
which is given by nx.adjacency_matrix
is a compressed sparse row matrix format of your adjacency matrix.
A compressed format,
represents a matrix M by three (one-dimensional) arrays, that respectively contain [the] nonzero values, the extents of rows, and column indices.
In your case adj.indices
is a one-dimensional (numpy) array with the indices of the nonzero values of the adjacency matrix. Together with adj.indptr
you can know the exact location of nonzero values in your matrix. By definition adj.indptr[0] == 0
, and adj.indptr[i] == adj.indptr[i-1] + number of nonzero values at row i
. The data
attribute refers to the nonzero values, in your case they are all 1's.