我唯一能想到的方法就是使用
simple_cycles
然后迭代所有循环/循环,以确定距离可能是多少:
cycles = [p for p in nx.simple_cycles(G)]
num_nodes = 3
for nodes in cycles:
if (len(nodes) != num_nodes):
continue
print(nodes)
total = 0
node_pairs = list(walk_list(nodes,2,1))
for pair in node_pairs:
if len(pair) == 2:
total += G[pair[0]][pair[1]]['weight']
else:
total+= G[pair[0]][nodes[0]]['weight']
print(f"Total Distance: {total}")
有没有更好的解决方案?