#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Date : 2018-11-30 16:35:01
# @Author : rorg (928758777)
# @Link : QQ:928758777
# @Version : $Id$
import os
graph = {}
graph['start'] = {}
graph['start']['a'] = 6
graph['start']['b'] = 2
graph['a'] = {}
graph['a']['c'] = 1
# graph['a']['fin'] = 1
graph['c'] = {}
graph['c']['fin'] = 1
graph['b'] = {}
graph['b']['a'] = 3
graph['b']['fin'] = 5
graph['fin'] = {}
infinity = float('inf')
costs = {}
costs['a'] = 6
costs['b'] = 2
costs['c'] = infinity
costs['fin'] = infinity
parents = {}
parents['a'] = 'start'
parents['b'] = 'start'
parents['c'] = 'a'
parents['fin'] = None
processed = []
def find_lowest_cost_node(costs):
lowest_cost = float('inf')
# print("ss", 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)
# print('nd:', node)
while node is not None:
cost = costs[node]
# print('node:', node)
neighbors = graph[node]
# print(neighbors)
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)
print(costs)
print(processed)