本文簡要介紹
networkx.algorithms.shortest_paths.dense.floyd_warshall_predecessor_and_distance
的用法。用法:
floyd_warshall_predecessor_and_distance(G, weight='weight')
使用 Floyd 算法查找 all-pairs 最短路徑長度。
- G:NetworkX 圖
- weight: string, optional (default= ‘weight’):
邊權重對應的邊數據鍵。
- predecessor,distance:字典
字典,由源和目標鍵控,前身和最短路徑的距離。
參數:
返回:
注意:
當 Dijkstra 算法失敗時,Floyd 算法適用於在密集圖或負權重圖中尋找最短路徑。如果存在負循環,該算法仍然可能失敗。它的運行時間為 ,運行空間為 。
例子:
>>> G = nx.DiGraph() >>> G.add_weighted_edges_from( ... [ ... ("s", "u", 10), ... ("s", "x", 5), ... ("u", "v", 1), ... ("u", "x", 2), ... ("v", "y", 1), ... ("x", "u", 3), ... ("x", "v", 5), ... ("x", "y", 2), ... ("y", "s", 7), ... ("y", "v", 6), ... ] ... ) >>> predecessors, _ = nx.floyd_warshall_predecessor_and_distance(G) >>> print(nx.reconstruct_path("s", "v", predecessors)) ['s', 'x', 'u', 'v']
相關用法
- Python NetworkX from_dict_of_dicts用法及代碼示例
- Python NetworkX from_scipy_sparse_array用法及代碼示例
- Python NetworkX from_pandas_adjacency用法及代碼示例
- Python NetworkX from_scipy_sparse_matrix用法及代碼示例
- Python NetworkX full_join用法及代碼示例
- Python NetworkX find_induced_nodes用法及代碼示例
- Python NetworkX from_dict_of_lists用法及代碼示例
- Python NetworkX from_pydot用法及代碼示例
- Python NetworkX from_edgelist用法及代碼示例
- Python NetworkX freeze用法及代碼示例
- Python NetworkX find_cycle用法及代碼示例
- Python NetworkX from_nested_tuple用法及代碼示例
- Python NetworkX from_graph6_bytes用法及代碼示例
- Python NetworkX from_prufer_sequence用法及代碼示例
- Python NetworkX from_pandas_edgelist用法及代碼示例
- Python NetworkX from_numpy_matrix用法及代碼示例
- Python NetworkX from_sparse6_bytes用法及代碼示例
- Python NetworkX find_threshold_graph用法及代碼示例
- Python NetworkX from_agraph用法及代碼示例
- Python NetworkX from_numpy_array用法及代碼示例
- Python NetworkX negative_edge_cycle用法及代碼示例
- Python NetworkX voronoi_cells用法及代碼示例
- Python NetworkX numerical_edge_match用法及代碼示例
- Python NetworkX inverse_line_graph用法及代碼示例
- Python NetworkX LFR_benchmark_graph用法及代碼示例
注:本文由純淨天空篩選整理自networkx.org大神的英文原創作品 networkx.algorithms.shortest_paths.dense.floyd_warshall_predecessor_and_distance。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。