from utils import Graph from colorama import Fore from PyInquirer import prompt graph = Graph() selection = "" graph.add_vertex("A") graph.add_vertex("A#/Bb") graph.add_vertex("B") graph.add_vertex("C") graph.add_vertex("C#/Db") graph.add_vertex("D") graph.add_vertex("D#/Eb") graph.add_vertex("E") graph.add_vertex("F") graph.add_vertex("F#/Gb") graph.add_vertex("G") graph.add_vertex("G#/Ab") graph.add_edge("A", "A#/Bb") graph.add_edge("A#/Bb", "B") graph.add_edge("B", "C") graph.add_edge("C", "C#/Db") graph.add_edge("C#/Db", "D") graph.add_edge("D", "D#/Eb") graph.add_edge("D#/Eb", "E") graph.add_edge("E", "F") graph.add_edge("F", "F#/Gb") graph.add_edge("F#/Gb", "G") graph.add_edge("G", "G#/Ab")
def earliest_ancestor(ancestors, starting_node): """ 10 / 1 2 4 11 \ / / \ / 3 5 8 \ / \ \ 6 7 9 Write a function that, given the dataset and the ID of an individual in the dataset, returns their earliest known ancestor – the one at the farthest distance from the input individual. If there is more than one ancestor tied for "earliest", return the one with the lowest numeric ID. If the input individual has no parents, the function should return -1. """ # UPER # use bft # use len(longest) # if len(longest) == 1 then return -1 # check the length of the set of vertices for the longest # if there is no child return -1 # while there is an edge: # ancestor[0] is parent ancestor[1] is child # starting node has no parent then return -1 # initialize with the starting node is equal to the child # after that the child becomes the parent # graph = Graph() # # relatives is a node # for relatives in ancestors: # for relative in relatives: # graph.add_vertex(relative) # # print('GRAPHXXXXXX',graph.vertices) # for relatives in ancestors: # graph.add_edge(relatives[1],relatives[0]) # print('GRAPHXXXXXX',graph.vertices) graph = Graph() for pair in ancestors: graph.add_vertex(pair[0]) graph.add_vertex(pair[1]) graph.add_edge(pair[1], pair[0]) print('XXXXXVERTICESXXXX', graph.vertices) q = Queue() q.enqueue([starting_node]) max_path_len = 1 earliest_ancestor = -1 while q.size() > 0: path = q.dequeue() v = path[-1] # if path is longer or equal and value is smaller, or path is longer if (len(path) >= max_path_len and v < earliest_ancestor) or (len(path) > max_path_len): earliest_ancestor = v max_path_len = len(path) for neighbor in graph.vertices[v]: path_copy = list(path) path_copy.append(neighbor) q.enqueue(path_copy) return earliest_ancestor
world.print_rooms() player = Player(world.starting_room) # Fill this out with directions to walk # traversal_path = ['n', 'n'] traversal_path = [] room_graph = {} for i in range(len(origin_room_graph)): room_graph[i] = origin_room_graph[i] #Created Graph Object for DFS Method gg = Graph() for i in room_graph: gg.add_vertex(i) for i in room_graph: for key in room_graph[i][1]: gg.add_edge(i, room_graph[i][1][key]) # copy_of_origin_room_graph = origin_room_graph # for key in copy_of_origin_room_graph: # copy_of_origin_room_graph[key][1]) # print(origin_room_graph[1][1]) def get_neighbors(room): neighbors = [] if room.n_to is not None: neighbors.append(room.n_to.id)
while q.size: path = q.dequeue() curr_room = path[-1][0] if "?" in gr.rooms[curr_room].values(): return [d for _, d in path][1:] elif curr_room not in visited: visited.add(curr_room) for direction in curr_room.get_exits(): next_room = curr_room.get_room_in_direction(direction) q.enqueue(path + [(next_room, direction)]) return None gr = Graph() for room in world.rooms.values(): gr.add_vertex(room) while True: if not any("?" in d.values() for d in gr.rooms.values()): break linear_dir = gr.go_in_direction_until_dead_end(player.current_room) get_current_room(linear_dir) traversal_path += linear_dir path_to_unexplored_room = find_unexplored_room() if path_to_unexplored_room is not None: traversal_path += path_to_unexplored_room get_current_room(path_to_unexplored_room) # TRAVERSAL TEST visited_rooms = set() player.current_room = world.starting_room
world.load_graph(room_graph) # Print an ASCII map world.print_rooms() player = Player(world.starting_room) # Fill this out with directions to walk # traversal_path = ['n', 'n'] traversal_path = [] prev_room = player.current_room.id current_room = player.current_room.id room_dirs = world.rooms[current_room].get_exits() graph = Graph() graph.add_vertex(player.current_room.id, room_dirs) visited = set() visited.add(current_room) # dirs = ["n","s","e","w"] # prev_dir = None # ustack = Stack() # ustack.push(current_room) while len(visited) < len(world.rooms): # if len(traversal_path)> 2000: # print('nope') # break while "?" in graph.vertices[current_room].values():