예제 #1
0
    def test_contract(self) -> None:
        g = Graph([(1, 2), (1, 3), (2, 3), (2, 4), (2, 2)])
        assert g[1].adj_vertices() == {
            g[2],
            g[3],
        }, "vertex 1 should be adjacent to vertices 2 and 3"

        g.get_edge(1, 2).contract(remove_loops=False)

        assert g[1].adj_vertices() == {
            g[1],
            g[3],
            g[4],
        }, "after edge contraction, vertex 1 should be adjacent to vertices 1, 3, and 4"
        assert g[1].loop_edge is not None
        assert (
            g[1].loop_edge.label == "(1, 1)"
        ), "vertex 1 should have loop edge (1, 1), due to loop that was on vertex 2"
        assert not g.has_vertex(
            2), "after edge contraction, vertex 2 should be removed"

        g2 = Graph([("a", "b"), ("b", "c"), ("b", "b")])
        g2.get_edge("a", "b").contract(remove_loops=True)
        assert not g2[
            "a"].loop_edge, "loop edge should be removed after edge contraction"
예제 #2
0
    def test_remove(self) -> None:
        g = Graph([(1, 2), (2, 3)])
        assert g.has_edge(1,
                          2), "prior to removal, graph should have edge (1, 2)"
        assert not g[1].is_isolated(
        ), "vertex 1 should not be isolated prior to edge removal"

        g.get_edge(1, 2).remove(remove_semi_isolated_vertices=False)

        assert not g.has_edge(
            1, 2), "after removal, graph should not have edge (1, 2)"
        assert g.has_vertex(
            1
        ), "after edge removal, isolated vertex 1 should still be in graph"
        assert g[1].is_isolated(
        ), "vertex 1 should be isolated after edge removal"

        g2 = Graph([(1, 2), (1, 1), (2, 3)])
        g2.get_edge(1, 2).remove(remove_semi_isolated_vertices=True)
        assert not g2.has_vertex(
            1
        ), "after edge removal, semi-isolated vertex 1 should have been removed"