[英]Max from a list of all shortest paths between two nodes
我有一個無向圖
我想從網絡中兩個節點之間所有最短路徑長度的列表中計算出最大值,有什么想法可以做到嗎?
如果只想考慮源和目標的某些頂點子集,則可以執行以下操作:
# 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
的元素是此長度的路徑。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.