def breadth_first_search(self): """ Breadth-first search. @rtype: list @return: Generated spanning_tree """ return searching.breadth_first_search(self)
def breadth_first_search(self): """ Breadth-first search. @rtype: dictionary @return: Generated spanning_tree """ return searching.breadth_first_search(self)
def main(): st = time.perf_counter() #Start a time counter. if len(sys.argv) == 4: #If the length of the keyword arguments is four... method = sys.argv[ 1] #The second argument is the method/algorithm used to find a solution. input_file = sys.argv[ 2] #The third argument is a .txt file containing the initial and final state of the problem. output_file = sys.argv[ 3] #The fourth argument is a .txt file containing the solution of the problem. initial_state, goal_state = read_input_file( filename=input_file ) #Read the input file and return two state objects. if method == 'breadth': #Check which method is selected and solve the problem accordingly. solution = breadth_first_search(current_state=initial_state, goal_state=goal_state, timeout=300) elif method == 'depth': solution = depth_first_search(current_state=initial_state, goal_state=goal_state, timeout=300) elif method == 'best': solution = heuristic_search(current_state=initial_state, goal_state=goal_state, method='best', timeout=300) elif method == 'astar': solution = heuristic_search(current_state=initial_state, goal_state=goal_state, method='astar', timeout=300) else: #If the method argument is none of the above, print a usage message. solution = None print( 'Usage: python bw.py <method> <input filename> <output filename>' ) if solution == goal_state: #If the solution is equal to the goal state... number_of_moves = write_output_file( solution=solution, filename=output_file ) #Write the solution file and return the number of moves. print('Solution found!') print('Number of blocks:', len(initial_state.layout.keys())) print('Method:', method) print('Number of moves:', number_of_moves) print('Execution time:', str(round(time.perf_counter() - st, 4))) else: #Else, if the length of the keyword arguments is not equal to four, print a usage message. print( 'Usage: python bw.py <method> <input filename> <output filename>')
def breadth_first_search(self, root=None): """ Breadth-first search. @type root: node @param root: Optional root node (will explore only root's connected component) @rtype: dictionary @return: A tuple containing a dictionary and a list. 1. Generated spanning tree 2. Graph's level-based ordering """ return searching.breadth_first_search(self, root)
def test_bfs(self): """Test and visualize breadth-first search""" start = 'a' goal = 'u' node_positions = { n: self.romania.node[n]['pos'] for n in self.romania.node.keys() } self.romania.reset_search() path = breadth_first_search(self.romania, start, goal) self.draw_graph(self.romania, node_positions=node_positions, start=start, goal=goal, path=path)