예제 #1
0
    def test_edmonds_undirected_graph(self) -> None:
        g = Graph([("s", "t", 10), ("s", "y", 5), ("t", "y", 2)])

        # Edmonds' algorithm does not work on undirected graphs.
        with pytest.raises(exception.GraphTypeNotSupported):
            for _ in directed.edmonds(g):
                pass
예제 #2
0
    def test_edmonds_unfeasible_spanning_arborescence(self) -> None:
        """Test to ensure that Unfeasible raised if a digraph does not contain a spanning
        arborescence."""
        g = DiGraph([("a", "b", 1), ("c", "d", 3)])

        with pytest.raises(exception.Unfeasible):
            _ = next(directed.edmonds(g, find_spanning_arborescence=True))
예제 #3
0
    def test_edmonds_empty_graph(self) -> None:
        g = DiGraph()

        # Edmonds' algorithm does not work on empty graphs.
        with pytest.raises(exception.Unfeasible):
            for _ in directed.edmonds(g):
                pass
예제 #4
0
    def test_edmonds_min_arborescence_multigraph(self) -> None:
        g = MultiDiGraph([
            ("a", "b", 1),
            ("a", "b", 2),
            ("b", "c", 3),
            ("b", "c", 6),
            ("c", "d", 5),
            ("c", "d", 10),
            ("d", "a", 2),
            ("d", "a", 4),
        ])
        spanning_edges_min_arborescence = {("d", "a"), ("a", "b"), ("b", "c")}

        arborescence = next(
            directed.edmonds(g, minimum=True, find_spanning_arborescence=True))

        for edge_label in spanning_edges_min_arborescence:
            assert edge_label in arborescence

        assert len(arborescence.edges()
                   ) == 3, "min spanning arborescence should have 3 edges"

        weight = 0.0
        for edge in spanning_edges_min_arborescence:
            weight += min(
                c.weight
                for c in arborescence.get_edge(edge[0], edge[1]).connections())
        assert weight == 6
예제 #5
0
    def test_edmonds_min_directed_forest(self) -> None:
        g = DiGraph(test_edges_multi_tree)
        main_arborescence = {("j", "g"), ("g", "h"), ("h", "i"), ("g", "c")}

        count = 0
        weight = 0.0
        for arborescence in directed.edmonds(g,
                                             minimum=True,
                                             find_spanning_arborescence=False):
            count += 1
            weight += arborescence.weight
            if len(arborescence) > 2:
                for edge_label in main_arborescence:
                    assert edge_label in arborescence

        assert count == 5, "min branching should have 5 arborescences"
        assert weight == -24
예제 #6
0
    def test_edmonds_max_directed_forest(self) -> None:
        g = DiGraph(test_edges_multi_tree)
        main_arborescence = {("d", "a"), ("a", "b"), ("b", "c"), ("c", "f")}

        count = 0
        weight = 0.0
        for arborescence in directed.edmonds(g,
                                             minimum=False,
                                             find_spanning_arborescence=False):
            count += 1
            weight += arborescence.weight
            if len(arborescence) > 1:
                for edge_label in main_arborescence:
                    assert edge_label in arborescence

        assert count == 6, "max branching should have 6 arborescences"
        assert weight == 20
예제 #7
0
    def test_edmonds_min_spanning_arborescence(self) -> None:
        g = DiGraph(test_edges_acyclic)
        spanning_edges_min_arborescence = {
            ("a", "e"),
            ("a", "b"),
            ("b", "c"),
            ("g", "h"),
            ("f", "g"),
            ("c", "f"),
            ("c", "d"),
        }
        arborescence = next(
            directed.edmonds(g, minimum=True, find_spanning_arborescence=True))
        for edge_label in spanning_edges_min_arborescence:
            assert edge_label in arborescence

        assert arborescence.weight == 30, "min spanning arborescence weight should be 30"
예제 #8
0
    def test_edmonds_max_spanning_arborescence_cyclic_graph(self) -> None:
        g = DiGraph(test_edges_cyclic)
        spanning_edges_max_arborescence = {
            ("b", "a"),
            ("b", "c"),
            ("b", "g"),
            ("g", "h"),
            ("h", "d"),
            ("d", "e"),
            ("e", "f"),
        }

        arborescence = next(
            directed.edmonds(g, minimum=False,
                             find_spanning_arborescence=True))

        for edge_label in spanning_edges_max_arborescence:
            assert edge_label in arborescence

        assert arborescence.weight == 28, "max spanning arborescence weight should be 28"