【发布时间】:2018-12-30 13:36:47
【问题描述】:
如何在 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库了吗?
标签: python networkx graph-theory breadth-first-search