import rustworkx as rx graph_dict = { 'kerbol': { 'moho': {}, 'eve': {'gilly': {}}, 'kerbin': {'mun': {}, 'minmus': {}}, 'duna': {'ike': {}}, 'dres': {}, 'jool': {'laythe': {}, 'vall': {}, 'tylo': {}, 'bop': {}, 'pol': {}}, 'eeloo': {}, } } def create_graph_node(graph_map, name, graph_dict): ground_name = "{}_ground".format(name) ground = graph_map.add_node(ground_name) orbit_name = "{}_orbit".format(name) orbit = graph_map.add_node(orbit_name) graph_map.add_edge(orbit, ground, 1) for body_name, body_dict in graph_dict.items(): child_orbit = create_graph_node(graph_map, body_name, body_dict) graph_map.add_edge(orbit, child_orbit, 1) return orbit class Map: instance = None @classmethod def get_map(cls): if cls.instance is None: cls.instance = Map() return cls.instance def __init__(self): self.graph = rx.PyGraph() self.index_dict = {} create_graph_node(self.graph, 'kerbol', graph_dict['kerbol']) self.reverse_index = self.graph.nodes() self.index_dict = {value: key for key, value in enumerate(self.reverse_index)} def get_shortest_path(self, body_a, body_b): result = rx.dijkstra_shortest_paths(self.graph, self.index_dict[body_a], self.index_dict[body_b]) return [self.reverse_index[body_index] for body_index in result[self.index_dict[body_b]]]