1. python多個起點不交叉最短路徑
1 針對給定的多個起點和終點,如果要求起點之間不交叉,那麼存在最短運禪路徑。
2 因為起點之間不交叉,可以將問題簡化為多個單起點單終點的問題,可以使用 Dijkstra 演算法或者 A* 演算法等察汪求解最短路徑的演算法。
3 如果需要考慮多個起點之間的交叉情況,可以考慮使用遺傳演算法等旁沒塵啟發式演算法,不過這樣的演算法復雜度較高,需要更長的計算時間。
2. 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)
3. Python中networkx中shortest_path使用的是哪一種最短路徑方法
不全是。依據傳入的參數決定調用哪種演算法。
看源碼:至少涉及了dijkstra、廣乎灶度優先/深度優先演算法。
ifsource歲遲扮isNone:
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)