#广度优先搜索
graph = {
'S' : ['A','B'],
'A' : ['B','C','D'],
'B' : ['C'],
'C' : ['D'],
'D' : []
}
visited = []
queue = []
goal = 'D'
def bfs(visited, graph, node):
visited.append(node)
queue.append(node)
while queue :
s = queue.pop(0)
print(s, end = "\n")
for neighbour in graph[s]:
if neighbour not in visited:
visited.append(neighbour)
queue.append(neighbour)
if goal in visited:
break
bfs(visited,graph,'S')
#上述代码用于遍历路径.有谁能告诉我找到最短路径的密码.上述代码的输出为S A B C D