class DFSGraphTest(unittest.TestCase): def setUp(self): self.g = Graph(5) self.g.add_edge(0, 1, 4) self.g.add_edge(0, 2, 1) self.g.add_edge(2, 1, 2) self.g.add_edge(1, 3, 1) self.g.add_edge(2, 3, 5) self.g.add_edge(3, 4, 3) def test_dfs_00(self): self.assertEqual(dfs_graph.dfs_recursive(self.g), self.g.size) def test_dfs_01(self): self.assertEqual(dfs_graph.dfs_iterative(self.g), self.g.size)
class TopSortTest(unittest.TestCase): def setUp(self): self.g = Graph(5) self.g.add_edge(0, 1, 4) self.g.add_edge(0, 2, 1) self.g.add_edge(2, 1, 2) self.g.add_edge(1, 3, 1) self.g.add_edge(2, 3, 5) self.g.add_edge(3, 4, 3) def test_dijkstra_00(self): dist, prev = dijkstra.dijkstra(self.g, 0, 4) self.assertEqual(dist, 7) self.assertEqual(prev, [None, 2, 0, 1, 3]) def test_dijkstra_01(self): path = dijkstra.reconstruct_shortest_path(self.g, 0, 4) self.assertEqual(path, [0, 2, 1, 3, 4])