Exemple #1
0
 def test_is_acyclic(self):
     def test_case(label: str, case, ans: bool):
         algo = GraphAlgo()
         g_lis = Graph.make(type=PHY_ADJ_LIS, edges=case)
         g_mat = Graph.make(type=PHY_MAT, edges=case)
         assert ans == algo.is_acyclic(g_lis), f"fail, label:{label}"
         assert ans == algo.is_acyclic(g_mat), f"fail, label:{label}"
Exemple #2
0
    def test_city(self):
        g1 = Graph.make(PHY_MAT, Citys)
        g2 = Graph.make(PHY_ADJ_LIS, Citys)

        g1.get_storage().print_status()
        print("-" * 32)
        g2.get_storage().print_status()
Exemple #3
0
    def test_usage(self):
        lis = GRAPH_CASES["tree"]
        _ = Graph().set_type(PHY_ADJ_LIS).from_list(lis)
        _ = Graph.make(PHY_ADJ_LIS, lis)

        _ = Graph().set_type(PHY_MAT).from_list(lis)
        _ = Graph.make(PHY_MAT, lis)
Exemple #4
0
    def test_graph_storage(self):
        lis = GRAPH_CASES["tree"]
        g = Graph.make(PHY_ADJ_LIS, lis)
        sto = g.get_storage()
        assert isinstance(sto, WeightedAdjLis)

        g2 = Graph.make(PHY_MAT, lis)
        sto2 = g2.get_storage()
        assert isinstance(sto2, AdjMat)
Exemple #5
0
    def test_scc(self):
        g1 = Graph.make(PHY_ADJ_LIS, GRAPH_CASES["tree"])
        res = GraphAlgo().scc(g1)
        print(res)

        g2 = Graph.make(PHY_ADJ_LIS, GRAPH_CASES["cyclic"])
        res = GraphAlgo().scc(g2)
        print(res)

        g3 = Graph.make(PHY_ADJ_LIS, GRAPH_CASES["4-complete"])
        res = GraphAlgo().scc(g3)
        print(res)
        def test_edge(label: str, case):
            cmp_closure = lambda x: (x[0], x[1], x[2])
            correct = case
            correct.sort(key=cmp_closure)

            g = Graph.make(type=PHY_ADJ_LIS, edges=case)
            output = g.get_storage().edges()
            output.sort(key=cmp_closure)
            assert output == correct, f"Error, label={label}, \noutput={output}\ncorrect={correct}"
        def test_node(label: str, case):
            correct = set()
            for u, v, _ in case:
                correct.add(u)
                correct.add(v)
            correct = [x for x in correct]
            correct.sort()

            g = Graph.make(type=PHY_ADJ_LIS, edges=case)
            output = g.get_storage().nodes()
            output.sort()
            assert output == correct, f"Error, label={label}, output={output}"
Exemple #8
0
    def test_topo_sort_recur(self):
        g1 = Graph.make(type=PHY_ADJ_LIS, edges=GRAPH_CASES['tree'])
        g1m = Graph.make(type=PHY_MAT, edges=GRAPH_CASES['tree'])

        algo = GraphAlgo()
        flag, lis = algo.topological_sort_recur(g1)
        assert flag
        flag, lis = algo.topological_sort_recur(g1m)
        assert flag

        g1 = Graph.make(type=PHY_ADJ_LIS, edges=GRAPH_CASES['circle'])
        g1m = Graph.make(type=PHY_MAT, edges=GRAPH_CASES['circle'])
        flag, lis = algo.topological_sort_recur(g1)
        assert not flag
        flag, lis = algo.topological_sort_recur(g1m)
        assert not flag

        g1 = Graph.make(type=PHY_ADJ_LIS, edges=GRAPH_CASES['dag'])
        g1m = Graph.make(type=PHY_MAT, edges=GRAPH_CASES['dag'])
        flag, lis = algo.topological_sort_recur(g1)
        assert flag
        # print(lis)
        flag, lis = algo.topological_sort_recur(g1m)
        assert flag
 def test_trans(src, tar, type):
     trans = Graph.make(PHY_ADJ_LIS, src)\
                 .get_storage()\
                 .transposition()
     cmp = Graph.make(type, tar)
     assert cmp.get_storage() == trans