【问题标题】:Efficent algorithm for finding the shortest paths from ALL nodes to a node?查找从所有节点到节点的最短路径的有效算法?
【发布时间】:2020-07-04 00:49:17
【问题描述】:

我有兴趣找到从所有点到网络中特定点的最短路径。到目前为止,我一直在通过循环所有点来计算最短路径来估计这一点。

随着网络规模的增加,这种方法的计算成本会很高。有没有有效的替代方案?我忽略了 NetworkX 中的任何内容?

# Pseudocode 
For a_node in list_of_nodes:
  shortest_path_to_poi = nx.shortest_path(G, source=a_node, target=poi, method='dijkstra')

感谢您的时间和考虑。

【问题讨论】:

标签: python networkx graph-theory


【解决方案1】:

Dijkstra 的算法通常通过查找从源节点到每个其他节点的所有最短路径来实现 - 参见例如wikipedia。在 NetworkX 中,您可以使用nx.single_source_dijkstra

lengths, paths = nx.single_source_dijkstra(G, source=poi)
# paths[v] is the shortest path from poi to the vertex with index v

另请参阅 nx 文档中的 shortest paths reference

【讨论】:

  • 如果图形是定向的,您需要在反向图形上执行此操作。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-01-18
  • 1970-01-01
  • 2011-01-26
  • 2021-11-30
  • 1970-01-01
  • 1970-01-01
  • 2021-01-01
相关资源
最近更新 更多