def run_maze(): visited = set() graph = Graph() travel_path = [] dfs_rooms = graph.dfs(player.current_room) rooms = [room for room in dfs_rooms] while (len(visited) < len(room_graph) - 1): current_room = rooms[0] next_room = rooms[1] shortest_path = graph.bfs(current_room, next_room) while len(shortest_path) > 1: current_room_neighbors = dfs_rooms[shortest_path[0]] next_room = shortest_path[1] if next_room in current_room_neighbors: travel_path.append(current_room_neighbors[next_room]) shortest_path.remove(shortest_path[0]) rooms.remove(current_room) visited.add(current_room) return travel_path
def earliest_ancestor(parents_children, start_vert): graph = Graph() for pair in parents_children: graph.add_vert(pair) parents = {p for p, c in parents_children} children = {c for p, c in parents_children} paths = [] for p in parents: route = graph.dfs(start_vert, p) paths.append(route) valid_paths = [p for p in paths if p != None and len(p) > 1] # valid_paths = list(filter(lambda p: p != None, paths)) if len(valid_paths) == 0: return -1 longest_path = max(valid_paths, key=len) return longest_path[-1]