Beispiel #1
0
 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)
Beispiel #2
0
 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)
Beispiel #3
0
    def test_repeated_edges(self):
        graph_size = 20
        for _ in range(20):
            graph = Graph.graph(graph_size,
                                int(graph_size * 2),
                                repeated_edges=True)
            edges = [(e.start, e.end) for e in graph.iterate_edges()]
            has_repeated_edges = len(edges) > len(set(edges))
            if has_repeated_edges:
                break
        self.assertTrue(has_repeated_edges)

        for _ in range(10):
            graph = Graph.graph(graph_size,
                                int(graph_size * 2),
                                repeated_edges=False)
            edges = list(graph.iterate_edges())
            self.assertEqual(len(edges), len(set(edges)))
Beispiel #4
0
    def test_self_loop(self):
        graph_size = 20
        for _ in range(20):
            graph = Graph.graph(graph_size,
                                int(graph_size * 2),
                                self_loop=True)
            has_self_loop = max(
                [e.start == e.end for e in graph.iterate_edges()])
            if has_self_loop:
                break
        self.assertTrue(has_self_loop)

        for _ in range(10):
            graph = Graph.graph(graph_size,
                                int(graph_size * 2),
                                self_loop=False)
            self.assertFalse(
                max([e.start == e.end for e in graph.iterate_edges()]))
Beispiel #5
0
 def test_tree_connected(self):
     graph_size = 20
     for _ in range(20):
         ufs = UnionFindSet(graph_size)
         tree = Graph.tree(graph_size)
         for edge in tree.iterate_edges():
             ufs.merge(edge.start, edge.end)
         for i in range(graph_size - 1):
             self.assertTrue(ufs.test_same(i + 1, i + 2))
Beispiel #6
0
    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))
Beispiel #7
0
    def test_undirected_graph(self):
        graph_size = 20
        for _ in range(10):  # test 10 times
            ufs = UnionFindSet(graph_size)
            graph = Graph.UDAG(graph_size,
                               int(graph_size * 1.6),
                               repeated_edges=False,
                               self_loop=False)

            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))
Beispiel #8
0
    def test_DAG_without_loop(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=False)

            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))

            belong = tarjan(graph, graph_size)
            self.assertEqual(max(belong), graph_size)