有向图不对称邻接矩阵变无向图对称邻接矩阵
一个简单图实例

import numpy as np
# 使用numpy编写的上述有向图的邻接矩阵如下:
# 不对称邻接矩阵
A = np.matrix([
[0,1,0,0],
[0,0,1,1],
[0,1,0,0],
[1,0,1,0]],
dtype=float
)
A.T
matrix([[0., 0., 0., 1.],
[1., 0., 1., 0.],
[0., 1., 0., 1.],
[0., 1., 0., 0.]])
a = A.T > A
a=a.astype(float)
a
matrix([[0., 0., 0., 1.],
[1., 0., 0., 0.],
[0., 0., 0., 1.],
[0., 1., 0., 0.]])
np.multiply(A.T,a)
matrix([[0., 0., 0., 1.],
[1., 0., 0., 0.],
[0., 0., 0., 1.],
[0., 1., 0., 0.]])
# 变为对称邻接矩阵
A = A + np.multiply(A.T,a) - np.multiply(A,a)
A
matrix([[0., 1., 0., 1.],
[1., 0., 1., 1.],
[0., 1., 0., 1.],
[1., 1., 1., 0.]])
8653



被折叠的 条评论
为什么被折叠?



