graph = {}
costs = {}
parents = {}
processed = []
def find_lowest_cost_node(costs):
lowest_cost = float("inf")
lowest_cost_node = None
for node in costs: #遍历所有的节点
cost = costs(node)
if cost < lowest_cost and node not in processed: #如果当
lowest_cost = cost #就将其视为开销最低的节点
lowest_cost_node = node
return lowest_cost_node
node = find_lowest_cost_node(costs) #在未处理的节点中找到开销最小
while node is not None: #这个while循环在所有节点都被处理过后结
cost = costs[node]
neighbors = graph[node]
for n in neighbors.keys(): #遍历当前节点的所有邻居
new_cost = cost + neighbors[n]
if costs[n] > new_cost: #如果经当前节点前往改邻居更近
costs[n] = new_cost #就更新该邻居的开销
parents[n] = node #同时将该邻居的父节点设置为当前
processed.append(node) #将当前节点标志为处理过
node = find_lowest_cost_node(costs) #找出接下来要处理的节点
01-22
5315
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
03-25
591
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
09-03
835
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)