如何在Python中使用给定的邻接矩阵打印图的所有边缘?例如,如果0与3和8相邻,则应打印:
0 3
0 8
不重复
我一直在使用Bfs,但我不知道如何更新队列和当前元素。
以下是迄今为止我的代码:
以下是迄今为止我的代码:
A = [[0, 1, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 1],
[0, 0, 0, 1, 1, 0],
[0, 0, 0, 0, 1, 0],
[0, 0, 0, 0, 0, 0],
[1, 0, 0, 0, 0, 0]]
def edges(A):
visited = [False] * len(A)
queue = []
s = [0][0]
queue.append(s)
visited[s] = True
while len(queue) > 0:
s = queue.pop(0)
print(s)
for i in range(len(A)):
print(i)
for j in range(len(A[0])):
if A[i][j] == 1 and visited[s]== False:
queue.append([i][j])
visited[s] = True
print(edges(A))
networkx
库了吗? - Paritosh Singh