图的权重如图所示
graph = {}
graph["start"] = {}
graph["start"]["a"] = 2
graph["start"]["b"] = 5
graph["a"] = {}
graph["a"]["b"] = 8
graph["a"]["c"] = 7
graph["b"] = {}
graph["b"]["d"] = 4
graph["b"]["c"] = 2
graph["c"] = {}
graph["c"]["fin"] = 1
graph["d"] = {}
graph["d"]["c"] = 6
graph["d"]["fin"] = 3
graph["fin"] = {}
infinity = float("inf")
costs = {}
costs["a"] = 2
costs["b"] = 5
costs["c"] = infinity
costs["d"] = infinity
costs["fin"] = infinity
parents = {}
parents["a"] = "start"
parents["b"] = "start"
parents["c"] = "start"
parents["d"] = "start"
parents["fin"] = None
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:
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)
print("cost from the start to each node:")
print(costs)
|