## 怎样用python对邻接矩阵进行标准化处理（出度)

0

``````# 邻接矩阵
[0, 1, 1, 1, 1, 1, 0, 0],  # a
[0, 0, 1, 0, 1, 0, 0, 0],  # b
[0, 0, 0, 1, 0, 0, 0, 0],  # c
[0, 0, 0, 0, 1, 0, 0, 0],  # d
[0, 0, 0, 0, 0, 1, 0, 0],  # e
[0, 0, 1, 0, 0, 0, 1, 1],  # f
[0, 0, 0, 0, 0, 1, 0, 1],  # g
[0, 0, 0, 0, 0, 1, 1, 0]  # h
]
for y in x:
print(y,end=' ')
print()
# 邻接集的字典
G = { 'a':set('bcdef'),
'b':set('ce'),
'c':set('d'),
'd':set('e'),
'e':set('f'),
'f':set('cgh'),
'g':set('fh'),
'h':set('fg') }

out_degrees = dict((u, 0) for u in G)
in_degrees = dict((u, 0) for u in G)
for u in G:
out_degrees[u] = len(G[u])
for u in G:
for v in G[u]:
in_degrees[v] += 1
print(out_degrees)
print(in_degrees)``````

``````0 1 1 1 1 1 0 0
0 0 1 0 1 0 0 0
0 0 0 1 0 0 0 0
0 0 0 0 1 0 0 0
0 0 0 0 0 1 0 0
0 0 1 0 0 0 1 1
0 0 0 0 0 1 0 1
0 0 0 0 0 1 1 0
{'a': 5, 'b': 2, 'c': 1, 'd': 1, 'e': 1, 'f': 3, 'g': 2, 'h': 2}
{'a': 0, 'b': 1, 'c': 3, 'd': 2, 'e': 3, 'f': 4, 'g': 2, 'h': 2}``````

0

``````# 邻接矩阵
[0, 1, 1, 1, 1, 1, 0, 0],  # a
[0, 0, 1, 0, 1, 0, 0, 0],  # b
[0, 0, 0, 1, 0, 0, 0, 0],  # c
[0, 0, 0, 0, 1, 0, 0, 0],  # d
[0, 0, 0, 0, 0, 1, 0, 0],  # e
[0, 0, 1, 0, 0, 0, 1, 1],  # f
[0, 0, 0, 0, 0, 1, 0, 1],  # g
[0, 0, 0, 0, 0, 1, 1, 0]  # h
]
vertex=['a','b','c','d','e','f','g','h']

def outDegree(mat, name):
print('出度：')
i=0
for x in mat:
degree=0
for y in x:
if y==1:
degree = degree + 1
print(name[i] ,'出度:',degree, end=', ')
i=i+1

def intDegree(mat, name):
print('\n入度：')
for i in range(0, len(mat)):
degree=0
for j in range(0, len(mat)):
if mat[j][i]==1:
degree = degree + 1
print(name[i], '入度：',degree, end=', ')

``````D:\untitled\venv\Scripts\python.exe D:/pythonProject0/out_int_Degree.py