算法图解之狄克斯特拉算法实现

狄克斯特拉算法用于在加权图中查找最短路径(权重不能为负)
'''实现狄克斯特拉算法'''
graph = {}
graph['start'] = {}#在散列表graph中再建一个散列表为start,start即是里面的散列表的名字,也是graph中的key
graph['start']['a'] = 6
graph['start']['b'] = 2
#print(graph['start'].keys()) 调用key函数获取关键字
#print(graph['start'])单独打印start散列表 ---{'a': 6, 'b': 2}
#print(graph)  打印整个散列表 ---{'start': {'a': 6, 'b': 2}}
graph['a'] = {}
graph['a']['fin'] = 1

graph['b'] = {}  #b节点有两个邻点
graph['b']['a'] = 3
graph['b']['fin'] = 5
graph['fin'] = {}
#print(graph) ---{'start': {'a': 6, 'b': 2}, 'a': {'fin': 1}, 'b': {'a': 3, 'fin': 5}}
infinity = float('inf')#无穷大
costs = {}  #存储现有的到每个节点的花销;后面就会更新
costs['a'] = 6
costs['b'] = 2
costs['fin'] = infinity
parents = {}
parents['a'] = 'start'
parents['b'] = 'start'
parents['fin'] = None
processed = []

#定义一个函数来找cost最小的节点
#参数两个:1.该节点的cost;2.该节点对应的key
#循环所有的节点,将每个节点的costs与最小比较,如果满足节点未经处理且比最小还小,就进行赋值;否则就找下一个节点
def find_lowest_node(costs):
    lowest_cost = infinity
    lowest_cost_node = None
    for node in costs:
        if costs[node] < lowest_cost and node not in processed:
            lowest_cost = costs[node]
            lowest_cost_node = node
    return lowest_cost_node

node = find_lowest_node(costs)
while node is not None:
    cost = costs[node]#获取开销最小的节点的costs
    neighbors = graph[node]#获取这个节点的邻点,应该是一个散列表
    #print(neighbors)
    for i in neighbors.keys():
        new_cost = cost + neighbors[i] #应该要把neighbors[n]的values拿去相加
        if costs[i] > new_cost:
            costs[i] = new_cost
            parents[i] = node
    processed.append(node)
    node = find_lowest_node(costs)
print(graph)
print(costs['fin'])
'''实现狄克斯特拉算法'''
# ------------整个图的散列表(字典)--------
graph = {}
#起点
graph['start'] = {} #起点是一个散列表
graph['start']['B'] = 2  #存储权重start---B
graph['start']['c'] = 5  #存储权重start---C
#存储其他节点
graph['B'] = {}  #节点B所有的邻节点
graph['B']['C'] = 8  #B---C
graph['B']['E'] = 7  #B---E
graph['C'] = {}
graph['C']['D'] = 4
graph['C']['E'] = 2
graph['D'] = {}
graph['D']['E'] = 6
graph['D']['fin'] = 3
graph['E'] = {}
graph['E']['fin'] = 1
#终点
graph['fin'] = {}
# ---------实时计算消耗权重的散列表(字典)------------
infinity = float("inf") # python中表示无穷大,因为此刻的开销还不知搭配
costs = {}
costs['B'] = 2
costs["C"] = 5
# costs["destination"] = infinity # 表示路径还没选择,此时到达终点所消耗权重未知
# -----------存储父节点的散列表----------------
parents = {}
parents["B"] = "start"
parents["C"] = "start"
parents["destination"] = None
# ----------记录存储过的节点-----------------
processed = []
print(graph)
def find_lowest_node(costs):
    lowest_cost = infinity
    lowest_cost_node = None
    for node in costs:
        if costs[node] < lowest_cost and node not in processed:
            lowest_cost = costs[node]
            lowest_cost_node = node
    return lowest_cost_node

node = find_lowest_node(costs)
while node is not None:
    cost = costs[node]#获取开销最小的节点的costs
    neighbors = graph[node]#获取这个节点的邻点,应该是一个散列表
    #print(neighbors)
    for i in neighbors.keys():
        new_cost = cost + neighbors[i] #应该要把neighbors[n]的values拿去相加
        if costs[i] > new_cost:
            costs[i] = new_cost
            parents[i] = node
    processed.append(node)
    node = find_lowest_node(costs)

print(costs['fin'])

第二个程序报错:KeyError: 'E'(未解决)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值