Beispiel #1
0
    def test_contract(self) -> None:
        g = MultiGraph([(1, 2), (1, 3), (2, 3), (2, 4), (2, 2), (2, 2)])
        assert g[1].adj_vertices() == {
            g[2],
            g[3],
        }, "vertex 1 should be adjacent to vertices 2 and 3"
        assert (
            g.get_edge(1, 3).multiplicity == 1
        ), "before edge contraction, edge (1, 3) should have multiplicity 1"
        assert g.get_edge(
            2, 2).multiplicity == 2, "vertex 2 should have two loops"

        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.get_edge(1, 3).multiplicity == 2
        ), "after edge contraction, edge (1, 3) should have multiplicity 2"
        assert g[1].loop_edge, "vertex 1 should have loop edge (1, 1)"
        print(f"\nDEBUG: g[1].loop_edge => {g[1].loop_edge}\n")
        assert g[
            1].loop_edge.multiplicity == 3, "loop edge should have multiplicity 3"
        assert not g.has_vertex(
            2), "after edge contraction, vertex 2 should be removed"

        g2 = MultiDiGraph([(1, 2), (2, 4), (2, 2), (2, 2)])

        g2.get_edge(1, 2).contract(remove_loops=True)

        assert not g2[
            1].loop_edge, "loop edge should be removed after edge contraction"
Beispiel #2
0
 def test_issubclass_and_isinstance(self) -> None:
     g = MultiDiGraph()
     v1 = g.add_vertex(1)
     assert isinstance(
         v1, vertex_module.VertexBase
     ), "v1 should be an instance of superclass VertexBase"
     assert isinstance(
         v1, MultiDiVertex), "v1 should be a MultiDiVertex instance"
     assert issubclass(MultiDiVertex, vertex_module.VertexBase
                       ), "MultiDiVertex should be VertexBase subclass"
Beispiel #3
0
 def test_issubclass_and_isinstance(self) -> None:
     g = MultiDiGraph()
     edge: MultiDiEdge = g.add_edge(1, 2)
     assert isinstance(
         edge, edge_module.MultiEdgeBase
     ), "edge should be an instance of superclass MultiEdgeBase"
     assert isinstance(
         edge, MultiDiEdge), "edge should be an MultiDiEdge instance"
     assert issubclass(MultiDiEdge, edge_module.MultiEdgeBase
                       ), "MultiDiEdge should be MultiEdgeBase subclass"
Beispiel #4
0
 def test_repr_str_and_label(self) -> None:
     mdg = MultiDiGraph([(2, 1), (2, 1)])
     assert mdg.get_edge(
         2,
         1).label == "(2, 1)", "directed multiedge label should be (2, 1)"
     assert (mdg.get_edge(2, 1).__str__() == mdg.get_edge(
         2, 1).__repr__()), "edge __repr__ should equal __str__"
     assert (mdg.get_edge(2, 1).__str__() == "(2, 1), (2, 1)"
             ), "__str__ should be '(2, 1), (2, 1)'"
     mdg.add_edge(3, 4, weight=1.5)
     assert mdg.get_edge(
         2,
         1).label == "(2, 1)", "directed multiedge label should be (2, 1)"
     assert (
         mdg.get_edge(2, 1).__str__() == "(2, 1, 1.0), (2, 1, 1.0)"
     ), "edge __str__ should be '(2, 1, 1.0), (2, 1, 1.0)' after adding weighted edge to graph"
Beispiel #5
0
    def test_degree(self) -> None:
        g = MultiDiGraph([(1, 1), (1, 1), (2, 3), (2, 3)])
        assert g[
            1].degree == 4, "vertex with two self loops should have degree 4"
        assert g[
            1].indegree == 2, "vertex with two loops should have indegree 2"
        assert g[
            1].outdegree == 2, "vertex with two loops should have outdegree 2"

        assert g[2].outdegree == 2, "vertex 2 should have outdegree 2"
        assert g[2].indegree == 0, "vertex 2 should have indegree 0"
        assert g[3].indegree == 2, "vertex 3 should have indegree 2"
