コード例 #1
0
    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)
コード例 #2
0
    def setUp(self):
        # Create directed unweighted graph Cormen page 596.
        self.graph1 = graph.DirectedUnWeightedGraph(5)

        self.v1_g1 = graph_vertex.UnWeightedGraphVertex(self.graph1, 'S')
        self.v2_g1 = graph_vertex.UnWeightedGraphVertex(self.graph1, 'T')
        self.v3_g1 = graph_vertex.UnWeightedGraphVertex(self.graph1, 'X')
        self.v4_g1 = graph_vertex.UnWeightedGraphVertex(self.graph1, 'Y')
        self.v5_g1 = graph_vertex.UnWeightedGraphVertex(self.graph1, 'Z')

        self.graph1.add_vertex(self.v1_g1)
        self.graph1.add_vertex(self.v2_g1)
        self.graph1.add_vertex(self.v3_g1)
        self.graph1.add_vertex(self.v4_g1)
        self.graph1.add_vertex(self.v5_g1)

        self.e12 = graph_edge.DirectedUnWeightedGraphEdge(
            self.graph1, self.v1_g1, self.v2_g1)  # S -> T
        self.e14 = graph_edge.DirectedUnWeightedGraphEdge(
            self.graph1, self.v1_g1, self.v4_g1)  # S -> Y
        self.e23 = graph_edge.DirectedUnWeightedGraphEdge(
            self.graph1, self.v2_g1, self.v3_g1)  # T -> X
        self.e24 = graph_edge.DirectedUnWeightedGraphEdge(
            self.graph1, self.v2_g1, self.v4_g1)  # T -> Y
        self.e35 = graph_edge.DirectedUnWeightedGraphEdge(
            self.graph1, self.v3_g1, self.v5_g1)  # X -> Z
        self.e42 = graph_edge.DirectedUnWeightedGraphEdge(
            self.graph1, self.v4_g1, self.v2_g1)  # Y -> T
        self.e43 = graph_edge.DirectedUnWeightedGraphEdge(
            self.graph1, self.v4_g1, self.v3_g1)  # Y -> X
        self.e45 = graph_edge.DirectedUnWeightedGraphEdge(
            self.graph1, self.v4_g1, self.v5_g1)  # Y -> Z
        self.e53 = graph_edge.DirectedUnWeightedGraphEdge(
            self.graph1, self.v5_g1, self.v3_g1)  # Z -> X
        self.e51 = graph_edge.DirectedUnWeightedGraphEdge(
            self.graph1, self.v5_g1, self.v1_g1)  # Z -> S

        self.graph1.add_edge(self.v1_g1, self.v2_g1)
        self.graph1.add_edge(self.v1_g1, self.v4_g1)
        self.graph1.add_edge(self.v2_g1, self.v3_g1)
        self.graph1.add_edge(self.v2_g1, self.v4_g1)
        self.graph1.add_edge(self.v3_g1, self.v5_g1)
        self.graph1.add_edge(self.v4_g1, self.v2_g1)
        self.graph1.add_edge(self.v4_g1, self.v3_g1)
        self.graph1.add_edge(self.v4_g1, self.v5_g1)
        self.graph1.add_edge(self.v5_g1, self.v3_g1)
        self.graph1.add_edge(self.v5_g1, self.v1_g1)

        # Create directed unweighted acyclic graph Bruno R. Preiss - Java - page 563.
        self.graph2 = graph.DirectedUnWeightedGraph(9)

        self.v0_g2 = graph_vertex.UnWeightedGraphVertex(self.graph2, 'a')
        self.v1_g2 = graph_vertex.UnWeightedGraphVertex(self.graph2, 'b')
        self.v2_g2 = graph_vertex.UnWeightedGraphVertex(self.graph2, 'c')
        self.v3_g2 = graph_vertex.UnWeightedGraphVertex(self.graph2, 'd')
        self.v4_g2 = graph_vertex.UnWeightedGraphVertex(self.graph2, 'e')
        self.v5_g2 = graph_vertex.UnWeightedGraphVertex(self.graph2, 'f')
        self.v6_g2 = graph_vertex.UnWeightedGraphVertex(self.graph2, 'g')
        self.v7_g2 = graph_vertex.UnWeightedGraphVertex(self.graph2, 'h')
        self.v8_g2 = graph_vertex.UnWeightedGraphVertex(self.graph2, 'i')

        self.graph2.add_vertex(self.v0_g2)
        self.graph2.add_vertex(self.v1_g2)
        self.graph2.add_vertex(self.v2_g2)
        self.graph2.add_vertex(self.v3_g2)
        self.graph2.add_vertex(self.v4_g2)
        self.graph2.add_vertex(self.v5_g2)
        self.graph2.add_vertex(self.v6_g2)
        self.graph2.add_vertex(self.v7_g2)
        self.graph2.add_vertex(self.v8_g2)

        self.e01_g2 = graph_edge.DirectedUnWeightedGraphEdge(
            self.graph2, self.v0_g2, self.v1_g2)
        self.e02_g2 = graph_edge.DirectedUnWeightedGraphEdge(
            self.graph2, self.v0_g2, self.v2_g2)
        self.e04_g2 = graph_edge.DirectedUnWeightedGraphEdge(
            self.graph2, self.v0_g2, self.v4_g2)
        self.e13_g2 = graph_edge.DirectedUnWeightedGraphEdge(
            self.graph2, self.v1_g2, self.v3_g2)
        self.e14_g2 = graph_edge.DirectedUnWeightedGraphEdge(
            self.graph2, self.v1_g2, self.v4_g2)
        self.e27_g2 = graph_edge.DirectedUnWeightedGraphEdge(
            self.graph2, self.v2_g2, self.v7_g2)
        self.e25_g2 = graph_edge.DirectedUnWeightedGraphEdge(
            self.graph2, self.v2_g2, self.v5_g2)
        self.e36_g2 = graph_edge.DirectedUnWeightedGraphEdge(
            self.graph2, self.v3_g2, self.v6_g2)
        self.e46_g2 = graph_edge.DirectedUnWeightedGraphEdge(
            self.graph2, self.v4_g2, self.v6_g2)
        self.e48_g2 = graph_edge.DirectedUnWeightedGraphEdge(
            self.graph2, self.v4_g2, self.v8_g2)
        self.e47_g2 = graph_edge.DirectedUnWeightedGraphEdge(
            self.graph2, self.v4_g2, self.v7_g2)
        self.e57_g2 = graph_edge.DirectedUnWeightedGraphEdge(
            self.graph2, self.v5_g2, self.v7_g2)
        self.e68_g2 = graph_edge.DirectedUnWeightedGraphEdge(
            self.graph2, self.v6_g2, self.v8_g2)
        self.e78_g2 = graph_edge.DirectedUnWeightedGraphEdge(
            self.graph2, self.v7_g2, self.v8_g2)

        self.graph2.add_edge(self.v0_g2, self.v1_g2)  # a -> b
        self.graph2.add_edge(self.v0_g2, self.v2_g2)  # a -> c
        self.graph2.add_edge(self.v0_g2, self.v4_g2)  # a -> e
        self.graph2.add_edge(self.v1_g2, self.v3_g2)  # b -> d
        self.graph2.add_edge(self.v1_g2, self.v4_g2)  # b -> e
        self.graph2.add_edge(self.v2_g2, self.v7_g2)  # c -> h
        self.graph2.add_edge(self.v2_g2, self.v5_g2)  # c -> f
        self.graph2.add_edge(self.v3_g2, self.v6_g2)  # d -> g
        self.graph2.add_edge(self.v4_g2, self.v6_g2)  # e -> g
        self.graph2.add_edge(self.v4_g2, self.v8_g2)  # e -> i
        self.graph2.add_edge(self.v4_g2, self.v7_g2)  # e -> h
        self.graph2.add_edge(self.v5_g2, self.v7_g2)  # f -> h
        self.graph2.add_edge(self.v6_g2, self.v8_g2)  # g -> i
        self.graph2.add_edge(self.v7_g2, self.v8_g2)  # h -> i