Generation

generate functionSat, 12 Nov 2022

find the shortest path between two nodes on a graph

def find_shortest_path(current_node, destination_node): if(current_node == destination_node): return [] # Otherwise try all the other possible neighbours shortest_path = None for neighbour in current_node.neighbours: new_path = find_shortest_path(neighbour, destination_node) if (new_path is not None) and ((shortest_path is None) or (len(new_path) < len(shortest_path))): shortest_path = new_path return [current_node] + shortest_path

Questions about programming?Chat with your personal AI assistant