def test_DAG_boundary(self): with self.assertRaises( Exception, msg= "the number of edges of connected graph must more than the number of nodes - 1" ): Graph.DAG(8, 6) Graph.DAG(8, 7)
def gen_bad_cases(self, length): n = length # 点数 m = length # 边数 稀疏图 graph = Graph.DAG(n, m) self.output(length) self.output(self.__graph2list(graph)) self.output(0) self.output(length - 1)
def gen_random_cases(self, length): n = length m = length * min(1, int(length/2)) graph = Graph.DAG(n, m) self.output(length) self.output(self.__graph2list(graph)) self.output(random.randint(0, length - 1)) self.output(random.randint(0, length - 1))
def test_DAG(self): graph_size = 20 for _ in range(10): # test 10 times ufs = UnionFindSet(graph_size) graph = Graph.DAG(graph_size, int(graph_size * 1.6), repeated_edges=False, self_loop=False, loop=True) self.assertEqual(len(list(graph.iterate_edges())), int(graph_size * 1.6)) for edge in graph.iterate_edges(): ufs.merge(edge.start, edge.end) for i in range(graph_size - 1): self.assertTrue(ufs.test_same(i + 1, i + 2))