def test_shortest_path_not_dest_node(self): g = Graph() g.add_vertice("from") try: shortest_path(g, "from", "to") self.fail("Should not be able to find a path in non-existent node") except ValueError as e: self.assertEqual("Vertice not found in graph", str(e))
def test_complex_graph_shortest_path(self): g = Graph() v00 = "(0, 0)" v01 = "(0, 1)" v02 = "(0, 2)" v10 = "(1, 0)" v11 = "(1, 1)" v12 = "(1, 2)" v20 = "(2, 0)" v21 = "(2, 1)" v22 = "(2, 2)" g.add_vertice(v00) g.add_vertice(v01) g.add_vertice(v02) g.add_vertice(v10) g.add_vertice(v11) g.add_vertice(v12) g.add_vertice(v20) g.add_vertice(v21) g.add_vertice(v22) # o - o - o # | # o o o # | | | # o - o - o g.add_edge(v00, v01) g.add_edge(v01, v02) g.add_edge(v01, v11) g.add_edge(v10, v20) g.add_edge(v11, v21) g.add_edge(v12, v22) g.add_edge(v20, v21) g.add_edge(v21, v22) self.assertCountEqual([v00, v01, v11, v21, v22, v12], shortest_path(g, v00, v12)) self.assertCountEqual([v00, v01, v02], shortest_path(g, v00, v02)) self.assertCountEqual([v10, v20, v21, v11, v01, v00], shortest_path(g, v10, v00)) self.assertCountEqual([v21, v11, v01, v02], shortest_path(g, v21, v02)) self.assertCountEqual([v12, v22, v21, v20, v10], shortest_path(g, v12, v10))
def test_shortest_path_one_path(self): g = Graph() g.add_vertice("one") g.add_vertice("two") g.add_edge("one", "two") self.assertCountEqual(["one", "two"], shortest_path(g, "one", "two"))
def solution_path(self): """Returns the path that represents the "correct" steps needed to move from the start to the end of the maze. Returns: list(str): A list of edges in string format that represent the correct path from start to finish""" return shortest_path(self.__graph, str(self.__starting_pos), str(self.__end_pos))
def test_shortest_path_forked_path(self): g = Graph() v00 = "(0, 0)" v01 = "(0, 1)" v10 = "(1, 0)" v11 = "(1, 1)" g.add_vertice(v00) g.add_vertice(v01) g.add_vertice(v10) g.add_vertice(v11) g.add_edge(v00, v01) g.add_edge(v00, v10) g.add_edge(v10, v11) self.assertCountEqual([v00, v10, v11], shortest_path(g, v00, v11)) self.assertCountEqual([], shortest_path(g, v00, v00)) self.assertCountEqual([v01, v00, v10, v11], shortest_path(g, v01, v11))
def __closest_cell_helper(self, row, col): """Helper function that returns two useful pieces of data with regards to the closest cell: The closest cell, as well as how far that given cell is from the solution cell Args: row(int): the row of the given cell col(int): the col of the given cell Returns: tuple: A tuple representing (cell, distance), where cell is the name of the closest cell in the solution path, and distance is how far the given cell is from that cell Raises: IndexError: If no such given cell position exists in the maze""" original_cell = cell_format.format(row, col) if not self.__graph.contains_vertice(original_cell): raise ValueError("Given invalid position") solution_mapping = set(self.solution_path()) visited = set() work_list = [cell_format.format(row, col)] while work_list: current_vertice = work_list.pop(0) visited.add(current_vertice) if current_vertice in solution_mapping: distance_from_path = len( shortest_path(self.__graph, original_cell, current_vertice)) if distance_from_path: # removes the from node to prevent over counting return current_vertice, distance_from_path - 1 else: return current_vertice, 0 for neighbor in self.__graph.neighbors(current_vertice): if neighbor not in visited: work_list.append(neighbor) return "", -1
def test_shortest_path_empty_case(self): g = Graph() g.add_vertice("one") g.add_vertice("two") self.assertEqual([], shortest_path(g, "one", "two"))