NetworkX系列教程(9)-线性代数相关
学过线性代数的都了解矩阵,在矩阵上的文章可做的很多,什么特征矩阵,单位矩阵等.grpah存储可以使用矩阵,比如graph的邻接矩阵
,权重矩阵
等,这节主要是在等到graph后,如何快速得到这些信息.详细官方文档在这里
注意: 如果代码出现找不库,请返回第一个教程,把库文件导入.
线性代数相关
图矩阵
1 | #定义图的节点和边 |
输出:
邻接矩阵:
[[0. 0. 0. 0. 5. 0. 0. 0. 6. ]
[0. 0. 0. 2. 0. 0. 0. 0. 0. ]
[0. 0. 0. 0. 0. 0.5 0.5 0. 0. ]
[0. 2. 0. 1. 1. 0. 0. 0. 0. ]
[5. 0. 0. 1. 0. 0. 0. 0. 7. ]
[0. 0. 0.5 0. 0. 0. 0.5 0. 0. ]
[0. 0. 0.5 0. 0. 0.5 0. 0. 0. ]
[0. 0. 0. 0. 0. 0. 0. 0. 0. ]
[6. 0. 0. 0. 7. 0. 0. 0. 0. ]]
关联矩阵:
[[1. 1. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 1. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 1. 1. 0. 0. 0. 0.]
[0. 0. 1. 0. 0. 1. 0. 0. 0.]
[0. 1. 0. 0. 0. 1. 0. 1. 0.]
[0. 0. 0. 1. 0. 0. 0. 0. 1.]
[0. 0. 0. 0. 1. 0. 0. 0. 1.]
[0. 0. 0. 0. 0. 0. 0. 0. 0.]
[1. 0. 0. 0. 0. 0. 0. 1. 0.]]
拉普拉斯矩阵:
[[11. 0. 0. 0. -5. 0. 0. 0. -6. ]
[ 0. 2. 0. -2. 0. 0. 0. 0. 0. ]
[ 0. 0. 1. 0. 0. -0.5 -0.5 0. 0. ]
[ 0. -2. 0. 3. -1. 0. 0. 0. 0. ]
[-5. 0. 0. -1. 13. 0. 0. 0. -7. ]
[ 0. 0. -0.5 0. 0. 1. -0.5 0. 0. ]
[ 0. 0. -0.5 0. 0. -0.5 1. 0. 0. ]
[ 0. 0. 0. 0. 0. 0. 0. 0. 0. ]
[-6. 0. 0. 0. -7. 0. 0. 0. 13. ]]
标准化的拉普拉斯矩阵:
[[ 1. 0. 0. 0. -0.418 0. 0. 0. -0.502]
[ 0. 1. 0. -0.707 0. 0. 0. 0. 0. ]
[ 0. 0. 1. 0. 0. -0.5 -0.5 0. 0. ]
[ 0. -0.707 0. 0.75 -0.139 0. 0. 0. 0. ]
[-0.418 0. 0. -0.139 1. 0. 0. 0. -0.538]
[ 0. 0. -0.5 0. 0. 1. -0.5 0. 0. ]
[ 0. 0. -0.5 0. 0. -0.5 1. 0. 0. ]
[ 0. 0. 0. 0. 0. 0. 0. 0. 0. ]
[-0.502 0. 0. 0. -0.538 0. 0. 0. 1. ]]
有向拉普拉斯矩阵:
[[ 0.889 -0.117 -0.029 -0.087 -0.319 -0.029 -0.029 -0.129 -0.242]
[-0.117 0.889 -0.026 -0.278 -0.051 -0.026 -0.026 -0.114 -0.056]
[-0.029 -0.026 0.994 -0.012 -0.009 -0.481 -0.481 -0.025 -0.01 ]
[-0.087 -0.278 -0.012 0.757 -0.097 -0.012 -0.012 -0.052 -0.006]
[-0.319 -0.051 -0.009 -0.097 0.994 -0.009 -0.009 -0.041 -0.434]
[-0.029 -0.026 -0.481 -0.012 -0.009 0.994 -0.481 -0.025 -0.01 ]
[-0.029 -0.026 -0.481 -0.012 -0.009 -0.481 0.994 -0.025 -0.01 ]
[-0.129 -0.114 -0.025 -0.052 -0.041 -0.025 -0.025 0.889 -0.045]
[-0.242 -0.056 -0.01 -0.006 -0.434 -0.01 -0.01 -0.045 0.994]]
拉普拉斯算子的特征值:
[-1.436e-15 0.000e+00 4.610e-16 7.000e-01 1.500e+00 1.500e+00
4.576e+00 1.660e+01 2.013e+01]
邻接矩阵的特征值:
[12.068+0.000e+00j 2.588+0.000e+00j -7.219+0.000e+00j -4.925+0.000e+00j
-1.513+0.000e+00j 1. +0.000e+00j -0.5 +2.393e-17j -0.5 -2.393e-17j
0. +0.000e+00j]
无向图的代数连通性:
0.0
图的光谱排序:
['4', '2', '1', '0', '5', 'b', 'c', 'a', '3']
后面还有两个小节,由于对图论算法不是很明白,所以先讲明白算法原理,再使用networkX实现,如无须读算法,可以跳过算法原理部分.