def test_un_directed_weighted_graph_classify_edges_acyclic(self): """ Test classify edges - acyclic graph. """ # Create an undirected unweighted acyclic graph a_graph = graph.UnDirectedWeightedGraph(4) v_1 = graph_vertex.UnWeightedGraphVertex(a_graph, 'A') v_2 = graph_vertex.UnWeightedGraphVertex(a_graph, 'B') v_3 = graph_vertex.UnWeightedGraphVertex(a_graph, 'C') v_4 = graph_vertex.UnWeightedGraphVertex(a_graph, 'D') a_graph.add_vertex(v_1) a_graph.add_vertex(v_2) a_graph.add_vertex(v_3) a_graph.add_vertex(v_4) a_graph.add_edge(v_1, v_2, 10) a_graph.add_edge(v_2, v_3, 20) a_graph.add_edge(v_2, v_4, 30) res = a_graph.classify_edges().get_edges() ref = dfs_edge_classification.DFSEdgeClassification( a_graph).get_edges() e12 = graph_edge.UnDirectedWeightedGraphEdge(a_graph, v_1, v_2, 10) e23 = graph_edge.UnDirectedWeightedGraphEdge(a_graph, v_2, v_3, 20) e24 = graph_edge.UnDirectedWeightedGraphEdge(a_graph, v_2, v_4, 30) ref[e12] = graph_edge.EdgeClassification.TREE_EDGE ref[e23] = graph_edge.EdgeClassification.TREE_EDGE ref[e24] = graph_edge.EdgeClassification.TREE_EDGE self.assertEqual(res, ref)
def test_un_directed_weighted_graph_has_edge_not(self): """ Test method "has_edge" - inverted. """ vertex_a = graph_vertex.UnWeightedGraphVertex(self.g_1, 'X') vertex_b = graph_vertex.UnWeightedGraphVertex(self.g_1, 'Y') a_edge = graph_edge.UnDirectedWeightedGraphEdge( self.g_1, vertex_a, vertex_b, 7) self.assertFalse(self.g_1.has_edge(a_edge))
def test_graph_algorithms_prim_0(self): """ Test of Prims algorithm. """ elem1 = graph_edge.UnDirectedWeightedGraphEdge(self.graph1, self.v2_g1, self.v1_g1, 7) elem2 = graph_edge.UnDirectedWeightedGraphEdge(self.graph1, self.v3_g1, self.v5_g1, 5) elem3 = graph_edge.UnDirectedWeightedGraphEdge(self.graph1, self.v4_g1, self.v1_g1, 5) elem4 = graph_edge.UnDirectedWeightedGraphEdge(self.graph1, self.v5_g1, self.v2_g1, 7) elem5 = graph_edge.UnDirectedWeightedGraphEdge(self.graph1, self.v6_g1, self.v4_g1, 6) elem6 = graph_edge.UnDirectedWeightedGraphEdge(self.graph1, self.v7_g1, self.v5_g1, 9) mst_ref = minimum_spanning_tree.MinimumSpanningTree(self.graph1) mst_ref.add_edge(elem1) mst_ref.add_edge(elem2) mst_ref.add_edge(elem3) mst_ref.add_edge(elem4) mst_ref.add_edge(elem5) mst_ref.add_edge(elem6) mst_res = graph_algorithms.GraphAlgorithms.prims_algorithm( self.graph1, self.v1_g1) self.assertEqual(mst_ref, mst_res)
def test_un_directed_weighted_graph_has_edge(self): """ Test method "has_edge". """ a_graph = graph.UnDirectedWeightedGraph(2) vertex_a = graph_vertex.UnWeightedGraphVertex(a_graph, 'A') vertex_b = graph_vertex.UnWeightedGraphVertex(a_graph, 'B') a_graph.add_vertex(vertex_a) a_graph.add_vertex(vertex_b) a_edge = graph_edge.UnDirectedWeightedGraphEdge( a_graph, vertex_a, vertex_b, 7) a_graph.add_edge(vertex_a, vertex_b, 7) self.assertTrue(a_graph.has_edge(a_edge))
def setUp(self): self.g_1 = graph.UnDirectedWeightedGraph(7) self.v_1 = graph_vertex.UnWeightedGraphVertex(self.g_1, 'A') self.v_2 = graph_vertex.UnWeightedGraphVertex(self.g_1, 'B') self.v_3 = graph_vertex.UnWeightedGraphVertex(self.g_1, 'C') self.v_4 = graph_vertex.UnWeightedGraphVertex(self.g_1, 'D') self.v_5 = graph_vertex.UnWeightedGraphVertex(self.g_1, 'E') self.v_6 = graph_vertex.UnWeightedGraphVertex(self.g_1, 'F') self.v_7 = graph_vertex.UnWeightedGraphVertex(self.g_1, 'G') self.g_1.add_vertex(self.v_1) self.g_1.add_vertex(self.v_2) self.g_1.add_vertex(self.v_3) self.g_1.add_vertex(self.v_4) self.g_1.add_vertex(self.v_5) self.g_1.add_vertex(self.v_6) self.g_1.add_vertex(self.v_7) self.e12 = graph_edge.UnDirectedWeightedGraphEdge( self.g_1, self.v_1, self.v_2, 7) self.e21 = graph_edge.UnDirectedWeightedGraphEdge( self.g_1, self.v_2, self.v_1, 7) self.e14 = graph_edge.UnDirectedWeightedGraphEdge( self.g_1, self.v_1, self.v_4, 5) self.e41 = graph_edge.UnDirectedWeightedGraphEdge( self.g_1, self.v_4, self.v_1, 5) self.e23 = graph_edge.UnDirectedWeightedGraphEdge( self.g_1, self.v_2, self.v_3, 8) self.e32 = graph_edge.UnDirectedWeightedGraphEdge( self.g_1, self.v_3, self.v_2, 8) self.e24 = graph_edge.UnDirectedWeightedGraphEdge( self.g_1, self.v_2, self.v_4, 9) self.e42 = graph_edge.UnDirectedWeightedGraphEdge( self.g_1, self.v_4, self.v_2, 9) self.e25 = graph_edge.UnDirectedWeightedGraphEdge( self.g_1, self.v_2, self.v_5, 7) self.e52 = graph_edge.UnDirectedWeightedGraphEdge( self.g_1, self.v_5, self.v_2, 7) self.e35 = graph_edge.UnDirectedWeightedGraphEdge( self.g_1, self.v_3, self.v_5, 5) self.e53 = graph_edge.UnDirectedWeightedGraphEdge( self.g_1, self.v_5, self.v_3, 5) self.e45 = graph_edge.UnDirectedWeightedGraphEdge( self.g_1, self.v_4, self.v_5, 15) self.e54 = graph_edge.UnDirectedWeightedGraphEdge( self.g_1, self.v_5, self.v_4, 15) self.e46 = graph_edge.UnDirectedWeightedGraphEdge( self.g_1, self.v_4, self.v_6, 6) self.e64 = graph_edge.UnDirectedWeightedGraphEdge( self.g_1, self.v_6, self.v_4, 6) self.e56 = graph_edge.UnDirectedWeightedGraphEdge( self.g_1, self.v_5, self.v_6, 8) self.e65 = graph_edge.UnDirectedWeightedGraphEdge( self.g_1, self.v_6, self.v_5, 8) self.e57 = graph_edge.UnDirectedWeightedGraphEdge( self.g_1, self.v_5, self.v_7, 9) self.e75 = graph_edge.UnDirectedWeightedGraphEdge( self.g_1, self.v_7, self.v_5, 9) self.e67 = graph_edge.UnDirectedWeightedGraphEdge( self.g_1, self.v_6, self.v_7, 11) self.e76 = graph_edge.UnDirectedWeightedGraphEdge( self.g_1, self.v_7, self.v_6, 11) self.g_1.add_edge(self.v_1, self.v_2, 7) self.g_1.add_edge(self.v_1, self.v_4, 5) self.g_1.add_edge(self.v_2, self.v_3, 8) self.g_1.add_edge(self.v_2, self.v_4, 9) self.g_1.add_edge(self.v_2, self.v_5, 7) self.g_1.add_edge(self.v_3, self.v_5, 5) self.g_1.add_edge(self.v_4, self.v_5, 15) self.g_1.add_edge(self.v_4, self.v_6, 6) self.g_1.add_edge(self.v_5, self.v_6, 8) self.g_1.add_edge(self.v_5, self.v_7, 9) self.g_1.add_edge(self.v_6, self.v_7, 11)