Beispiel #1
0
 def test__len__(self) -> None:
     g = Graph([(1, 2), (2, 3), (3, 4)])
     tree: Tree[Vertex, Edge] = Tree(g[1])
     tree.add_edge(g.get_edge(1, 2))
     tree.add_edge(g.get_edge(2, 3))
     tree.add_edge(g.get_edge(3, 4))
     assert len(tree) == 4, "tree should contain 4 vertices"
Beispiel #2
0
 def test_edges_and_edge_count(self) -> None:
     g = Graph([(1, 2), (2, 1), (2, 3), (3, 4)])
     assert set(g.edges()) == {
         g.get_edge(1, 2),
         g.get_edge(2, 3),
         g.get_edge(3, 4),
     }, "edges should be (1, 2), (2, 3), (3, 4)"
     assert g.edge_count == 3, "graph should have 3 edges"
Beispiel #3
0
    def test_merge(self) -> None:
        g = Graph([(1, 2), (2, 3), (1, 4), (3, 4), (4, 5)])
        tree1: Tree[Vertex, Edge] = Tree(g[1])
        tree5: Tree[Vertex, Edge] = Tree(g[5])

        tree1.add_edge(g.get_edge(1, 4))
        tree1.add_edge(g.get_edge(4, 3))
        tree5.add_edge(g.get_edge(5, 4))
        tree1.merge(tree5)
        assert set(tree1.vertices()) == {tree1[1], tree1[3], tree1[4], tree1[5]}
Beispiel #4
0
 def test__iter__(self) -> None:
     g = Graph([(1, 2), (2, 3), (3, 4)])
     tree: Tree[Vertex, Edge] = Tree(g[1])
     tree.add_edge(g.get_edge(1, 2))
     tree.add_edge(g.get_edge(2, 3))
     tree.add_edge(g.get_edge(3, 4))
     count = sum(1 for _ in tree)
     assert count == 4, "tree should iterate over its 4 vertices"
     assert set([tree[1], tree[2], tree[3], tree[4]]) == set(
         tree
     ), "tree should iterate over its 4 vertices"
Beispiel #5
0
    def test_add_edge(self) -> None:
        g = Graph([(1, 2), (2, 3), (1, 4), (3, 4), (4, 5)])
        tree: Tree[Vertex, Edge] = Tree(g[1])
        tree.add_edge(g.get_edge(1, 2))
        assert (1, 2) in tree

        tree.add_edge(g.get_edge(2, 3))
        with pytest.raises(exception.Unfeasible):
            # Raise exception due to (4, 5) not containing a vertex already in the tree.
            tree.add_edge(g.get_edge(4, 5))

        tree.add_edge(g.get_edge(3, 4))
        with pytest.raises(exception.Unfeasible):
            # Raises exception due to cycle.
            tree.add_edge(g.get_edge(1, 4))
Beispiel #6
0
    def test_get_random_edge(self) -> None:
        g = Graph([(1, 2), (3, 4), (5, 6)])

        cnt: Counter[Edge] = collections.Counter()
        for _ in range(1000):
            rand_edge = g.get_random_edge()
            if rand_edge is None:
                raise Exception("rand_edge returned None")
            cnt[rand_edge] += 1
        assert cnt[g.get_edge(
            1, 2)] > 270, r"~33% of random samples should be edge (1, 2)"
        assert cnt[g.get_edge(
            3, 4)] > 270, r"~33% of random samples should be edge (3, 4)"
        assert cnt[g.get_edge(
            5, 6)] > 270, r"~33% of random samples should be edge (5, 6)"
Beispiel #7
0
    def test_add_edges_from(self) -> None:
        g = Graph()
        g.add_edges_from([
            (1, 2, 4.5, {
                "color": "blue",
                "mass": 42
            }),
            (4, 3, 9.5),
            (5, 6, {
                "color": "red",
                "mass": 99
            }),
            (8, 7),
            (7, 8),
        ])

        assert g.edge_count == 4, "graph should have 4 edges"
        assert g.get_edge(
            1, 2).weight == 4.5, "edge (1, 2) should have weight 4.5"
        assert g.get_edge(
            1, 2
        )["color"] == "blue", "edge (1, 2) should have 'color' set to 'blue'"
        assert g.get_edge(
            1, 2)["mass"] == 42, "edge (1, 2) should have 'mass' set to 42"
        assert not g.get_edge(3, 4).has_attributes_dict(
        ), "edge (3, 4) should not have attributes dict"
        assert (g.get_edge(5, 6).weight == edge_module.DEFAULT_WEIGHT
                ), "edge should have default weight"
        assert g.get_edge(7, 8) is g.get_edge(
            8, 7), "order of vertices should not matter"
