算法图解之狄克斯特拉算法实现
生活随笔
收集整理的這篇文章主要介紹了
算法图解之狄克斯特拉算法实现
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
狄克斯特拉算法用于在加權(quán)圖中查找最短路徑(權(quán)重不能為負(fù))
'''實現(xiàn)狄克斯特拉算法'''
graph = {}
graph['start'] = {}#在散列表graph中再建一個散列表為start,start即是里面的散列表的名字,也是graph中的key
graph['start']['a'] = 6
graph['start']['b'] = 2
#print(graph['start'].keys()) 調(diào)用key函數(shù)獲取關(guān)鍵字
#print(graph['start'])單獨打印start散列表 ---{'a': 6, 'b': 2}
#print(graph) 打印整個散列表 ---{'start': {'a': 6, 'b': 2}}
graph['a'] = {}
graph['a']['fin'] = 1graph['b'] = {} #b節(jié)點有兩個鄰點
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 = {} #存儲現(xiàn)有的到每個節(jié)點的花銷;后面就會更新
costs['a'] = 6
costs['b'] = 2
costs['fin'] = infinity
parents = {}
parents['a'] = 'start'
parents['b'] = 'start'
parents['fin'] = None
processed = []#定義一個函數(shù)來找cost最小的節(jié)點
#參數(shù)兩個:1.該節(jié)點的cost;2.該節(jié)點對應(yīng)的key
#循環(huán)所有的節(jié)點,將每個節(jié)點的costs與最小比較,如果滿足節(jié)點未經(jīng)處理且比最小還小,就進(jìn)行賦值;否則就找下一個節(jié)點
def find_lowest_node(costs):lowest_cost = infinitylowest_cost_node = Nonefor node in costs:if costs[node] < lowest_cost and node not in processed:lowest_cost = costs[node]lowest_cost_node = nodereturn lowest_cost_nodenode = find_lowest_node(costs)
while node is not None:cost = costs[node]#獲取開銷最小的節(jié)點的costsneighbors = graph[node]#獲取這個節(jié)點的鄰點,應(yīng)該是一個散列表#print(neighbors)for i in neighbors.keys():new_cost = cost + neighbors[i] #應(yīng)該要把neighbors[n]的values拿去相加if costs[i] > new_cost:costs[i] = new_costparents[i] = nodeprocessed.append(node)node = find_lowest_node(costs)
print(graph)
print(costs['fin'])
'''實現(xiàn)狄克斯特拉算法'''
# ------------整個圖的散列表(字典)--------
graph = {}
#起點
graph['start'] = {} #起點是一個散列表
graph['start']['B'] = 2 #存儲權(quán)重start---B
graph['start']['c'] = 5 #存儲權(quán)重start---C
#存儲其他節(jié)點
graph['B'] = {} #節(jié)點B所有的鄰節(jié)點
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'] = {}
# ---------實時計算消耗權(quán)重的散列表(字典)------------
infinity = float("inf") # python中表示無窮大,因為此刻的開銷還不知搭配
costs = {}
costs['B'] = 2
costs["C"] = 5
# costs["destination"] = infinity # 表示路徑還沒選擇,此時到達(dá)終點所消耗權(quán)重未知
# -----------存儲父節(jié)點的散列表----------------
parents = {}
parents["B"] = "start"
parents["C"] = "start"
parents["destination"] = None
# ----------記錄存儲過的節(jié)點-----------------
processed = []
print(graph)
def find_lowest_node(costs):lowest_cost = infinitylowest_cost_node = Nonefor node in costs:if costs[node] < lowest_cost and node not in processed:lowest_cost = costs[node]lowest_cost_node = nodereturn lowest_cost_nodenode = find_lowest_node(costs)
while node is not None:cost = costs[node]#獲取開銷最小的節(jié)點的costsneighbors = graph[node]#獲取這個節(jié)點的鄰點,應(yīng)該是一個散列表#print(neighbors)for i in neighbors.keys():new_cost = cost + neighbors[i] #應(yīng)該要把neighbors[n]的values拿去相加if costs[i] > new_cost:costs[i] = new_costparents[i] = nodeprocessed.append(node)node = find_lowest_node(costs)print(costs['fin'])
第二個程序報錯:KeyError: 'E'(未解決)
總結(jié)
以上是生活随笔為你收集整理的算法图解之狄克斯特拉算法实现的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 微信小程序-枯木学习笔记5-我的信息
- 下一篇: 服务器电源线的分类及应用