Esempio n. 1
0
 def test_load_graph_with_weights(self):
     # act
     graph = load_graph(self.dijkstras, True)
     vertex = graph.get_vertex(0)
     # assert
     self.assertIsNotNone(graph)
     self.assertIsNotNone(vertex.weights)
Esempio n. 2
0
 def test_topological_sort(self):
     # arrange
     graph = load_graph(self.topological_graph_file, False)
     # act
     result = topological_sort(graph)
     self.assertEqual(9, len(result))
     print(result)
Esempio n. 3
0
 def test_dijkstras_shortest_path(self):
     # act
     graph = load_graph(self.dijkstras, True)
     result = single_path(graph, 1, 3)
     # assert
     self.assertIsNotNone(graph)
     self.assertIsNotNone(result)
     self.assertEqual(9, result[0])
Esempio n. 4
0
 def test_get_shortest_path(self):
     # arrange
     graph = load_graph(self.undirected_cyclic_graph_file, False)
     # act
     path = get_shortest_path(graph, 0, 6)
     # assert
     self.assertIsNotNone(path)
     self.assertEqual(len(path), 4)
     self.assertEqual(path[2], graph.get_vertex('3'))
Esempio n. 5
0
 def test_detect_cycle(self):
     # arrange
     graph = load_graph(self.directed_cyclic_graph_file, False)
     # act
     result1 = dfs_detect_cycle(graph, 1)
     result2 = dfs_detect_cycle(graph, 5)
     # assert
     self.assertTrue(result1)
     self.assertFalse(result2)
Esempio n. 6
0
 def test_load_directed_cyclic_graph(self):
     # act
     graph = load_graph(self.directed_cyclic_graph_file, True)
     vertex = graph.get_vertex(0)
     # assert
     self.assertIsNotNone(graph)
     self.assertTrue(graph.cyclic)
     self.assertFalse(graph.directed)
     self.assertEqual(len(graph.vertexes), 6)
     self.assertEqual(len(vertex.adj), 2)
Esempio n. 7
0
 def test_bfs_undirected_cyclic(self):
     # arrange
     graph = load_graph(self.undirected_cyclic_graph_file, False)
     # act
     bfs = bfs_undirected_cyclic(graph, 0)
     # assert
     self.assertIsNotNone(bfs)
     self.assertIsNotNone(bfs[0])
     self.assertEqual(len(bfs[0]), 8)
     self.assertEqual(bfs[0][graph.get_vertex('7')], 3)
     self.assertIsNotNone(bfs[1])
     self.assertEqual(bfs[1][graph.get_vertex('4')], graph.get_vertex('3'))
Esempio n. 8
0
    def test_dfs_directed_cyclic(self):
        # arrange
        graph = load_graph(self.directed_cyclic_graph_file, False)
        # act
        # dfs = dfs_undirected_cyclic_b(graph, 1)
        dfs = bfs_undirected_cyclic(graph, 1)

        # assert
        levels = dfs[0]
        relations = dfs[1]
        self.assertIsNotNone(dfs)
        self.assertIsNotNone(levels)
        self.assertEqual(len(levels), 4)
        self.assertTrue(graph.get_vertex(0) not in levels)
        self.assertIsNotNone(relations)
        self.assertEqual(len(relations), 4)
        self.assertTrue(graph.get_vertex(0) not in relations)
Esempio n. 9
0
 def test_get_graph_sources(self):
     # arrange
     graph = load_graph(self.topological_graph_file, False)
     # act
     result = topological_sort_get_sources(graph)
     self.assertEqual(4, len(result))