简体   繁体   English

来自两个节点之间所有最短路径的列表中的最大值

[英]Max from a list of all shortest paths between two nodes

I have an undirected graph 我有一个无向图

I would like to calculate the max from a list of all the shortest path lengths between two nodes in the network, any ideas how I can do that? 我想从网络中两个节点之间所有最短路径长度的列表中计算出最大值,有什么想法可以做到吗?

I think you want the graph diameter which is the maximum of all-pairs shortest paths. 我认为您想要的图形直径是所有对最短路径的最大值。 https://networkx.github.io/documentation/latest/reference/generated/networkx.algorithms.distance_measures.diameter.html https://networkx.github.io/documentation/latest/reference/generation/networkx.algorithms.distance_measures.diameter.html

If you only want to consider some subset of vertices for source and target, you might do something like: 如果只想考虑源和目标的某些顶点子集,则可以执行以下操作:

# 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)

Afterward, max_shortest_path is a list. 之后, max_shortest_path是一个列表。 All elements of max_shortest_path are lists of equal length. max_shortest_path所有元素都是等长列表。

len(max_shortest_path[0]) will get you the length of the maximal shortest path in the graph. len(max_shortest_path[0])将为您提供图形中最大最短路径的长度

The elements of max_shortest_path are the paths of this length. max_shortest_path的元素是此长度的路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM