Exemplo n.º 1
0
    def test_un_directed_weighted_graph_remove_vertex_v0(self):
        """
        Test method "remove_vertex".
        """
        # https://reference.wolfram.com/mathematica/ref/VertexDelete.html
        # Create a graph from where a vertex should be removed.
        a_graph = graph.UnDirectedWeightedGraph(5)
        v_0 = graph_vertex.UnWeightedGraphVertex(a_graph, 'A')
        v_1 = graph_vertex.UnWeightedGraphVertex(a_graph, 'B')
        v_2 = graph_vertex.UnWeightedGraphVertex(a_graph, 'C')
        v_3 = graph_vertex.UnWeightedGraphVertex(a_graph, 'D')
        v_4 = graph_vertex.UnWeightedGraphVertex(a_graph, 'E')

        # Add vertices to the graph.
        a_graph.add_vertex(v_0)
        a_graph.add_vertex(v_1)
        a_graph.add_vertex(v_2)
        a_graph.add_vertex(v_3)
        a_graph.add_vertex(v_4)

        # Add edges to the graph.
        a_graph.add_edge(v_0, v_1, 10)
        a_graph.add_edge(v_0, v_2, 20)
        a_graph.add_edge(v_0, v_3, 30)
        a_graph.add_edge(v_0, v_4, 40)
        a_graph.add_edge(v_1, v_2, 50)
        a_graph.add_edge(v_1, v_3, 60)
        a_graph.add_edge(v_1, v_4, 70)
        a_graph.add_edge(v_2, v_3, 80)
        a_graph.add_edge(v_2, v_4, 90)
        a_graph.add_edge(v_3, v_4, 100)

        # Create a reference graph used to compare the result after a vertex has been removed.
        g_ref = graph.UnDirectedWeightedGraph(4)

        # Create reference vertices.
        v1_ref = graph_vertex.UnWeightedGraphVertex(g_ref, 'B')
        v2_ref = graph_vertex.UnWeightedGraphVertex(g_ref, 'C')
        v3_ref = graph_vertex.UnWeightedGraphVertex(g_ref, 'D')
        v4_ref = graph_vertex.UnWeightedGraphVertex(g_ref, 'E')

        # Add vertices to the reference graph.
        g_ref.add_vertex(v1_ref)
        g_ref.add_vertex(v2_ref)
        g_ref.add_vertex(v3_ref)
        g_ref.add_vertex(v4_ref)

        # Add edges to the reference graph.
        g_ref.add_edge(v1_ref, v2_ref, 50)
        g_ref.add_edge(v1_ref, v3_ref, 60)
        g_ref.add_edge(v1_ref, v4_ref, 70)
        g_ref.add_edge(v2_ref, v3_ref, 80)
        g_ref.add_edge(v2_ref, v4_ref, 90)
        g_ref.add_edge(v3_ref, v4_ref, 100)

        # Remove vertex form graph.
        a_graph.remove_vertex(v_0)
        self.assertEqual(g_ref, a_graph)
Exemplo n.º 2
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)
Exemplo n.º 3
0
    def test_un_directed_weighted_graph_copy(self):
        """
        Test operator "copy".
        """
        a_graph = graph.UnDirectedWeightedGraph(5)
        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')
        v_5 = graph_vertex.UnWeightedGraphVertex(a_graph, 'E')

        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_vertex(v_5)

        a_graph.add_edge(v_1, v_2, 1)
        a_graph.add_edge(v_1, v_3, 2)
        a_graph.add_edge(v_1, v_4, 3)
        a_graph.add_edge(v_1, v_5, 4)
        a_graph.add_edge(v_2, v_3, 5)
        a_graph.add_edge(v_2, v_4, 6)
        a_graph.add_edge(v_2, v_5, 7)
        a_graph.add_edge(v_3, v_4, 8)
        a_graph.add_edge(v_3, v_5, 9)
        a_graph.add_edge(v_4, v_5, 10)
        ref = copy.copy(a_graph)
        self.assertEqual(a_graph, ref)
Exemplo n.º 4
0
 def test_un_directed_weighted_graph_has_vertex(self):
     """
     Test method "has_vertex".
     """
     a_graph = graph.UnDirectedWeightedGraph(1)
     a_vertex = graph_vertex.UnWeightedGraphVertex(a_graph, 'A')
     a_graph.add_vertex(a_vertex)
     self.assertTrue(a_graph.has_vertex(a_vertex))
Exemplo n.º 5
0
 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))
Exemplo n.º 6
0
    def test_un_directed_weighted_graph_is_cyclic_not(self):
        """
        Test method "is_cyclic" - inverted.
        """
        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, 1)
        a_graph.add_edge(v_2, v_3, 2)
        a_graph.add_edge(v_2, v_4, 3)
        self.assertFalse(a_graph.is_cyclic())
