예제 #1
0
    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_directed_weighted_graph_classify_edges_cyclic(self):
        """
        Test graph classification for directed weighted cyclic graph.
        """
        # Create a directed weighted cyclic graph

        a_graph = graph.DirectedWeightedGraph(4)
        vertex1 = graph_vertex.UnWeightedGraphVertex(a_graph, 'A')
        vertex2 = graph_vertex.UnWeightedGraphVertex(a_graph, 'B')
        vertex3 = graph_vertex.UnWeightedGraphVertex(a_graph, 'C')
        vertex4 = graph_vertex.UnWeightedGraphVertex(a_graph, 'D')

        a_graph.add_vertex(vertex1)
        a_graph.add_vertex(vertex2)
        a_graph.add_vertex(vertex3)
        a_graph.add_vertex(vertex4)

        a_graph.add_edge(vertex1, vertex2, 10)
        a_graph.add_edge(vertex2, vertex3, 20)
        a_graph.add_edge(vertex3, vertex1, 30)

        res = a_graph.classify_edges().get_edges()
        ref = dfs_edge_classification.DFSEdgeClassification(
            a_graph).get_edges()
        e12 = graph_edge.DirectedWeightedGraphEdge(a_graph, vertex1, vertex2,
                                                   10)
        e23 = graph_edge.DirectedWeightedGraphEdge(a_graph, vertex2, vertex3,
                                                   20)
        e31 = graph_edge.DirectedWeightedGraphEdge(a_graph, vertex3, vertex1,
                                                   30)
        ref[e12] = graph_edge.EdgeClassification.TREE_EDGE
        ref[e23] = graph_edge.EdgeClassification.TREE_EDGE
        ref[e31] = graph_edge.EdgeClassification.BACK_EDGE
        self.assertEqual(res, ref)
 def test_dfs_edge_classification_undirected_cyclic_clear(self):
     """
     Test method "clear" using an undirected graph.
     """
     res = self.classification_undirected_cyclic.clear()
     ref = dfs_edge_classification.DFSEdgeClassification(self.g_undirected_cyclic).clear()
     self.assertEqual(ref, res)
    def test_dfs_edge_classification_directed_cyclic_get_edges(self):
        """
        Test method "get_edges" using a directed graph.
        """
        res = self.classification_directed_cyclic.get_edges()
        # Create reference from (Cormen page 542)
        ref = dfs_edge_classification.DFSEdgeClassification(
            self.g_directed_cyclic).get_edges()
        e_uv = self.g_directed_cyclic.get_edge(self.u_g_directed_cyclic,
                                               self.v_g_directed_cyclic)
        e_ux = self.g_directed_cyclic.get_edge(self.u_g_directed_cyclic,
                                               self.x_g_directed_cyclic)
        e_vy = self.g_directed_cyclic.get_edge(self.v_g_directed_cyclic,
                                               self.y_g_directed_cyclic)
        e_wy = self.g_directed_cyclic.get_edge(self.w_g_directed_cyclic,
                                               self.y_g_directed_cyclic)
        e_wz = self.g_directed_cyclic.get_edge(self.w_g_directed_cyclic,
                                               self.z_g_directed_cyclic)
        e_xv = self.g_directed_cyclic.get_edge(self.x_g_directed_cyclic,
                                               self.v_g_directed_cyclic)
        e_yx = self.g_directed_cyclic.get_edge(self.y_g_directed_cyclic,
                                               self.x_g_directed_cyclic)
        e_zz = self.g_directed_cyclic.get_edge(self.z_g_directed_cyclic,
                                               self.z_g_directed_cyclic)

        # Add the edges to map of edges
        ref[e_uv] = graph_edge.EdgeClassification.TREE_EDGE
        ref[e_ux] = graph_edge.EdgeClassification.FORWARD_EDGE
        ref[e_vy] = graph_edge.EdgeClassification.TREE_EDGE
        ref[e_wy] = graph_edge.EdgeClassification.CROSS_EDGE
        ref[e_wz] = graph_edge.EdgeClassification.TREE_EDGE
        ref[e_xv] = graph_edge.EdgeClassification.BACK_EDGE
        ref[e_yx] = graph_edge.EdgeClassification.TREE_EDGE
        ref[e_zz] = graph_edge.EdgeClassification.BACK_EDGE
        self.assertEqual(ref, res)
