狄克斯特拉算法python

graph={}

graph["start"]={}
graph["start"]["a"]=2
graph["start"]["b"]=6

graph["a"]={}
graph["a"]["final"]=1

graph["b"]={}
graph["b"]["a"]=3
graph["b"]["final"]=5

graph["final"]={}

infinity=float('inf')
costs={}
costs["a"]=6
costs["b"]=2
costs["final"]=infinity

parents={}
parents["a"]="start"
parents["b"]="start"
parents["final"]=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)

你可能感兴趣的:(狄克斯特拉算法python)