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"
def test_dfs_undirected_cyclic_graph(self) -> None: g = Graph() g.add_edges_from([(0, 1), (1, 2), (1, 3), (2, 3), (3, 4), (4, 5), (3, 5), (6, 7)]) results: SearchResults[Vertex, Edge] = dfs(g, 0) tree: Tree[Vertex, Edge] = next(iter(results.graph_search_trees())) assert tree.root == 0, "DFS tree should be rooted at vertex 0" assert ( len(results.graph_search_trees()) == 1 ), "DFS search with source vertex should yield one DFS tree" assert len(tree) == 6, "DFS tree should have 6 vertices (excluding vertices 6 & 7)" assert ( len(tree.edges()) == 5 ), "DFS tree should have 5 edges, since for all trees |E| = |V| - 1" assert g[6] not in tree, "DFS tree should not contain vertex 6" assert not results.is_acyclic(), "graph should not be acyclic, since it contains 2 cycles" dfs_vertices = results.vertices_preorder() assert len(tree) == len( dfs_vertices ), "DFS vertices should match the DFS tree, since only one tree was searched" dfs_edges = results.edges_in_discovery_order() assert tree.edge_count == len( dfs_edges ), "DFS edges should match the DFS tree, since only one tree was searched" assert len(results.back_edges()) > 0, "tree should have back edges, since there are cycles" assert ( not results.has_topological_ordering() ), "since graph contains cycles, there should be no topological ordering" first_edge: Edge = results.edges_in_discovery_order()[0] assert first_edge.vertex1 == 0, "first edge should have vertex1 of 0" assert first_edge.vertex2 == 1, "first edge should have vertex2 of 1" assert ( not results.cross_edges() and not results.forward_edges() ), "using DFS in an undirected graph, every edge is either a tree edge or a back edge"
def test_bfs_undirected_cyclic_graph(self) -> None: g = Graph() g.add_edges_from([(0, 1), (1, 2), (1, 3), (2, 3), (3, 4), (4, 5), (3, 5), (6, 7)]) results: SearchResults[Vertex, Edge] = bfs(g, 0) tree: Tree[Vertex, Edge] = next(iter(results.graph_search_trees())) assert tree.root == 0, "BFS tree should be rooted at vertex 0" assert ( len(results.graph_search_trees()) == 1 ), "BFS search with source vertex should yield one BFS tree" assert len(tree) == 6, "BFS tree should have 6 vertices (excluding vertices 6 & 7)" assert ( len(tree.edges()) == 5 ), "BFS tree should have 5 edges, since for all trees |E| = |V| - 1" assert g[6] not in tree, "BFS tree should not contain vertex 6" # Breadth-first search does not support cycle detection. with pytest.raises(exception.VertizeeException): results.is_acyclic() assert ( not results.has_topological_ordering() ), "no topological ordering for undirected graphs" with pytest.raises(exception.Unfeasible): results.vertices_topological_order() assert ( results.vertices_preorder() == results.vertices_postorder() ), "a BFS should yield vertices in same order for both preorder and postorder" assert not results.back_edges(), "BFS on undirected graph cannot have back edges" assert not results.forward_edges(), "BFS on undirected graph cannot have forward edges" assert len(results.cross_edges()) == 2, "tree should have 2 cross edges" first_edge: Edge = results.edges_in_discovery_order()[0] assert first_edge.vertex1 == 0, "first edge should have vertex1 of 0" assert first_edge.vertex2 == 1, "first edge should have vertex2 of 1"