예제 #5
0
    def test_un_directed_graph_classify_edges_cyclic(self):
        """
        Test method "classify_edges" - cyclic graph.
        """
        # Create an undirected cyclic graph
        a_graph = graph.UnDirectedGraph(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)
        a_graph.add_edge(v_2, v_3)
        a_graph.add_edge(v_1, v_3)

        res = a_graph.classify_edges().get_edges()
        ref = dfs_edge_classification.DFSEdgeClassification(
            a_graph).get_edges()
        e12 = graph_edge.UnDirectedGraphEdge(a_graph, v_1, v_2)
        e23 = graph_edge.UnDirectedGraphEdge(a_graph, v_2, v_3)
        e13 = graph_edge.UnDirectedGraphEdge(a_graph, v_3, v_1)
        ref[e12] = graph_edge.EdgeClassification.TREE_EDGE
        ref[e23] = graph_edge.EdgeClassification.TREE_EDGE
        ref[e13] = graph_edge.EdgeClassification.BACK_EDGE
        self.assertEqual(res, ref)
    def test_directed_un_weighted_graph_classify_edges_acyclic(self):
        """
        Test edge classification - directed unweighted acyclic graph.
        """
        # Create a directed unweighted acyclic graph

        a_graph = graph.DirectedUnWeightedGraph(4)
        vertex1 = graph_vertex.UnWeightedGraphVertex(a_graph, 'A')
        vertex2 = graph_vertex.UnWeightedGraphVertex(a_graph, 'B')
        vertex3 = graph_vertex.UnWeightedGraphVertex(a_graph, 'C')
        vertex4 = graph_vertex.UnWeightedGraphVertex(a_graph, 'D')

        a_graph.add_vertex(vertex1)
        a_graph.add_vertex(vertex2)
        a_graph.add_vertex(vertex3)
        a_graph.add_vertex(vertex4)

        a_graph.add_edge(vertex1, vertex2)
        a_graph.add_edge(vertex2, vertex3)
        a_graph.add_edge(vertex2, vertex4)

        res = a_graph.classify_edges().get_edges()
        ref = dfs_edge_classification.DFSEdgeClassification(
            a_graph).get_edges()
        e12 = graph_edge.DirectedUnWeightedGraphEdge(a_graph, vertex1, vertex2)
        e23 = graph_edge.DirectedUnWeightedGraphEdge(a_graph, vertex2, vertex3)
        e24 = graph_edge.DirectedUnWeightedGraphEdge(a_graph, vertex2, vertex4)
        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_dfs_edge_classification_undirected_cyclic_get_cross_edges(self):
     """
     Test method "get_cross_edges" using an undirected graph.
     """
     res = self.classification_undirected_cyclic.get_cross_edges()
     ref = dfs_edge_classification.DFSEdgeClassification(self.g_undirected_cyclic).get_cross_edges()
     self.assertEqual(ref, res)
 def test_dfs_edge_classification_undirected_cyclic_get_back_edges(self):
     """
     Test method "get_back_edges" using an undirected graph.
     """
     res = self.classification_undirected_cyclic.get_back_edges()
     ref = dfs_edge_classification.DFSEdgeClassification(self.g_undirected_cyclic).get_back_edges()
     e31 = graph_edge.UnDirectedGraphEdge(self.g_undirected_cyclic, self.c_g_undirected_cyclic, self.a_g_undirected_cyclic)
     ref.add(e31)
     self.assertEqual(ref, res)
 def test_dfs_edge_classification_directed_cyclic_get_cross_edges(self):
     """
     Test method "get_cross_edges" using a directed graph.
     """
     res = self.classification_directed_cyclic.get_cross_edges()
     ref = dfs_edge_classification.DFSEdgeClassification(self.g_directed_cyclic).get_cross_edges()
     edge_wy = self.g_directed_cyclic.get_edge(self.w_g_directed_cyclic, self.y_g_directed_cyclic)
     ref.add(edge_wy)
     self.assertEqual(ref, res)
 def test_dfs_edge_classification_undirected_cyclic_get_finishing_time(self):
     """
     Test method "get_finishing_time" using an undirected graph.
     """
     res = self.classification_undirected_cyclic.get_finishing_time()
     ref = dfs_edge_classification.DFSEdgeClassification(self.g_undirected_cyclic).get_finishing_time()
     ref[self.a_g_undirected_cyclic] = 6
     ref[self.b_g_undirected_cyclic] = 5
     ref[self.c_g_undirected_cyclic] = 4
     ref[self.d_g_undirected_cyclic] = 8
     self.assertEqual(ref, res)
 def test_dfs_edge_classification_directed_cyclic_get_back_edges(self):
     """
     Test method "get_back_edges" using a directed graph.
     """
     res = self.classification_directed_cyclic.get_back_edges()
     ref = dfs_edge_classification.DFSEdgeClassification(self.g_directed_cyclic).get_back_edges()
     edge_xv = self.g_directed_cyclic.get_edge(self.x_g_directed_cyclic, self.v_g_directed_cyclic)
     edge_zz = self.g_directed_cyclic.get_edge(self.z_g_directed_cyclic, self.z_g_directed_cyclic)
     ref.add(edge_xv)
     ref.add(edge_zz)
     self.assertEqual(ref, res)
 def test_dfs_edge_classification_undirected_cyclic_get_order(self):
     """
     Test method "get_order" using an undirected graph.
     """
     res = self.classification_undirected_cyclic.get_order()
     ref = dfs_edge_classification.DFSEdgeClassification(self.g_undirected_cyclic).get_order()
     ref.append(self.c_g_undirected_cyclic)
     ref.append(self.b_g_undirected_cyclic)
     ref.append(self.a_g_undirected_cyclic)
     ref.append(self.d_g_undirected_cyclic)
     self.assertEqual(ref, res)
 def test_dfs_edge_classification_undirected_cyclic_get_discovery_time(self):
     """
     Test method "get_discovery_time" using an undirected graph.
     """
     res = self.classification_undirected_cyclic.get_discovery_time()
     ref = dfs_edge_classification.DFSEdgeClassification(self.g_undirected_cyclic).get_discovery_time()
     ref[self.a_g_undirected_cyclic] = 1
     ref[self.b_g_undirected_cyclic] = 2
     ref[self.c_g_undirected_cyclic] = 3
     ref[self.d_g_undirected_cyclic] = 7
     self.assertEqual(ref, res)
 def test_dfs_edge_classification_undirected_cyclic_get_parent(self):
     """
     Test method "get_parent" using an undirected graph.
     """
     res = self.classification_undirected_cyclic.get_parent()
     ref = dfs_edge_classification.DFSEdgeClassification(self.g_undirected_cyclic).get_parent()
     ref[self.a_g_undirected_cyclic] = None
     ref[self.b_g_undirected_cyclic] = self.a_g_undirected_cyclic
     ref[self.c_g_undirected_cyclic] = self.b_g_undirected_cyclic
     ref[self.d_g_undirected_cyclic] = None
     self.assertEqual(ref, res)
 def test_dfs_edge_classification_directed_cyclic_get_order(self):
     """
     Test method "get_order" using a directed graph.
     """
     res = self.classification_directed_cyclic.get_order()
     ref = dfs_edge_classification.DFSEdgeClassification(self.g_directed_cyclic).get_order()
     ref.append(self.x_g_directed_cyclic)
     ref.append(self.y_g_directed_cyclic)
     ref.append(self.v_g_directed_cyclic)
     ref.append(self.u_g_directed_cyclic)
     ref.append(self.z_g_directed_cyclic)
     ref.append(self.w_g_directed_cyclic)
     self.assertEqual(ref, res)
 def test_dfs_edge_classification_undirected_cyclic_get_edges(self):
     """
     Test method "get_edges" using an undirected graph.
     """
     res = self.classification_undirected_cyclic.get_edges()
     ref = dfs_edge_classification.DFSEdgeClassification(self.g_undirected_cyclic).get_edges()
     e31 = graph_edge.UnDirectedGraphEdge(self.g_undirected_cyclic, self.c_g_undirected_cyclic, self.a_g_undirected_cyclic)
     e23 = graph_edge.UnDirectedGraphEdge(self.g_undirected_cyclic, self.b_g_undirected_cyclic, self.c_g_undirected_cyclic)
     e12 = graph_edge.UnDirectedGraphEdge(self.g_undirected_cyclic, self.a_g_undirected_cyclic, self.b_g_undirected_cyclic)
     ref[e31] = graph_edge.EdgeClassification.BACK_EDGE
     ref[e23] = graph_edge.EdgeClassification.TREE_EDGE
     ref[e12] = graph_edge.EdgeClassification.TREE_EDGE
     self.assertEqual(ref, res)
 def test_dfs_edge_classification_directed_cyclic_get_tree_edges(self):
     """
     Test method "get_tree_edges" using a directed graph.
     """
     res = self.classification_directed_cyclic.get_tree_edges()
     ref = dfs_edge_classification.DFSEdgeClassification(self.g_directed_cyclic).get_tree_edges()
     edge_vy = self.g_directed_cyclic.get_edge(self.v_g_directed_cyclic, self.y_g_directed_cyclic)
     edge_uv = self.g_directed_cyclic.get_edge(self.u_g_directed_cyclic, self.v_g_directed_cyclic)
     edge_yx = self.g_directed_cyclic.get_edge(self.y_g_directed_cyclic, self.x_g_directed_cyclic)
     edge_wz = self.g_directed_cyclic.get_edge(self.w_g_directed_cyclic, self.z_g_directed_cyclic)
     ref.add(edge_vy)
     ref.add(edge_uv)
     ref.add(edge_yx)
     ref.add(edge_wz)
     self.assertEqual(ref, res)
    def test_dfs_edge_classification_directed_cyclic_get_parent(self):
        """
        Test method "get_parent" using a directed graph.
        """
        res = self.classification_directed_cyclic.get_parent()
        ref = dfs_edge_classification.DFSEdgeClassification(self.g_directed_cyclic).get_parent()

        # Add vertices to map of parent for vertices
        ref[self.u_g_directed_cyclic] = None
        ref[self.v_g_directed_cyclic] = self.u_g_directed_cyclic
        ref[self.w_g_directed_cyclic] = None
        ref[self.x_g_directed_cyclic] = self.y_g_directed_cyclic
        ref[self.y_g_directed_cyclic] = self.v_g_directed_cyclic
        ref[self.z_g_directed_cyclic] = self.w_g_directed_cyclic
        self.assertEqual(ref, res)
    def test_dfs_edge_classification_directed_cyclic_get_discovery_time(self):
        """
        Test method "get_discovery_time" using a directed graph.
        """
        res = self.classification_directed_cyclic.get_discovery_time()
        ref = dfs_edge_classification.DFSEdgeClassification(self.g_directed_cyclic).get_discovery_time()

        # Add vertices to map of discovering times for vertices
        ref[self.u_g_directed_cyclic] = 1
        ref[self.v_g_directed_cyclic] = 2
        ref[self.y_g_directed_cyclic] = 3
        ref[self.x_g_directed_cyclic] = 4
        ref[self.w_g_directed_cyclic] = 9
        ref[self.z_g_directed_cyclic] = 10
        self.assertEqual(ref, res)
    def test_dfs_edge_classification_directed_cyclic_get_finishing_time(self):
        """
        Test method "get_finishing_time" using a directed graph.
        """
        res = self.classification_directed_cyclic.get_finishing_time()
        ref = dfs_edge_classification.DFSEdgeClassification(self.g_directed_cyclic).get_finishing_time()

        # Add vertices to map of finishing times for vertices
        ref[self.x_g_directed_cyclic] = 5
        ref[self.y_g_directed_cyclic] = 6
        ref[self.v_g_directed_cyclic] = 7
        ref[self.u_g_directed_cyclic] = 8
        ref[self.z_g_directed_cyclic] = 11
        ref[self.w_g_directed_cyclic] = 12
        self.assertEqual(ref, res)