Beispiel #8
0
    def test__contains__(self) -> None:
        g = Graph([(1, 2), (2, 3), (1, 4), (3, 4), (4, 5)])
        tree: Tree[Vertex, Edge] = Tree(g[1])
        assert g[1] in tree, "vertex 1 should be in tree"
        assert 1 in tree, "vertex specified as int should be in tree"
        assert "1" in tree, "vertex specified as str should be in tree"
        assert 3 not in tree, "vertex 3 should not be in tree"

        tree.add_edge(g.get_edge(1, 2))
        assert g.get_edge(1, 2) in tree
        assert (1, 2) in tree, "edge specified as tuple should be in tree"
        assert ("1", "2") in tree
        assert (1, 3) not in tree

        with pytest.raises(TypeError):
            _ = 4.5 not in g  # type: ignore
        with pytest.raises(TypeError):
            _ = (1, 2, 3, 4) not in g  # type: ignore
Beispiel #9
0
    def test_add_edge(self) -> None:
        g = Graph()
        edge = g.add_edge(1, 2, weight=4.5, color="blue", mass=42)
        assert isinstance(edge, Edge), "new edge should an Edge object"
        assert g.get_edge(
            1, 2).weight == 4.5, "edge (1, 2) should have weight 4.5"
        assert edge[
            "color"] == "blue", "edge should have 'color' attribute set to 'blue'"
        assert edge[
            "mass"] == 42, "edge should have 'mass' attribute set to 42"

        edge_dup = g.add_edge(1, 2, weight=1.5, color="red", mass=57)
        assert (
            edge is edge_dup
        ), "adding an edge with same vertices as existing edge should return existing edge"

        g.add_edge(3, 4)
        assert (g.get_edge(3, 4).weight == edge_module.DEFAULT_WEIGHT
                ), "edge should have default weight"
        assert not g.get_edge(3, 4).has_attributes_dict(
        ), "edge should not have attributes dictionary"
Beispiel #10
0
    def test__init__from_graph(self) -> None:
        mg = MultiGraph([(1, 2, 5.0, {
            "color": "red"
        }), (1, 2, 10.0, {
            "color": "black"
        }), (3, 4)])
        assert mg.weight == 16.0, "multigraph should have weight 16.0"

        g = Graph(mg)
        assert g.has_edge(
            1, 2), "graph should have edge (1, 2) copied from multigraph"
        assert (g.get_edge(1, 2)["color"] == "red"
                ), "edge (1, 2) should have 'color' attribute set to red"
        assert g.get_edge(
            1, 2).weight == 5.0, "edge (1, 2) should have weight 5.0"
        assert g.has_edge(
            3, 4), "graph should have edge (3, 4) copied from multigraph"
        assert g.edge_count == 2, "graph should have two edges"
        assert g.weight == 6.0, "graph should have weight 6.0"
        assert g.get_edge(1, 2) is not mg.get_edge(  # type: ignore
            1, 2), "graph should have deep copies of edges and vertices"
Beispiel #11
0
    def test__getitem__(self) -> None:
        g = Graph([(1, 2), (2, 3), (1, 4), (3, 4), (4, 5)])
        tree: Tree[Vertex, Edge] = Tree(g[1])
        assert isinstance(tree[1], Vertex), "tree should have vertex 1"
        assert isinstance(tree["1"], Vertex), "tree should have vertex 1"
        assert isinstance(tree[(1, {})], Vertex), "tree should have vertex 1"
        assert isinstance(tree[1, {}], Vertex), "tree should have vertex 1"
        v1 = tree[1]
        assert isinstance(tree[v1], Vertex), "tree should have vertex 1"
        with pytest.raises(TypeError):
            _ = tree[2.0]  # type: ignore
        with pytest.raises(KeyError):
            _ = tree[3]

        tree.add_edge(g.get_edge(1, 2))
        assert isinstance(tree.get_edge(1, 2), Edge), "tree should have edge (1, 2)"
        assert isinstance(tree.get_edge("1", "2"), Edge), "tree should have edge (1, 2)"
        assert tree.has_edge(1, 2), "tree should have edge (1, 2)"
        with pytest.raises(KeyError):
            _ = g.get_edge(1.0, 2.0)  # type: ignore
        with pytest.raises(KeyError):
            _ = g.get_edge(1, 3)
Beispiel #12
0
    def test_deepcopy(self) -> None:
        g = Graph([(1, 2, {"color": "blue"}), (3, 4)])
        g.add_vertex(42)
        g_copy = g.deepcopy()

        assert set(g.vertices()) == set(
            g_copy.vertices()), "graph copy should have same vertices"
        assert (
            g[1] is not g_copy[1]
        ), "graph copy vertex objects should be distinct from original graph"

        assert set(g.edges()) == set(
            g_copy.edges()), "graph copy should have same edges"
        assert g.get_edge(1, 2) is not g_copy.get_edge(
            1, 2
        ), "graph copy edge objects should be distinct from original graph"
        assert (
            g.get_edge(1, 2)._attr == g_copy.get_edge(1, 2)._attr
        ), "graph copy edge object `_attr` dictionary should contain logically equal contents"
        assert (
            g.get_edge(1, 2)._attr is not g_copy.get_edge(1, 2)._attr
        ), "graph copy edge object `_attr` dictionary should be distinct from original graph"
Beispiel #13
0
    def test__contains__(self) -> None:
        g = Graph()
        g.add_edge(1, 2)
        assert g[1] in g, "vertex 1 should be in graph"
        assert 1 in g, "vertex specified as int should be in graph"
        assert "1" in g, "vertex specified as str should be in graph"
        assert 3 not in g, "vertex 3 should not be in graph"

        assert g.get_edge(1, 2) in g, "edge (1, 2) should be in graph"
        assert (1, 2) in g, "edge specified as tuple should be in graph"
        assert ("1", "2") in g, "edge specified as tuple should be in graph"
        assert (1, 3) not in g, "edge (1, 3) should not be in graph"

        with pytest.raises(TypeError):
            _ = 4.5 not in g  # type: ignore
        with pytest.raises(TypeError):
            _ = (1, 2, 3, 4) not in g  # type: ignore
Beispiel #14
0
    def test__init__from_graph(self) -> None:
        g = Graph([(2, 1, 5.0, {"color": "red"}), (4, 3)])
        assert g.weight == 6.0, "graph should have weight 6.0"

        dg = DiGraph(g)
        assert dg.has_edge(
            2, 1), "digraph should have edge (2, 1) copied from graph"
        assert (dg.get_edge(2, 1)["color"] == "red"
                ), "edge (2, 1) should have 'color' attribute set to red"
        assert dg.get_edge(
            2, 1).weight == 5.0, "edge (2, 1) should have weight 5.0"
        assert dg.has_edge(
            4, 3), "graph should have edge (4, 3) copied from graph"
        assert dg.edge_count == 2, "graph should have two edges"
        assert dg.weight == 6.0, "graph should have weight 6.0"
        assert dg.get_edge(2, 1) is not g.get_edge(  # type: ignore
            2, 1), "digraph should have deep copies of edges and vertices"
Beispiel #15
0
    def test__init__from_graph(self) -> None:
        g0 = Graph([(1, 2, 5.0, {"color": "red"}), (3, 4)])

        g = MultiGraph(g0)
        assert g.has_edge(
            1, 2), "multigraph should have edge (1, 2) copied from graph"
        connection12 = g.get_edge(1, 2).connections()[0]
        assert (
            connection12["color"] == "red"
        ), "edge connection (1, 2) should have 'color' attribute set to red"
        assert g.get_edge(
            1, 2).weight == 5.0, "edge (1, 2) should have weight 5.0"
        assert g.has_edge(3,
                          4), "graph should have edge (3, 4) copied from graph"
        assert g.edge_count == 2, "graph should have two edges"
        assert g.weight == 6.0, "graph should have weight 6.0"
        assert g.get_edge(1, 2) is not g0.get_edge(  # type: ignore
            1, 2), "multigraph should have deep copies of edges and vertices"
Beispiel #16
0
    def test__getitem__(self) -> None:
        g = Graph()
        g.add_edge(1, 2)
        assert isinstance(g[1], Vertex), "graph should have vertex 1"
        assert isinstance(g["1"], Vertex), "graph should have vertex 1"
        assert isinstance(g[(1, {})], Vertex), "graph should have vertex 1"
        assert isinstance(g[1, {}], Vertex), "graph should have vertex 1"
        v1 = g[1]
        assert isinstance(g[v1], Vertex), "graph should have vertex 1"
        with pytest.raises(KeyError):
            _ = g.get_edge(1, 3)
        with pytest.raises(KeyError):
            _ = g[3]

        assert isinstance(g.get_edge(1, 2),
                          Edge), "graph should have edge (1, 2)"
        assert isinstance(g.get_edge("1", "2"),
                          Edge), "graph should have edge (1, 2)"
        _ = g.get_edge(1, 2)
        with pytest.raises(KeyError):
            _ = g.get_edge(1.0, 2.0)  # type: ignore
        with pytest.raises(KeyError):
            _ = g.get_edge(1, 3)