python最短路徑
㈠ python中怎樣求解最短路能不能給個例子,謝謝各位
graph = {'A': ['B', 'C'],
'B': ['C', 'D'],
'C': ['D'],
'D': ['C'],
'E': ['F'],
'F': ['C']}
def find_shortest_path(graph, start, end, path=[]):
path = path + [start]
if start == end:
return path
if not graph.has_key(start):
return None
shortest = None
for node in graph[start]:
if node not in path:
newpath = find_shortest_path(graph, node, end, path)
if newpath:
if not shortest or len(newpath) < len(shortest):
shortest = newpath
return shortest
>>> find_shortest_path(graph, 'A', 'D')
['A', 'C', 'D']
>>>
㈡ Python 嵌套列表求最短線路!
你好,答案如下所示。
沒看懂你的返回值是哪個到哪個的最短路徑,比如第三個數字4,看了半天,好像沒看到哪個的最短路徑是4,應該都比4小喔
希望你能夠詳細查看。
如果你有不會的,你可以提問
我有時間就會幫你解答。
希望你好好學習。
每一天都過得充實。
㈢ Python中networkx中shortest_path使用的是哪一種最短路徑方法
不全是。依據傳入的參數決定調用哪種演算法。
看源碼:至少涉及了dijkstra、廣度優先/深度優先演算法。
ifsourceisNone:
iftargetisNone:
##Findpathsbetweenallpairs.
ifweightisNone:
paths=nx.all_pairs_shortest_path(G)
else:
paths=nx.all_pairs_dijkstra_path(G,weight=weight)
else:
##Findpathsfromallnodesco-accessibletothetarget.
directed=G.is_directed()
ifdirected:
G.reverse(=False)
ifweightisNone:
paths=nx.single_source_shortest_path(G,target)
else:
paths=nx.single_source_dijkstra_path(G,target,weight=weight)
#.
fortargetinpaths:
paths[target]=list(reversed(paths[target]))
ifdirected:
G.reverse(=False)
else:
iftargetisNone:
##.
ifweightisNone:
paths=nx.single_source_shortest_path(G,source)
else:
paths=nx.single_source_dijkstra_path(G,source,weight=weight)
else:
##Findshortestsource-targetpath.
ifweightisNone:
paths=nx.bidirectional_shortest_path(G,source,target)
else:
paths=nx.dijkstra_path(G,source,target,weight)
㈣ Python中怎麼求有權圖的最短路徑
Dijkstra演算法。
㈤ 求助python的最短路徑問題
這是一個深度優先搜索演算法(Deepth First Search, DFS)
演算法核心是不斷遞歸,直到找到目標,入隊一種可能方案,return返回上一遞歸,再次嘗試以當前點開始計算有沒有其他方案,如有則繼續遞歸並入隊,如沒有則再次return
簡單來說就是這樣的結構:
def dfs(position, value):
# position 傳參位置,value 傳參到現在的計算結果
if 到達目標:
判斷value是否比最短路徑短
return value
else:
for x in position的所有可能下一路徑:
if x在路徑列表中:
# 不能有重復路徑,變成回環
continue
else:
獲取路徑x的值
改變position
入隊 dfs(new_position, value+x
這個代碼用的是字典存儲每個點可到達的點以及路程
然後深度優先搜索
不懂再追問
㈥ python networkx模塊裡面計算最短路徑時,如何處理等價路徑我怎麼測試只能顯示1條路徑,請大神賜教。
if source is None: if target is None: ## Find paths between all pairs. if weight is None: paths=nx.all_pairs_shortest_path(G) else: paths=nx.all_pairs_dijkstra_path(G,weight=weight) else: ## Find paths from all nodes co-accessible to the target. directed = G.is_directed() if directed: G.reverse(=False) if weight is None: paths=nx.single_source_shortest_path(G,target) else: paths=nx.single_source_dijkstra_path(G,target,weight=weight) # Now flip the paths so they go from a source to the target. for target in paths: paths[target] = list(reversed(paths[target])) if directed: G.reverse(=False) else: if target is None: ## Find paths to all nodes accessible from the source. if weight is None: paths=nx.single_source_shortest_path(G,source) else: paths=nx.single_source_dijkstra_path(G,source,weight=weight) else: ## Find shortest source-target path. if weight is None: paths=nx.bidirectional_shortest_path(G,source,target) else: paths=nx.dijkstra_path(G,source,target,weight)
㈦ python問題。建立9x9的方格 然後求最短距離
讀取地圖數據,建立地圖。
讀取起始點,終止點。
// 系統中肯定存在多條路徑,這些路徑 應該是以起點為根,以終點為葉到發生樹。
// 路徑不會短於 |x1-x2|+|y1-y2|
首先計算出一條路徑,作為當前路徑。
繼續計算下一條路徑。
如果這條路徑長於當前路徑,終止計算。 從終止計算點以下,所有樹葉全部舍棄。
否則將該條路徑當作當前路徑。繼續計算 直至算玩所以路徑或者找到最短路徑。
合理利用樹演算法。
㈧ Python 怎麼求graph的最短線路!!
不是有求最短路徑演算法嗎,迪傑斯特拉,用其對應的python實現就行
㈨ 如何用python在arcgis中編寫程序,求兩點的最短路徑
你是想學PYTHON編程還是只是想只得到這個PYTHON文件。可以給你提供一條簡潔的途徑用modelbuilder來實現,將內多個SHP文件拖入進去容,再把MERGE工具拖進去,雙擊modelbuilder中的merge工具框設置,再雙擊output dataset框設置輸出。然後將這些shp文件用倒數第二個按鈕添加鏈接的功能將他們一個個與merge工具框鏈接起來。最後點擊model-export-to srcipt-python 就會輸出一個python文件,可以用記事本打開查看裡面的代碼。