Kevin
Kevin

Reputation: 6833

Why does the NetworkX generate an adjacency matrix that is not symmetric for an undirected graph

I am trying to learn coding using NetworkX. I am now trying to get the adjacency matrix of an undirected graph using following code I wrote:

mat = nx.adjacency_matrix(graph)
matDense = mat.todense();

print(len(matDense ))
print(len(matDense [0]))

The output is

38
1

My understanding for adjacency matrix is that it should be symmetric, however, the output seems like not, seems like the adjacency matrix generated by NetworkX is an 38*1 matrix instead of 38*38?

If this, how may I access individual cells of an adjacency matrix, currently I could only locate to rows.If matDense[0][2], an array index out of bounds error will be thrown.

Thanks in advance for any help !

Upvotes: 0

Views: 475

Answers (1)

Joel
Joel

Reputation: 23827

It is a square matrix.

The problem is in this command:

print(len(matDense [0]))

matDense[0] is a matrix consisting of a single row. The len command in this case is telling you there is just one row. Try

print(len(matDense[0].transpose()))

Upvotes: 1

Related Questions