【问题标题】:Max from a list of all shortest paths between two nodes两个节点之间所有最短路径列表中的最大值
【发布时间】:2015-05-17 23:25:03
【问题描述】:

我有一个无向图

我想从网络中两个节点之间所有最短路径长度的列表中计算最大值,有什么想法可以做到吗?

【问题讨论】:

  • 最大最短路径是什么意思?哪些节点之间的最短路径?
  • 对不起,我没说清楚,我澄清了。谢谢!

标签: python networkx shortest-path


【解决方案1】:

如果您只想考虑源和目标的一些顶点子集,您可以执行以下操作:

# Change these to fit your needs
sources = G.nodes()     # For example, sources = [0,1,4]
targets = G.nodes()

max_shortest_path = None
for (s,t) in itertools.product(sources, targets):
    if s == t: continue # Ignore
    shortest_paths = list(nx.all_shortest_paths(G, s, t))
    path_len = len(shortest_paths[0])
    if max_shortest_path is None or path_len > len(max_shortest_path[0]):
        max_shortest_path = list(shortest_paths)    # Copy shortest_paths list
    elif path_len == len(max_shortest_path[0]):
        max_shortest_path.extend(shortest_paths)

之后,max_shortest_path 是一个列表。 max_shortest_path 的所有元素都是等长的列表。

len(max_shortest_path[0]) 将为您提供图中最大最短路径的长度

max_shortest_path 的元素就是这个长度的路径。

【讨论】:

    【解决方案2】:

    【讨论】:

      猜你喜欢
      • 2016-03-04
      • 2017-04-18
      • 1970-01-01
      • 2023-01-15
      • 2021-12-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-12
      相关资源
      最近更新 更多