networkx
distance.yaml
BJ:
SH: 1250
GZ: 2200
NJ: 1050
WH: 1200
XA: 1100
SH:
GZ: 1500
HZ: 200
NJ: 320
WH: 850
GZ:
HZ: 1300
XA: 1700
aa.py
import yaml,sys
import networkx as nx
from networkx.utils.misc import pairwise
def load_distance_from_yamlfile(yaml_file):
try:
yaml_infos=yaml.unsafe_load(open('distance.yaml').read())
edges={}
for node,edge in yaml_infos.items():
for remote_note,weight in edge.items():
edges[node,remote_note]=int(weight)
return edges
except FileNotFoundError:
print(f"{yaml_file} can't find")
sys.exit(1)
def load_distance(G,distance):
for edges in distance:
G.add_edge(edges[0],edges[1],weight=distance.get(edges))
if __name__=='__main__':
distance=load_distance_from_yamlfile('distance.yaml')
G=nx.Graph()
load_distance(G,distance)
for path in nx.all_simple_paths(G,'BJ','HZ'):
weight=0
for edge in pairwise(path):
weight+=G.get_edge_data(edge[0],edge[1]).get("weight")
print(path,weight)