def test_bfs_check_order(self): g = Graph([[0, 1], [0, 2], [1, 3], [1, 4], [2, 5], [2, 6]]) breadth_first_search(g, 0) self.assertTrue( g.get_node(0).visited < min(self.__get_visited_values(g, [1, 2]))) self.assertTrue( g.get_node(1).visited < min( self.__get_visited_values(g, [3, 4, 5, 6]))) self.assertTrue( g.get_node(2).visited < min( self.__get_visited_values(g, [3, 4, 5, 6])))
def test_dfs_disconnected(self): g = Graph([[0, 1], [2, 3]]) depth_first_search(g, 0) for i in range(2): self.assertTrue(g.get_node(i).visited) for i in range(2, 4): self.assertFalse(g.get_node(i).visited) depth_first_search(g, 2) for i in range(2, 4): self.assertTrue(g.get_node(i).visited)
def test_dfs_disconnected(self): g = Graph([[0,1],[2,3]]) depth_first_search(g, 0) for i in range(2): self.assertTrue(g.get_node(i).visited) for i in range(2,4): self.assertFalse(g.get_node(i).visited) depth_first_search(g, 2) for i in range(2,4): self.assertTrue(g.get_node(i).visited)
def test_connected_components_disconnected(self): graph = Graph([[0, 1], [1, 2], [2, 0], [3, 4], [3, 5], [3, 6], [7, 8]]) result = connected_components(graph) self.assertEqual(len(result), 3) self.assertTrue(result[0].id in [0, 1, 2]) self.assertTrue(result[1].id in [3, 4, 5, 6]) self.assertTrue(result[2].id in [7, 8])
def test_1(self): graph = Graph([[0, 1], [0, 2], [1, 3], [2, 3]], True) find_topological_order(graph) self.check_labels(graph, 0, 1) self.check_labels(graph, 0, 2) self.check_labels(graph, 1, 3) self.check_labels(graph, 2, 3)
def test_bfs_simple_connected(self): g = Graph([[0, 1], [1, 2], [2, 0]]) breadth_first_search(g, 0) for i in range(3): self.assertTrue(g.get_node(i).visited)
def test_dfs_self_edges(self): g = Graph([[0, 0], [0, 1], [1, 1], [2, 2], [2, 0]]) depth_first_search(g, 0) for i in range(3): self.assertTrue(g.get_node(i).visited)
def test_bfs_parallel_edges(self): g = Graph([[0, 1], [1, 2], [1, 2], [2, 1], [0, 1], [0, 2]]) breadth_first_search(g, 0) for i in range(3): self.assertTrue(g.get_node(i).visited)
def test_bfs_check_order(self): g = Graph([[0,1], [0,2], [1,3], [1,4], [2,5], [2,6]]) breadth_first_search(g, 0) self.assertTrue(g.get_node(0).visited < min(self.__get_visited_values(g, [1,2]))) self.assertTrue(g.get_node(1).visited < min(self.__get_visited_values(g, [3,4,5,6]))) self.assertTrue(g.get_node(2).visited < min(self.__get_visited_values(g, [3,4,5,6])))
def __init__(self, *args, **kwargs): super(TestGraphSearch, self).__init__(*args, **kwargs) # ----------------------------- # ----- Graph 1: Directed ----- # ----------------------------- """ graph1 = { '0': ['1', '2'], '1': ['2'], '2': ['0', '3'], '3': ['3'], } """ self.graph1 = Graph(directed=True) self.graph1.add_edge(0, 1) self.graph1.add_edge(0, 2) self.graph1.add_edge(1, 2) self.graph1.add_edge(2, 0) self.graph1.add_edge(2, 3) self.graph1.add_edge(3, 3) # ------------------------------- # ----- Graph 2: Undirected ----- # ------------------------------- """ graph2 = { '1': ['2', '5', '6'], '2': ['1', '3', '5'], '3': ['2', '4'], '4': ['3', '5'], '5': ['1', '2', '4'], '6': ['1'], } """ self.graph2 = Graph(directed=False) self.graph2.add_edge(1, 2) self.graph2.add_edge(1, 5) self.graph2.add_edge(1, 6) self.graph2.add_edge(2, 3) self.graph2.add_edge(2, 5) self.graph2.add_edge(3, 4) self.graph2.add_edge(4, 5) # ----------------------------------------- # ----- Graph 3: Undirected Bipartite ----- # ----------------------------------------- """ graph3 = { '1': ['2', '5', '6'], '2': ['1', '3', '4'], '3': ['2', '4'], '4': ['3', '5'], '5': ['1', '3', '4'], '6': ['1'], } """ self.graph3 = Graph(directed=False) self.graph3.add_edge(1, 2) self.graph3.add_edge(1, 5) self.graph3.add_edge(1, 6) self.graph3.add_edge(2, 3) self.graph3.add_edge(2, 4) self.graph3.add_edge(3, 5) self.graph3.add_edge(4, 5)
p_queue[vert] = new_distance dist_to_start[vert] = new_distance prev_vert[vert] = current return prev_vert # Convert the string to integers rows = string1.split('\n')[1:] for i in range(len(rows)): rows[i] = rows[i].split(' ') for j in range(len(rows[i])): rows[i][j] = int(rows[i][j]) # Create a graph that represents the integers graph = Graph() for i in range(len(rows)-1, -1, -1): # for each row in the pyramid for j in range(len(rows[i])): # for each item in the row graph.add_vert(str(i) + str(j)) # Initalize edges as the inverse of the node value (i.e. 70 --> 1/70) for i in range(len(rows)-1, 0, -1): for j in range(len(rows[i])): if j == 0: graph.add_edge(str(i)+str(j), str(i-1)+str(j), 1/float((rows[i-1][j]))) elif j == len(rows[i])-1: graph.add_edge(str(i)+str(j), str(i-1)+str(j-1), 1/float(rows[i-1][j-1])) else: graph.add_edge(str(i)+str(j), str(i-1)+str(j), 1/float(rows[i-1][j])) graph.add_edge(str(i)+str(j), str(i-1)+str(j-1), 1/float(rows[i-1][j-1]))
def __init__(self, l, is_directed=False): Graph.__init__(self, l, is_directed) self.t = 0 self.s = None
def test_dfs_check_order(self): g = Graph([[0,1], [0,2], [1,3], [1,4], [2,5], [2,6]]) depth_first_search(g, 0) if g.get_node(1).visited < g.get_node(2).visited: self.assertTrue(g.get_node(3).visited < g.get_node(2).visited) self.assertTrue(g.get_node(4).visited < g.get_node(2).visited) else: self.assertTrue(g.get_node(5).visited < g.get_node(1).visited) self.assertTrue(g.get_node(6).visited < g.get_node(1).visited) self.assertTrue(g.get_node(0).visited < g.get_node(1).visited) self.assertTrue(g.get_node(0).visited < g.get_node(2).visited) self.assertTrue(g.get_node(1).visited < g.get_node(3).visited) self.assertTrue(g.get_node(1).visited < g.get_node(4).visited) self.assertTrue(g.get_node(2).visited < g.get_node(5).visited) self.assertTrue(g.get_node(2).visited < g.get_node(6).visited)
def test_bfs_simple_connected(self): g = Graph([[0,1],[1,2],[2,0]]) breadth_first_search(g, 0) for i in range(3): self.assertTrue(g.get_node(i).visited)
def test_dfs_self_edges(self): g = Graph([[0,0],[0,1],[1,1],[2,2],[2,0]]) depth_first_search(g, 0) for i in range(3): self.assertTrue(g.get_node(i).visited)
def test_dfs_check_order(self): g = Graph([[0, 1], [0, 2], [1, 3], [1, 4], [2, 5], [2, 6]]) depth_first_search(g, 0) if g.get_node(1).visited < g.get_node(2).visited: self.assertTrue(g.get_node(3).visited < g.get_node(2).visited) self.assertTrue(g.get_node(4).visited < g.get_node(2).visited) else: self.assertTrue(g.get_node(5).visited < g.get_node(1).visited) self.assertTrue(g.get_node(6).visited < g.get_node(1).visited) self.assertTrue(g.get_node(0).visited < g.get_node(1).visited) self.assertTrue(g.get_node(0).visited < g.get_node(2).visited) self.assertTrue(g.get_node(1).visited < g.get_node(3).visited) self.assertTrue(g.get_node(1).visited < g.get_node(4).visited) self.assertTrue(g.get_node(2).visited < g.get_node(5).visited) self.assertTrue(g.get_node(2).visited < g.get_node(6).visited)
class TestGraphSearch(unittest.TestCase): def __init__(self, *args, **kwargs): super(TestGraphSearch, self).__init__(*args, **kwargs) # ----------------------------- # ----- Graph 1: Directed ----- # ----------------------------- """ graph1 = { '0': ['1', '2'], '1': ['2'], '2': ['0', '3'], '3': ['3'], } """ self.graph1 = Graph(directed=True) self.graph1.add_edge(0, 1) self.graph1.add_edge(0, 2) self.graph1.add_edge(1, 2) self.graph1.add_edge(2, 0) self.graph1.add_edge(2, 3) self.graph1.add_edge(3, 3) # ------------------------------- # ----- Graph 2: Undirected ----- # ------------------------------- """ graph2 = { '1': ['2', '5', '6'], '2': ['1', '3', '5'], '3': ['2', '4'], '4': ['3', '5'], '5': ['1', '2', '4'], '6': ['1'], } """ self.graph2 = Graph(directed=False) self.graph2.add_edge(1, 2) self.graph2.add_edge(1, 5) self.graph2.add_edge(1, 6) self.graph2.add_edge(2, 3) self.graph2.add_edge(2, 5) self.graph2.add_edge(3, 4) self.graph2.add_edge(4, 5) # ----------------------------------------- # ----- Graph 3: Undirected Bipartite ----- # ----------------------------------------- """ graph3 = { '1': ['2', '5', '6'], '2': ['1', '3', '4'], '3': ['2', '4'], '4': ['3', '5'], '5': ['1', '3', '4'], '6': ['1'], } """ self.graph3 = Graph(directed=False) self.graph3.add_edge(1, 2) self.graph3.add_edge(1, 5) self.graph3.add_edge(1, 6) self.graph3.add_edge(2, 3) self.graph3.add_edge(2, 4) self.graph3.add_edge(3, 5) self.graph3.add_edge(4, 5) def test_bipartite(self): false_result = self.graph2.is_bipartite() true__result = self.graph3.is_bipartite() self.assertFalse(false_result) self.assertTrue(true__result) def test_dfs(self): start = 6 end = 4 obtained = self.graph2.dfs(start) actual1 = [6, 1, 5, 4, 3, 2] actual2 = [6, 1, 2, 3, 4, 5] self.assertTrue(obtained == actual1 or obtained == actual2) def test_bfs(self): start = 1 end = 3 order, _ = self.graph1.bfs(start) actual = [1, 2, 0, 3] self.assertEqual(actual, order) start = 6 end = 4 order, _ = self.graph2.bfs(start) actual = [6, 1, 2, 5, 3, 4] self.assertEqual(actual, order) def test_shortest_path(self): start = 1 end = 3 _, parents = self.graph1.bfs(start) shortest_path = self.graph1.find_path(start, end, parents, path=[]) actual = [1, 2, 3] self.assertEqual(actual, shortest_path) start = 6 end = 4 shortest_path = self.graph2.find_path(start, end, parents=None, path=[]) actual = [6, 1, 5, 4] self.assertEqual(actual, shortest_path)
def test_connected_components_fully_connected(self): graph = Graph([[0, 1], [1, 2], [2, 0]]) result = connected_components(graph) self.assertEqual(len(result), 1) self.assertTrue(result[0].id in [0, 1, 2])
def test_bfs_parallel_edges(self): g = Graph([[0,1],[1,2],[1,2],[2,1],[0,1],[0,2]]) breadth_first_search(g, 0) for i in range(3): self.assertTrue(g.get_node(i).visited)