Exemplo n.º 7
0
    def test_un_directed_weighted_graph_remove_edge(self):
        """
        Test method "remove_edge".
        """
        # https://reference.wolfram.com/mathematica/ref/VertexDelete.html
        a_graph = graph.UnDirectedWeightedGraph(5)
        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')
        v_5 = graph_vertex.UnWeightedGraphVertex(a_graph, 'E')

        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_vertex(v_5)

        a_graph.add_edge(v_1, v_2, 10)
        a_graph.add_edge(v_1, v_3, 20)
        a_graph.add_edge(v_1, v_4, 30)
        a_graph.add_edge(v_1, v_5, 40)
        a_graph.add_edge(v_2, v_3, 50)
        a_graph.add_edge(v_2, v_4, 60)
        a_graph.add_edge(v_2, v_5, 70)
        a_graph.add_edge(v_3, v_4, 80)
        a_graph.add_edge(v_3, v_5, 90)
        a_graph.add_edge(v_4, v_5, 100)

        a_graph.remove_edge(v_1, v_2)
        a_graph.remove_edge(v_1, v_3)
        a_graph.remove_edge(v_1, v_4)
        a_graph.remove_edge(v_1, v_5)
        a_graph.remove_edge(v_2, v_3)
        a_graph.remove_edge(v_2, v_4)
        a_graph.remove_edge(v_2, v_5)
        a_graph.remove_edge(v_3, v_4)
        a_graph.remove_edge(v_3, v_5)
        a_graph.remove_edge(v_4, v_5)
        ref = []
        res = a_graph.get_edges()
        self.assertEqual(ref, res)
Exemplo n.º 8
0
    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)
    def setUp(self):
        # Graph from http://en.wikipedia.org/wiki/Prim%27s_algorithm
        self.graph1 = graph.UnDirectedWeightedGraph(7)

        self.v1_g1 = graph_vertex.UnWeightedGraphVertex(self.graph1, "A")
        self.v2_g1 = graph_vertex.UnWeightedGraphVertex(self.graph1, "B")
        self.v3_g1 = graph_vertex.UnWeightedGraphVertex(self.graph1, "C")
        self.v4_g1 = graph_vertex.UnWeightedGraphVertex(self.graph1, "D")
        self.v5_g1 = graph_vertex.UnWeightedGraphVertex(self.graph1, "E")
        self.v6_g1 = graph_vertex.UnWeightedGraphVertex(self.graph1, "F")
        self.v7_g1 = graph_vertex.UnWeightedGraphVertex(self.graph1, "G")

        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.graph1.add_vertex(self.v6_g1)
        self.graph1.add_vertex(self.v7_g1)

        self.graph1.add_edge(self.v1_g1, self.v2_g1, 7)  # ( A - B, 7 )
        self.graph1.add_edge(self.v1_g1, self.v4_g1, 5)  # ( A - D, 5 )
        self.graph1.add_edge(self.v2_g1, self.v3_g1, 8)  # ( B - C, 8 )
        self.graph1.add_edge(self.v2_g1, self.v4_g1, 9)  # ( B - D, 9 )
        self.graph1.add_edge(self.v2_g1, self.v5_g1, 7)  # ( B - E, 7 )
        self.graph1.add_edge(self.v3_g1, self.v5_g1, 5)  # ( C - E, 5 )
        self.graph1.add_edge(self.v4_g1, self.v5_g1, 15)  # ( D - E, 15 )
        self.graph1.add_edge(self.v4_g1, self.v6_g1, 6)  # ( D - F, 6 )
        self.graph1.add_edge(self.v5_g1, self.v6_g1, 8)  # ( E - F, 8 )
        self.graph1.add_edge(self.v5_g1, self.v7_g1, 9)  # ( E - G, 9 )
        self.graph1.add_edge(self.v6_g1, self.v7_g1, 11)  # ( F - G, 11 )

        # Directed weighted graph from:
        # http://compalg.inf.elte.hu/~tony/Oktatas/TDK/FINAL/Chap%2013.PDF
        self.graph2 = graph.DirectedWeightedGraph(7)

        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.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_edge(self.v0_g2, self.v1_g2, 7)  # ( A - B, 7 )
        self.graph2.add_edge(self.v1_g2, self.v2_g2, 2)  # ( B - C, 2 )
        self.graph2.add_edge(self.v1_g2, self.v6_g2, 3)  # ( B - G, 3 )
        self.graph2.add_edge(self.v2_g2, self.v3_g2, 2)  # ( C - D, 2 )
        self.graph2.add_edge(self.v2_g2, self.v6_g2, 4)  # ( C - G, 4 )
        self.graph2.add_edge(self.v3_g2, self.v4_g2, 5)  # ( D - E, 5 )
        self.graph2.add_edge(self.v3_g2, self.v6_g2, 1)  # ( D - G, 5 )
        self.graph2.add_edge(self.v4_g2, self.v5_g2, 6)  # ( E - F, 6 )
        self.graph2.add_edge(self.v5_g2, self.v0_g2, 1)  # ( F - A, 1 )
        self.graph2.add_edge(self.v5_g2, self.v6_g2, 4)  # ( F - G, 4 )
        self.graph2.add_edge(self.v6_g2, self.v0_g2, 7)  # ( G - A, 7 )
        self.graph2.add_edge(self.v6_g2, self.v4_g2, 1)  # ( G - E, 1 )