python矩陣/字典實現(xiàn)最短路徑算法
前言:好像感覺各種博客的最短路徑python實現(xiàn)都花里胡哨的?輸出不明顯,唉,可能是因為不想讀別人的代碼吧(明明自己學(xué)過離散)。然后可能有些人是用字典實現(xiàn)的?的確字典的話,比較省空間。改天,也用字典試下。先貼個圖吧。
然后再貼代碼:
_=inf=999999#inf def Dijkstra_all_minpath(start,matrix): length=len(matrix)#該圖的節(jié)點數(shù) path_array=[] temp_array=[] path_array.extend(matrix[start])#深復(fù)制 temp_array.extend(matrix[start])#深復(fù)制 temp_array[start] = inf#臨時數(shù)組會把處理過的節(jié)點的值變成inf,表示不是最小權(quán)值的節(jié)點了 already_traversal=[start]#start已處理 path_parent=[start]*length#用于畫路徑,記錄此路徑中該節(jié)點的父節(jié)點 while(len(already_traversal)<length): i= temp_array.index(min(temp_array))#找最小權(quán)值的節(jié)點的坐標(biāo) temp_array[i]=inf path=[]#用于畫路徑 path.append(str(i)) k=i while(path_parent[k]!=start):#找該節(jié)點的父節(jié)點添加到path,直到父節(jié)點是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):#這個不用多說了吧 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#說明父節(jié)點是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]
主要是這樣輸出的話比較好看,然后這樣算是直接算一個點到所有點的最短路徑吧。那么寫下字典實現(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)值點 already_traversal.add(min_node)#把找到的最小節(jié)點添加進去 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é)點的父節(jié)點添加到path,直到父節(jié)點是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é)點 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é)點添加進去 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}
還行吧,有時間再看看networkx這個庫怎么說。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Pytorch實現(xiàn)圖像識別之?dāng)?shù)字識別(附詳細注釋)
這篇文章主要介紹了Pytorch實現(xiàn)圖像識別之?dāng)?shù)字識別(附詳細注釋),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-05-05Python基礎(chǔ)練習(xí)之用戶登錄實現(xiàn)代碼分享
這篇文章主要介紹了Python基礎(chǔ)練習(xí)之用戶登錄實現(xiàn)代碼分享,還是比較不錯的,這里分享給大家,供需要的朋友參考。2017-11-11python如何通過Json路徑返回Json響應(yīng)對應(yīng)的值
這篇文章主要介紹了python如何通過Json路徑返回Json響應(yīng)對應(yīng)的值問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-06-06matplotlib基礎(chǔ)繪圖命令之bar的使用方法
這篇文章主要介紹了matplotlib基礎(chǔ)繪圖命令之bar的使用方法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08Python中使用pypdf2合并、分割、加密pdf文件的代碼詳解
這篇文章主要介紹了Python中使用pypdf2合并、分割、加密pdf文件的代碼,本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2019-05-05matplotlib之pyplot模塊坐標(biāo)軸范圍設(shè)置(autoscale(),xlim(),ylim())
這篇文章主要介紹了matplotlib之pyplot模塊坐標(biāo)軸范圍設(shè)置(autoscale(),xlim(),ylim()),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03Python列表list內(nèi)建函數(shù)用法實例分析【insert、remove、index、pop等】
這篇文章主要介紹了Python列表list內(nèi)建函數(shù)用法,結(jié)合具體實例形式分析了list中insert、remove、index、pop等函數(shù)的功能、使用方法與相關(guān)注意事項,需要的朋友可以參考下2017-07-07