import logging import math logging.basicConfig(level=logging.DEBUG) logger = logging.getLogger(__name__) graph = { "start": {"a": 2, "b": 2}, "a": {"b": 2}, "b": {"c": 2, "fin": 2}, "c": {"fin": 2}, "fin": {}, } costs = { "a": 2, "b": 2, "c": math.inf, "fin": math.inf, } parents = { "a": "start", "b": "start", "c": None, "fin": None, } processed = set() def find_lowest_cost_node(costs): lowest_cost = math.inf lowest_cost_node = None for node in costs: cost = costs[node] if cost < lowest_cost and node not in processed: lowest_cost = cost lowest_cost_node = node return lowest_cost_node node = find_lowest_cost_node(costs) while node is not None: cost = costs[node] neighbors = graph[node] for n in neighbors.keys(): new_cost = cost + neighbors[n] if costs[n] > new_cost: costs[n] = new_cost parents[n] = node processed.add(node) node = find_lowest_cost_node(costs) print(f"lowest cost route: {costs['fin']}")