日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發(fā)布時間:2024/3/13 编程问答 50 豆豆
生活随笔 收集整理的這篇文章主要介紹了 算法图解之狄克斯特拉算法实现 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
狄克斯特拉算法用于在加權(quán)圖中查找最短路徑(權(quán)重不能為負(fù)) '''實(shí)現(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'])單獨(dú)打印start散列表 ---{'a': 6, 'b': 2} #print(graph) 打印整個散列表 ---{'start': {'a': 6, 'b': 2}} graph['a'] = {} graph['a']['fin'] = 1graph['b'] = {} #b節(jié)點(diǎn)有兩個鄰點(diǎn) 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é)點(diǎn)的花銷;后面就會更新 costs['a'] = 6 costs['b'] = 2 costs['fin'] = infinity parents = {} parents['a'] = 'start' parents['b'] = 'start' parents['fin'] = None processed = []#定義一個函數(shù)來找cost最小的節(jié)點(diǎn) #參數(shù)兩個:1.該節(jié)點(diǎn)的cost;2.該節(jié)點(diǎn)對應(yīng)的key #循環(huán)所有的節(jié)點(diǎn),將每個節(jié)點(diǎn)的costs與最小比較,如果滿足節(jié)點(diǎn)未經(jīng)處理且比最小還小,就進(jìn)行賦值;否則就找下一個節(jié)點(diǎn) 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é)點(diǎn)的costsneighbors = graph[node]#獲取這個節(jié)點(diǎn)的鄰點(diǎn),應(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']) '''實(shí)現(xiàn)狄克斯特拉算法''' # ------------整個圖的散列表(字典)-------- graph = {} #起點(diǎn) graph['start'] = {} #起點(diǎn)是一個散列表 graph['start']['B'] = 2 #存儲權(quán)重start---B graph['start']['c'] = 5 #存儲權(quán)重start---C #存儲其他節(jié)點(diǎn) graph['B'] = {} #節(jié)點(diǎn)B所有的鄰節(jié)點(diǎn) 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 #終點(diǎn) graph['fin'] = {} # ---------實(shí)時計算消耗權(quán)重的散列表(字典)------------ infinity = float("inf") # python中表示無窮大,因?yàn)榇丝痰拈_銷還不知搭配 costs = {} costs['B'] = 2 costs["C"] = 5 # costs["destination"] = infinity # 表示路徑還沒選擇,此時到達(dá)終點(diǎn)所消耗權(quán)重未知 # -----------存儲父節(jié)點(diǎn)的散列表---------------- parents = {} parents["B"] = "start" parents["C"] = "start" parents["destination"] = None # ----------記錄存儲過的節(jié)點(diǎn)----------------- 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é)點(diǎn)的costsneighbors = graph[node]#獲取這個節(jié)點(diǎn)的鄰點(diǎn),應(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)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。