简体   繁体   English

为什么NetworkX生成对无向图不对称的邻接矩阵

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

I am trying to learn coding using NetworkX. 我正在尝试使用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? 我对邻接矩阵的理解是,它应该是对称的,但是输出似乎不是,好像NetworkX生成的邻接矩阵是38 * 1矩阵而不是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. 如果是这样,我将如何访问邻接矩阵的单个单元格,当前我只能定位到行。如果matDense [0] [2],将引发数组索引超出范围错误。

Thanks in advance for any help ! 在此先感谢您的帮助!

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. matDense[0]是由单行组成的矩阵。 The len command in this case is telling you there is just one row. 在这种情况下, len命令告诉您只有一行。 Try 尝试

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM