def test_vertex_count(self) -> None: g = Graph([(1, 2), (2, 3), (3, 4)]) assert g.vertex_count == 4, "graph should have 4 vertices" g.add_vertex(5) assert g.vertex_count == 5, "graph should have 5 vertices" g.remove_vertex(5) assert g.vertex_count == 4, "graph should have 4 vertices"
def test_kruskal_optimum_forest_multiple_trees(self) -> None: g = Graph(test_edges) g.add_edge("x", "y", weight=22) g.add_edge("y", "z", weight=20) g.add_vertex("isolated") count = 0 total_weight = 0.0 for tree in undirected.kruskal_optimum_forest(g): count += 1 total_weight += tree.weight assert count == 3, "there should be 3 trees in the spanning forest" assert total_weight == 70, "total weight of trees should be 70"
def test_remove_vertex(self) -> None: g = Graph([(1, 2), (3, 3)]) g.add_vertex(4) assert g.has_vertex(4), "graph should have vertex 4" g.remove_vertex(4) assert not g.has_vertex( 4), "graph should not have vertex 4 after removal" assert g.has_vertex(3), "graph should have semi-isolated vertex 3" g.remove_vertex(3) assert not g.has_vertex( 3), "graph should not have vertex 3 after removal" with pytest.raises(exception.VertizeeException): g.remove_vertex(1)
def test_connected_components(self) -> None: g = Graph([(1, 2), (2, 3), (4, 5), (7, 7)]) g.add_vertex(8) component_list: List[Component[Vertex, Edge]] = list( components.connected_components(g)) assert len(component_list) == 4, "graph should have 4 components" edge_count = sum( len(list(component.edges())) for component in component_list) assert edge_count == 4, "components should contain a grand total of 4 edges" mg: MultiDiGraph = get_example_multidigraph() scc_list: List[Component[MultiDiVertex, MultiDiEdge]] = list( components.connected_components(mg)) assert len( scc_list ) == 4, "multidigraph should have 4 strongly-connected components"
def test_remove_isolated_vertices(self) -> None: g = Graph([(1, 2), (2, 3), (5, 5)]) g.remove_edge(1, 2, remove_semi_isolated_vertices=False) g.add_vertex(4) assert set(g.vertices()) == {g[1], g[2], g[3], g[4], g[5]} g.remove_isolated_vertices(ignore_self_loops=False) assert set(g.vertices()) == { g[2], g[3], g[5], }, "isolated vertices 1 and 4 should have been removed" g.remove_isolated_vertices(ignore_self_loops=True) assert set(g.vertices()) == { g[2], g[3] }, "semi-isolated vertex 5 should have been removed"
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"
def test__getitem__setitem(self) -> None: g = Graph() v1: Vertex = g.add_vertex(1) pairs = [("1", "one"), (2, "two")] d1: VertexDict[str] = VertexDict(pairs) assert d1[ v1] == "one", "Dict d1 getitem should work with Vertex object key" assert d1[1] == "one", "Dict d1 getitem should work with int key" assert d1["1"] == "one", "Dict d1 getitem should work with int key" assert d1[ "2"] == "two", "Dict d1 should have item associated with key 2" d1[2] = "new value" assert d1[ "2"] == "new value", 'Dict d1 should have "new value" for key 2'
def test_add_vertex(self) -> None: g = Graph() g.add_vertex(1) g.add_vertex("2") g.add_vertex("v3", color="blue", mass=42) assert g.has_vertex(1), "graph should have vertex 1" assert g.has_vertex(2), "graph should have vertex 2" assert g.has_vertex("v3"), "graph should have vertex v3" assert g["v3"][ "color"] == "blue", "vertex should have 'color' attribute set to 'blue'" assert g["v3"][ "mass"] == 42, "vertex should have 'mass' attribute set to 42"