Beispiel #6
0
    def test_is_vertex_type(self) -> None:
        g = Graph()
        v: Vertex = g.add_vertex(1)
        assert vertex_module.is_vertex_type(
            v), "Vertex object should be a VertexType"

        g2 = DiGraph()
        di_v: DiVertex = g2.add_vertex(1)
        assert vertex_module.is_vertex_type(
            di_v), "DiVertex object should be a VertexType"

        g3 = MultiGraph()
        multi_v: MultiVertex = g3.add_vertex(1)
        assert vertex_module.is_vertex_type(
            multi_v), "MultiVertex object should be a VertexType"

        g4 = MultiDiGraph()
        multi_di_v: MultiDiVertex = g4.add_vertex(1)
        assert vertex_module.is_vertex_type(
            multi_di_v), "MultiDiVertex should be a VertexType"

        assert vertex_module.is_vertex_type(
            10), "int vertex label should be a VertexType"
        assert vertex_module.is_vertex_type(
            "s"), "str vertex label should be a VertexType"
        assert vertex_module.is_vertex_type(("s", {
            "color": "blue"
        })), "vertex tuple should be a VertexType"
        assert not vertex_module.is_vertex_type(
            10.99), "float should not be a VertexType"
        assert not vertex_module.is_vertex_type(
            ("s", "t")), "edge tuple should not be a VertexType"
        assert not vertex_module.is_vertex_type(
            ("s", "t",
             4.5)), "edge tuple with edge weight should not be a VertexType"
        g.add_edge("s", "t")
        assert not vertex_module.is_vertex_type(g.get_edge(
            "s", "t")), "edge object should not be a VertexType"
Beispiel #7
0
    def test_loop_edge(self) -> None:
        g = MultiDiGraph()
        g.add_vertex(0)
        assert not g[0].loop_edge, "vertex 0 should not have a loop edge"

        g.add_edge(1, 1)
        g.add_edge(1, 1)
        assert g[1].incident_edges() == {g.get_edge(
            1, 1)}, "vertex 1 should have self loop as incident edge"
        assert g[1].loop_edge, "vertex 1 should have a self loop"
        assert g[
            1].loop_edge.multiplicity == 2, "vertex 1 should have two loops"
Beispiel #8
0
    def test_incident_edges(self) -> None:
        g = MultiDiGraph([(1, 1), (1, 1), (2, 3), (2, 3)])
        assert g[1].incident_edges() == {g.get_edge(
            1, 1)}, "vertex 1 should be incident on (1, 1)"
        assert g[1].incident_edges_incoming() == {g.get_edge(
            1, 1)}, "incoming edge should be (1, 1)"
        assert g[1].incident_edges_outgoing() == {g.get_edge(
            1, 1)}, "outgoing edge should be (1, 1)"

        assert g[2].incident_edges_outgoing() == {g.get_edge(
            2, 3)}, "outgoing edges should (2, 3)"
        assert not g[2].incident_edges_incoming(
        ), "should be no incoming edges"
Beispiel #9
0
    def test_adj_vertices(self) -> None:
        g = MultiDiGraph([(1, 1), (1, 1), (2, 3), (2, 3)])
        assert g[1].adj_vertices() == {
            g[1]
        }, "vertex with directed self-loop should be adjacent to itself"
        assert g[1].adj_vertices_incoming() == {
            g[1]
        }, "vertex with directed self-loop should connect to itself via an incoming edge"
        assert g[1].adj_vertices_outgoing() == {
            g[1]
        }, "vertex with directed self-loop should connect to itself via an outgoing edge"

        assert g[2].adj_vertices() == {
            g[3]
        }, "vertex 2 should be adjacent to vertex 3"
        assert not g[2].adj_vertices_incoming(
        ), "vertex 2 should have no incoming adjacent vertices"
        assert g[2].adj_vertices_outgoing() == {
            g[3]
        }, "vertex 2 should have vertex 3 as outgoing adjacent vertex"
Beispiel #10
0
 def test_equality_operator(self) -> None:
     mdg = MultiDiGraph([(1, 2), (2, 1), (3, 4), (3, 4, 1.5),
                         (6, 7, 9.5, {
                             "k": "v1"
                         })])
     mdg2 = MultiDiGraph([(2, 1), (2, 1), (3, 4), (3, 4, 5.5),
                          (6, 7, 9.5, {
                              "k": "v2"
                          })])
     assert mdg.get_edge(1, 2) != mdg2.get_edge(
         2, 1), "multiedges (1, 2) and (2, 1) should not be equal"
     assert mdg.get_edge(2, 1) != mdg2.get_edge(
         2, 1
     ), "multiedges (2, 1) should not be equal due to different multiplicities)"
     assert mdg.get_edge(3, 4) != mdg2.get_edge(
         3, 4
     ), "multiedges (3, 4) should not be equal due to different weights"
     assert mdg.get_edge(6, 7) == mdg2.get_edge(
         6, 7
     ), "multiedges (6, 7) should be equal (attributes of parallel connections not checked)"