python矩陣/字典實(shí)現(xiàn)最短路徑算法
前言:好像感覺(jué)各種博客的最短路徑python實(shí)現(xiàn)都花里胡哨的?輸出不明顯,唉,可能是因?yàn)椴幌胱x別人的代碼吧(明明自己學(xué)過(guò)離散)。然后可能有些人是用字典實(shí)現(xiàn)的?的確字典的話,比較省空間。改天,也用字典試下。先貼個(gè)圖吧。
然后再貼代碼:
_=inf=999999#inf def Dijkstra_all_minpath(start,matrix): length=len(matrix)#該圖的節(jié)點(diǎn)數(shù) path_array=[] temp_array=[] path_array.extend(matrix[start])#深復(fù)制 temp_array.extend(matrix[start])#深復(fù)制 temp_array[start] = inf#臨時(shí)數(shù)組會(huì)把處理過(guò)的節(jié)點(diǎn)的值變成inf,表示不是最小權(quán)值的節(jié)點(diǎn)了 already_traversal=[start]#start已處理 path_parent=[start]*length#用于畫(huà)路徑,記錄此路徑中該節(jié)點(diǎn)的父節(jié)點(diǎn) while(len(already_traversal)<length): i= temp_array.index(min(temp_array))#找最小權(quán)值的節(jié)點(diǎn)的坐標(biāo) temp_array[i]=inf path=[]#用于畫(huà)路徑 path.append(str(i)) k=i while(path_parent[k]!=start):#找該節(jié)點(diǎn)的父節(jié)點(diǎn)添加到path,直到父節(jié)點(diǎn)是start path.append(str(path_parent[k])) k=path_parent[k] path.append(str(start)) path.reverse()#path反序產(chǎn)生路徑 print(str(i)+':','->'.join(path))#打印路徑 already_traversal.append(i)#該索引已經(jīng)處理了 for j in range(length):#這個(gè)不用多說(shuō)了吧 if j not in already_traversal: if (path_array[i]+matrix[i][j])<path_array[j]: path_array[j] = temp_array[j] =path_array[i]+matrix[i][j] path_parent[j]=i#說(shuō)明父節(jié)點(diǎn)是i return path_array #領(lǐng)接矩陣 adjacency_matrix=[[0,10,_,30,100], [10,0,50,_,_], [_,50,0,20,10], [30,_,20,0,60], [100,_,10,60,0] ] print(Dijkstra_all_minpath(4,adjacency_matrix))
然后輸出:
2: 4->2
3: 4->2->3
0: 4->2->3->0
1: 4->2->1
[60, 60, 10, 30, 0]
主要是這樣輸出的話比較好看,然后這樣算是直接算一個(gè)點(diǎn)到所有點(diǎn)的最短路徑吧。那么寫(xiě)下字典實(shí)現(xiàn)吧
def Dijkstra_all_minpath_for_graph(start,graph): inf = 999999 # inf length=len(graph) path_graph={k:inf for k in graph.keys()} already_traversal=set() path_graph[start]=0 min_node=start#初始化最小權(quán)值點(diǎn) already_traversal.add(min_node)#把找到的最小節(jié)點(diǎn)添加進(jìn)去 path_parent={k:start for k in graph.keys()} while(len(already_traversal)<=length): p = min_node if p!=start: path = [] path.append(str(p)) while (path_parent[p] != start):#找該節(jié)點(diǎn)的父節(jié)點(diǎn)添加到path,直到父節(jié)點(diǎn)是start path.append(str(path_parent[p])) p=path_parent[p] path.append(str(start)) path.reverse()#反序 print(str(min_node) + ':', '->'.join(path))#打印 if(len(already_traversal)==length):break for k in path_graph.keys():#更新距離 if k not in already_traversal: if k in graph[min_node].keys() and (path_graph[min_node]+graph[min_node][k])<path_graph[k]: path_graph[k]=path_graph[min_node]+graph[min_node][k] path_parent[k]=min_node min_value=inf for k in path_graph.keys():#找最小節(jié)點(diǎn) if k not in already_traversal: if path_graph[k]<min_value: min_node=k min_value=path_graph[k] already_traversal.add(min_node)#把找到最小節(jié)點(diǎn)添加進(jìn)去 return path_graph adjacency_graph={0:{1:10,3:30,4:100}, 1:{0:10,2:50}, 2:{1:50,3:20,4:10}, 3:{0:30,2:20,4:60}, 4:{0:100,2:10,3:60}} print(Dijkstra_all_minpath_for_graph(4,adjacency_graph))
輸出:
2: 4->2
3: 4->2->3
0: 4->2->3->0
1: 4->2->1
{0: 60, 1: 60, 2: 10, 3: 30, 4: 0}
還行吧,有時(shí)間再看看networkx這個(gè)庫(kù)怎么說(shuō)。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Pytorch實(shí)現(xiàn)圖像識(shí)別之?dāng)?shù)字識(shí)別(附詳細(xì)注釋)
這篇文章主要介紹了Pytorch實(shí)現(xiàn)圖像識(shí)別之?dāng)?shù)字識(shí)別(附詳細(xì)注釋),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-05-05Python基礎(chǔ)練習(xí)之用戶(hù)登錄實(shí)現(xiàn)代碼分享
這篇文章主要介紹了Python基礎(chǔ)練習(xí)之用戶(hù)登錄實(shí)現(xiàn)代碼分享,還是比較不錯(cuò)的,這里分享給大家,供需要的朋友參考。2017-11-11python如何通過(guò)Json路徑返回Json響應(yīng)對(duì)應(yīng)的值
這篇文章主要介紹了python如何通過(guò)Json路徑返回Json響應(yīng)對(duì)應(yīng)的值問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-06-06matplotlib基礎(chǔ)繪圖命令之bar的使用方法
這篇文章主要介紹了matplotlib基礎(chǔ)繪圖命令之bar的使用方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08django 中使用DateTime常用的時(shí)間查詢(xún)方式
今天小編就為大家分享一篇django 中使用DateTime常用的時(shí)間查詢(xún)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-12-12Python中使用pypdf2合并、分割、加密pdf文件的代碼詳解
這篇文章主要介紹了Python中使用pypdf2合并、分割、加密pdf文件的代碼,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-05-05matplotlib之pyplot模塊坐標(biāo)軸范圍設(shè)置(autoscale(),xlim(),ylim())
這篇文章主要介紹了matplotlib之pyplot模塊坐標(biāo)軸范圍設(shè)置(autoscale(),xlim(),ylim()),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03python+pandas分析nginx日志的實(shí)例
下面小編就為大家分享一篇python+pandas分析nginx日志的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-04-04Python列表list內(nèi)建函數(shù)用法實(shí)例分析【insert、remove、index、pop等】
這篇文章主要介紹了Python列表list內(nèi)建函數(shù)用法,結(jié)合具體實(shí)例形式分析了list中insert、remove、index、pop等函數(shù)的功能、使用方法與相關(guān)注意事項(xiàng),需要的朋友可以參考下2017-07-07