graph = {'保安':['巡检', '巡逻', '监控'], '监控':['监视','密切','白领'], '巡逻':['白领', '蓝领', '科学家'], '白领':['销售', '大堂经理', '采购']} def bfs(graph, root): queue = [root] seen = {root} parent = {root:None} while queue: node = queue.pop(0) print(node) neighbors = graph.get(node) if neighbors: for each in neighbors: queue.append(each) seen.add(each) parent[each] = node return parent parent = test(graph, '监控') print(parent) # 从采购出发到监控的最短路径 node = '采购' while node: print(node) node = parent[node]
标签:node,parent,graph,路径,简单,queue,each,root From: https://www.cnblogs.com/demo-deng/p/17223139.html