Exemple #1
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)
Exemple #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)
    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_directed_weighted_graph_is_cyclic(self):
        """
        Test method "is_cyclic".
        """
        # Create the cyclic graph shown below:
        #
        # A---------->----------B
        # |                     |
        # |                     |
        # |                     |
        # ^                     V
        # |                     |
        # |                     |
        # |                     |
        # D----------<----------C

        a_graph = graph.DirectedWeightedGraph(4)
        vertex_a = graph_vertex.UnWeightedGraphVertex(a_graph, 'A')
        vertex_b = graph_vertex.UnWeightedGraphVertex(a_graph, 'B')
        vertex_c = graph_vertex.UnWeightedGraphVertex(a_graph, 'C')
        vertex_d = graph_vertex.UnWeightedGraphVertex(a_graph, 'D')

        a_graph.add_vertex(vertex_a)
        a_graph.add_vertex(vertex_b)
        a_graph.add_vertex(vertex_c)
        a_graph.add_vertex(vertex_d)

        a_graph.add_edge(vertex_a, vertex_b, 1)
        a_graph.add_edge(vertex_b, vertex_c, 2)
        a_graph.add_edge(vertex_c, vertex_d, 3)
        a_graph.add_edge(vertex_d, vertex_a, 4)
        self.assertTrue(a_graph.is_cyclic())
    def test_directed_un_weighted_graph_copy(self):
        """
        Test operator "copy".
        """
        a_graph = graph.DirectedUnWeightedGraph(5)
        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')
        vertex5 = graph_vertex.UnWeightedGraphVertex(a_graph, 'E')

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

        a_graph.add_edge(vertex1, vertex2)
        a_graph.add_edge(vertex1, vertex3)
        a_graph.add_edge(vertex1, vertex4)
        a_graph.add_edge(vertex1, vertex5)
        a_graph.add_edge(vertex2, vertex3)
        a_graph.add_edge(vertex2, vertex4)
        a_graph.add_edge(vertex2, vertex5)
        a_graph.add_edge(vertex3, vertex4)
        a_graph.add_edge(vertex3, vertex5)
        a_graph.add_edge(vertex4, vertex5)
        ref = copy.copy(a_graph)
        self.assertEqual(a_graph, ref)
    def test_directed_graph_copy(self):
        """
        Test operator "copy".
        """
        graph1 = graph.DirectedGraph(5)
        vertex1 = graph_vertex.UnWeightedGraphVertex(graph1, 'A')
        vertex2 = graph_vertex.UnWeightedGraphVertex(graph1, 'B')
        vertex3 = graph_vertex.UnWeightedGraphVertex(graph1, 'C')
        vertex4 = graph_vertex.UnWeightedGraphVertex(graph1, 'D')
        vertex5 = graph_vertex.UnWeightedGraphVertex(graph1, 'E')

        graph1.add_vertex(vertex1)
        graph1.add_vertex(vertex2)
        graph1.add_vertex(vertex3)
        graph1.add_vertex(vertex4)
        graph1.add_vertex(vertex5)

        graph1.add_edge(vertex1, vertex2)
        graph1.add_edge(vertex1, vertex3)
        graph1.add_edge(vertex1, vertex4)
        graph1.add_edge(vertex1, vertex5)
        graph1.add_edge(vertex2, vertex3)
        graph1.add_edge(vertex2, vertex4)
        graph1.add_edge(vertex2, vertex5)
        graph1.add_edge(vertex3, vertex4)
        graph1.add_edge(vertex3, vertex5)
        graph1.add_edge(vertex4, vertex5)
        ref = copy.copy(graph1)
        self.assertEqual(graph1, ref)
    def test_directed_un_weighted_graph_is_cyclic_not(self):
        """
        Test method "is_cyclic" - inverted.
        """
        # Create the acyclic graph shown below:
        #
        # A----------<----------B
        # |                     |
        # |                     |
        # |                     |
        # ^                     ^
        # |                     |
        # |                     |
        # |                     |
        # D----------<----------C

        a_graph = graph.DirectedUnWeightedGraph(4)

        v_a = graph_vertex.UnWeightedGraphVertex(a_graph, 'A')
        v_b = graph_vertex.UnWeightedGraphVertex(a_graph, 'B')
        v_c = graph_vertex.UnWeightedGraphVertex(a_graph, 'C')
        v_d = graph_vertex.UnWeightedGraphVertex(a_graph, 'D')

        a_graph.add_vertex(v_a)
        a_graph.add_vertex(v_b)
        a_graph.add_vertex(v_c)
        a_graph.add_vertex(v_d)

        a_graph.add_edge(v_b, v_a)
        a_graph.add_edge(v_d, v_a)
        a_graph.add_edge(v_c, v_b)
        a_graph.add_edge(v_c, v_d)
        self.assertFalse(a_graph.is_cyclic())
    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)
Exemple #9
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)
 def test_directed_graph_has_edge_not(self):
     """
     Test method "has_edge" - inverted.
     """
     a_vertex = graph_vertex.UnWeightedGraphVertex(self.graph1, 'X')
     b_vertex = graph_vertex.UnWeightedGraphVertex(self.graph1, 'Y')
     a_edge = graph_edge.DirectedGraphEdge(self.graph1, a_vertex, b_vertex)
     self.assertFalse(self.graph1.has_edge(a_edge))
Exemple #11
0
 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))
Exemple #12
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)
 def test_directed_graph_has_edge(self):
     """
     Test method "has_edge".
     """
     a_graph = graph.DirectedGraph(2)
     a_vertex = graph_vertex.UnWeightedGraphVertex(a_graph, 'S')
     b_vertex = graph_vertex.UnWeightedGraphVertex(a_graph, 'T')
     a_graph.add_vertex(a_vertex)
     a_graph.add_vertex(b_vertex)
     a_edge = graph_edge.DirectedGraphEdge(a_graph, a_vertex, b_vertex)
     a_graph.add_edge(a_vertex, b_vertex)
     self.assertTrue(a_graph.has_edge(a_edge))
Exemple #14
0
    def test_un_directed_graph_remove_vertex_v_4(self):
        """
        Test method "remove_vertex".
        """
        # Create a graph from where a vertex should be removed.
        a_graph = graph.UnDirectedGraph(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)
        a_graph.add_edge(v_0, v_2)
        a_graph.add_edge(v_0, v_3)
        a_graph.add_edge(v_0, v_4)
        a_graph.add_edge(v_1, v_2)
        a_graph.add_edge(v_1, v_3)
        a_graph.add_edge(v_1, v_4)
        a_graph.add_edge(v_2, v_3)
        a_graph.add_edge(v_2, v_4)
        a_graph.add_edge(v_3, v_4)

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

        # Create reference vertices.
        v_0_ref = graph_vertex.UnWeightedGraphVertex(g_ref, 'A')
        v_1_ref = graph_vertex.UnWeightedGraphVertex(g_ref, 'B')
        v_2_ref = graph_vertex.UnWeightedGraphVertex(g_ref, 'C')
        v_3_ref = graph_vertex.UnWeightedGraphVertex(g_ref, 'D')

        # Add vertices to the reference graph.
        g_ref.add_vertex(v_0_ref)
        g_ref.add_vertex(v_1_ref)
        g_ref.add_vertex(v_2_ref)
        g_ref.add_vertex(v_3_ref)

        # Add edges to the reference graph.
        g_ref.add_edge(v_0_ref, v_1_ref)
        g_ref.add_edge(v_0_ref, v_2_ref)
        g_ref.add_edge(v_0_ref, v_3_ref)
        g_ref.add_edge(v_1_ref, v_2_ref)
        g_ref.add_edge(v_1_ref, v_3_ref)
        g_ref.add_edge(v_2_ref, v_3_ref)

        # Remove vertex form graph.
        a_graph.remove_vertex(v_4)
        self.assertEqual(g_ref, a_graph)
    def test_directed_un_weighted_graph_remove_vertex_v4(self):
        """
        Test method "remove_vertex".
        """
        # Create a graph from where a vertex should be removed.
        a_graph = graph.DirectedUnWeightedGraph(5)
        vertex0 = graph_vertex.UnWeightedGraphVertex(a_graph, 'A')
        vertex1 = graph_vertex.UnWeightedGraphVertex(a_graph, 'B')
        vertex2 = graph_vertex.UnWeightedGraphVertex(a_graph, 'C')
        vertex3 = graph_vertex.UnWeightedGraphVertex(a_graph, 'D')
        vertex4 = graph_vertex.UnWeightedGraphVertex(a_graph, 'E')

        # Add vertices to the graph.
        a_graph.add_vertex(vertex0)
        a_graph.add_vertex(vertex1)
        a_graph.add_vertex(vertex2)
        a_graph.add_vertex(vertex3)
        a_graph.add_vertex(vertex4)

        # Add edges to the graph.
        a_graph.add_edge(vertex0, vertex1)
        a_graph.add_edge(vertex0, vertex2)
        a_graph.add_edge(vertex0, vertex3)
        a_graph.add_edge(vertex0, vertex4)
        a_graph.add_edge(vertex1, vertex2)
        a_graph.add_edge(vertex1, vertex3)
        a_graph.add_edge(vertex1, vertex4)
        a_graph.add_edge(vertex2, vertex3)
        a_graph.add_edge(vertex2, vertex4)
        a_graph.add_edge(vertex3, vertex4)

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

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

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

        # Add edges to the reference graph.
        g_ref.add_edge(v0_ref, v1_ref)
        g_ref.add_edge(v0_ref, v2_ref)
        g_ref.add_edge(v0_ref, v3_ref)
        g_ref.add_edge(v1_ref, v2_ref)
        g_ref.add_edge(v1_ref, v3_ref)
        g_ref.add_edge(v2_ref, v3_ref)

        # Remove vertex form graph.
        a_graph.remove_vertex(vertex4)
        self.assertEqual(g_ref, a_graph)
Exemple #16
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))
    def test_graph_algorithms_bellman_ford(self):
        """
        Test of Belleman-Fords algorithm.
        """
        # Create directed weighted graph - reference: http://algs4.cs.princeton.edu
        test_graph = graph.DirectedWeightedGraph(8)

        vertex0 = graph_vertex.UnWeightedGraphVertex(test_graph, "0")
        vertex1 = graph_vertex.UnWeightedGraphVertex(test_graph, "1")
        vertex2 = graph_vertex.UnWeightedGraphVertex(test_graph, "2")
        vertex3 = graph_vertex.UnWeightedGraphVertex(test_graph, "3")
        vertex4 = graph_vertex.UnWeightedGraphVertex(test_graph, "4")
        vertex5 = graph_vertex.UnWeightedGraphVertex(test_graph, "5")
        vertex6 = graph_vertex.UnWeightedGraphVertex(test_graph, "6")
        vertex7 = graph_vertex.UnWeightedGraphVertex(test_graph, "7")

        test_graph.add_vertex(vertex0)
        test_graph.add_vertex(vertex1)
        test_graph.add_vertex(vertex2)
        test_graph.add_vertex(vertex3)
        test_graph.add_vertex(vertex4)
        test_graph.add_vertex(vertex5)
        test_graph.add_vertex(vertex6)
        test_graph.add_vertex(vertex7)

        test_graph.add_edge(vertex0, vertex1, 5)
        test_graph.add_edge(vertex0, vertex4, 9)
        test_graph.add_edge(vertex0, vertex7, 8)
        test_graph.add_edge(vertex1, vertex2, 12)
        test_graph.add_edge(vertex1, vertex3, 15)
        test_graph.add_edge(vertex1, vertex7, 4)
        test_graph.add_edge(vertex2, vertex3, 3)
        test_graph.add_edge(vertex2, vertex6, 11)
        test_graph.add_edge(vertex3, vertex6, 9)
        test_graph.add_edge(vertex4, vertex5, 4)
        test_graph.add_edge(vertex4, vertex6, 20)
        test_graph.add_edge(vertex4, vertex7, 5)
        test_graph.add_edge(vertex5, vertex2, 1)
        test_graph.add_edge(vertex5, vertex6, 13)
        test_graph.add_edge(vertex7, vertex2, 7)
        test_graph.add_edge(vertex7, vertex5, 6)

        # Find the shortest path from vertex v0 to all other vertices
        # using Bellman-Ford's algorithm.
        res_tuple = graph_algorithms.GraphAlgorithms.bellman_ford_algorithm(
            test_graph, vertex0)

        # Create a reference map holding the result.
        ref_map = {}
        ref_map[vertex7] = 8
        ref_map[vertex1] = 5
        ref_map[vertex2] = 14
        ref_map[vertex3] = 17
        ref_map[vertex6] = 25
        ref_map[vertex0] = 0
        ref_map[vertex5] = 13
        ref_map[vertex4] = 9
        ref_directed = False
        ref_tuple = (ref_directed, ref_map)
        self.assertEqual(ref_tuple, res_tuple)
Exemple #18
0
 def test_un_directed_graph_has_vertex_not(self):
     """
     Test method "has_vertex" - inverted.
     """
     a_graph = graph.UnDirectedGraph(1)
     a_vertex = graph_vertex.UnWeightedGraphVertex(a_graph, 'BB')
     self.assertFalse(a_graph.has_vertex(a_vertex))
Exemple #19
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))
Exemple #20
0
    def test_un_directed_un_weighted_graph_equal(self):
        """
        Test operator "equal".
        """
        a_graph = graph.UnDirectedUnWeightedGraph(5)
        ref = graph.UnDirectedUnWeightedGraph(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)

        ref.add_vertex(v_1)
        ref.add_vertex(v_2)
        ref.add_vertex(v_3)
        ref.add_vertex(v_4)
        ref.add_vertex(v_5)

        a_graph.add_edge(v_1, v_2)
        a_graph.add_edge(v_1, v_3)
        a_graph.add_edge(v_1, v_4)
        a_graph.add_edge(v_1, v_5)
        a_graph.add_edge(v_2, v_3)
        a_graph.add_edge(v_2, v_4)
        a_graph.add_edge(v_2, v_5)
        a_graph.add_edge(v_3, v_4)
        a_graph.add_edge(v_3, v_5)
        a_graph.add_edge(v_4, v_5)

        ref.add_edge(v_1, v_2)
        ref.add_edge(v_1, v_3)
        ref.add_edge(v_1, v_4)
        ref.add_edge(v_1, v_5)
        ref.add_edge(v_2, v_3)
        ref.add_edge(v_2, v_4)
        ref.add_edge(v_2, v_5)
        ref.add_edge(v_3, v_4)
        ref.add_edge(v_3, v_5)
        ref.add_edge(v_4, v_5)
        self.assertEqual(ref, a_graph)
Exemple #21
0
    def test_un_directed_un_weighted_graph_is_cyclic(self):
        """
        Test method "is_cyclic".
        """
        a_graph = graph.UnDirectedUnWeightedGraph(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)
        self.assertTrue(a_graph.is_cyclic())
Exemple #22
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())
    def setUp(self):
        # Create an undirected cyclic graph
        self.g_undirected_cyclic = graph.UnDirectedGraph(4)
        self.a_g_undirected_cyclic = graph_vertex.UnWeightedGraphVertex(self.g_undirected_cyclic, 'a')
        self.b_g_undirected_cyclic = graph_vertex.UnWeightedGraphVertex(self.g_undirected_cyclic, 'b')
        self.c_g_undirected_cyclic = graph_vertex.UnWeightedGraphVertex(self.g_undirected_cyclic, 'c')
        self.d_g_undirected_cyclic = graph_vertex.UnWeightedGraphVertex(self.g_undirected_cyclic, 'd')

        self.g_undirected_cyclic.add_vertex(self.a_g_undirected_cyclic)
        self.g_undirected_cyclic.add_vertex(self.b_g_undirected_cyclic)
        self.g_undirected_cyclic.add_vertex(self.c_g_undirected_cyclic)
        self.g_undirected_cyclic.add_vertex(self.d_g_undirected_cyclic)

        self.g_undirected_cyclic.add_edge(self.a_g_undirected_cyclic, self.b_g_undirected_cyclic)
        self.g_undirected_cyclic.add_edge(self.b_g_undirected_cyclic, self.c_g_undirected_cyclic)
        self.g_undirected_cyclic.add_edge(self.a_g_undirected_cyclic, self.c_g_undirected_cyclic)
        self.classification_undirected_cyclic = self.g_undirected_cyclic.classify_edges()

        # Create a directed cyclic graph (Cormen page 542)
        self.g_directed_cyclic = graph.DirectedGraph(6)
        self.u_g_directed_cyclic = graph_vertex.UnWeightedGraphVertex(self.g_directed_cyclic, 'u')
        self.v_g_directed_cyclic = graph_vertex.UnWeightedGraphVertex(self.g_directed_cyclic, 'v')
        self.w_g_directed_cyclic = graph_vertex.UnWeightedGraphVertex(self.g_directed_cyclic, 'w')
        self.x_g_directed_cyclic = graph_vertex.UnWeightedGraphVertex(self.g_directed_cyclic, 'x')
        self.y_g_directed_cyclic = graph_vertex.UnWeightedGraphVertex(self.g_directed_cyclic, 'y')
        self.z_g_directed_cyclic = graph_vertex.UnWeightedGraphVertex(self.g_directed_cyclic, 'z')

        self.g_directed_cyclic.add_vertex(self.u_g_directed_cyclic)
        self.g_directed_cyclic.add_vertex(self.v_g_directed_cyclic)
        self.g_directed_cyclic.add_vertex(self.w_g_directed_cyclic)
        self.g_directed_cyclic.add_vertex(self.x_g_directed_cyclic)
        self.g_directed_cyclic.add_vertex(self.y_g_directed_cyclic)
        self.g_directed_cyclic.add_vertex(self.z_g_directed_cyclic)

        self.g_directed_cyclic.add_edge(self.u_g_directed_cyclic, self.v_g_directed_cyclic)
        self.g_directed_cyclic.add_edge(self.u_g_directed_cyclic, self.x_g_directed_cyclic)
        self.g_directed_cyclic.add_edge(self.v_g_directed_cyclic, self.y_g_directed_cyclic)
        self.g_directed_cyclic.add_edge(self.w_g_directed_cyclic, self.y_g_directed_cyclic)
        self.g_directed_cyclic.add_edge(self.w_g_directed_cyclic, self.z_g_directed_cyclic)
        self.g_directed_cyclic.add_edge(self.x_g_directed_cyclic, self.v_g_directed_cyclic)
        self.g_directed_cyclic.add_edge(self.y_g_directed_cyclic, self.x_g_directed_cyclic)
        self.g_directed_cyclic.add_edge(self.z_g_directed_cyclic, self.z_g_directed_cyclic)
        self.classification_directed_cyclic = self.g_directed_cyclic.classify_edges()
Exemple #24
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)
Exemple #25
0
    def setUp(self):
        self.g_1 = graph.UnDirectedGraph(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.UnDirectedGraphEdge(self.g_1, self.v_1, self.v_2)
        self.e21 = graph_edge.UnDirectedGraphEdge(self.g_1, self.v_2, self.v_1)
        self.e14 = graph_edge.UnDirectedGraphEdge(self.g_1, self.v_1, self.v_4)
        self.e41 = graph_edge.UnDirectedGraphEdge(self.g_1, self.v_4, self.v_1)
        self.e23 = graph_edge.UnDirectedGraphEdge(self.g_1, self.v_2, self.v_3)
        self.e32 = graph_edge.UnDirectedGraphEdge(self.g_1, self.v_3, self.v_2)
        self.e24 = graph_edge.UnDirectedGraphEdge(self.g_1, self.v_2, self.v_4)
        self.e42 = graph_edge.UnDirectedGraphEdge(self.g_1, self.v_4, self.v_2)
        self.e25 = graph_edge.UnDirectedGraphEdge(self.g_1, self.v_2, self.v_5)
        self.e52 = graph_edge.UnDirectedGraphEdge(self.g_1, self.v_5, self.v_2)
        self.e35 = graph_edge.UnDirectedGraphEdge(self.g_1, self.v_3, self.v_5)
        self.e53 = graph_edge.UnDirectedGraphEdge(self.g_1, self.v_5, self.v_3)
        self.e45 = graph_edge.UnDirectedGraphEdge(self.g_1, self.v_4, self.v_5)
        self.e54 = graph_edge.UnDirectedGraphEdge(self.g_1, self.v_5, self.v_4)
        self.e46 = graph_edge.UnDirectedGraphEdge(self.g_1, self.v_4, self.v_6)
        self.e64 = graph_edge.UnDirectedGraphEdge(self.g_1, self.v_6, self.v_4)
        self.e56 = graph_edge.UnDirectedGraphEdge(self.g_1, self.v_5, self.v_6)
        self.e65 = graph_edge.UnDirectedGraphEdge(self.g_1, self.v_6, self.v_5)
        self.e57 = graph_edge.UnDirectedGraphEdge(self.g_1, self.v_5, self.v_7)
        self.e75 = graph_edge.UnDirectedGraphEdge(self.g_1, self.v_7, self.v_5)
        self.e67 = graph_edge.UnDirectedGraphEdge(self.g_1, self.v_6, self.v_7)
        self.e76 = graph_edge.UnDirectedGraphEdge(self.g_1, self.v_7, self.v_6)

        self.g_1.add_edge(self.v_1, self.v_2)
        self.g_1.add_edge(self.v_1, self.v_4)
        self.g_1.add_edge(self.v_2, self.v_3)
        self.g_1.add_edge(self.v_2, self.v_4)
        self.g_1.add_edge(self.v_2, self.v_5)
        self.g_1.add_edge(self.v_3, self.v_5)
        self.g_1.add_edge(self.v_4, self.v_5)
        self.g_1.add_edge(self.v_4, self.v_6)
        self.g_1.add_edge(self.v_5, self.v_6)
        self.g_1.add_edge(self.v_5, self.v_7)
        self.g_1.add_edge(self.v_6, self.v_7)
    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 )
Exemple #27
0
 def test_un_directed_weighted_graph_has_vertex_not(self):
     """
     Test method "has_vertex" - inverted.
     """
     a_vertex = graph_vertex.UnWeightedGraphVertex(self.g_1, 'X')
     self.assertFalse(self.g_1.has_vertex(a_vertex))
    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
    def setUp(self):
        # Create directed weighted graph Cormen page 596.
        self.graph1 = graph.DirectedWeightedGraph(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.DirectedWeightedGraphEdge(
            self.graph1, self.v1_g1, self.v2_g1, 10)
        self.e14 = graph_edge.DirectedWeightedGraphEdge(
            self.graph1, self.v1_g1, self.v4_g1, 5)
        self.e23 = graph_edge.DirectedWeightedGraphEdge(
            self.graph1, self.v2_g1, self.v3_g1, 1)
        self.e24 = graph_edge.DirectedWeightedGraphEdge(
            self.graph1, self.v2_g1, self.v4_g1, 2)
        self.e35 = graph_edge.DirectedWeightedGraphEdge(
            self.graph1, self.v3_g1, self.v5_g1, 4)
        self.e42 = graph_edge.DirectedWeightedGraphEdge(
            self.graph1, self.v4_g1, self.v2_g1, 3)
        self.e43 = graph_edge.DirectedWeightedGraphEdge(
            self.graph1, self.v4_g1, self.v3_g1, 9)
        self.e45 = graph_edge.DirectedWeightedGraphEdge(
            self.graph1, self.v4_g1, self.v5_g1, 2)
        self.e53 = graph_edge.DirectedWeightedGraphEdge(
            self.graph1, self.v5_g1, self.v3_g1, 6)
        self.e51 = graph_edge.DirectedWeightedGraphEdge(
            self.graph1, self.v5_g1, self.v1_g1, 7)

        self.graph1.add_edge(self.v1_g1, self.v2_g1, 10)  # (S -> T, 10)
        self.graph1.add_edge(self.v1_g1, self.v4_g1, 5)  # (S -> Y, 5)
        self.graph1.add_edge(self.v2_g1, self.v3_g1, 1)  # (T -> X, 1)
        self.graph1.add_edge(self.v2_g1, self.v4_g1, 2)  # (T -> Y, 2)
        self.graph1.add_edge(self.v3_g1, self.v5_g1, 4)  # (X -> Z, 4)
        self.graph1.add_edge(self.v4_g1, self.v2_g1, 3)  # (Y -> T, 3)
        self.graph1.add_edge(self.v4_g1, self.v3_g1, 9)  # (Y -> X, 9)
        self.graph1.add_edge(self.v4_g1, self.v5_g1, 2)  # (Y -> Z, 2)
        self.graph1.add_edge(self.v5_g1, self.v3_g1, 6)  # (Z -> X, 6)
        self.graph1.add_edge(self.v5_g1, self.v1_g1, 7)  # (Z -> S, 7)

        # Create directed unweighted acyclic graph Bruno R. Preiss - Java - page 563.
        self.graph2 = graph.DirectedWeightedGraph(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.DirectedWeightedGraphEdge(
            self.graph2, self.v0_g2, self.v1_g2, 10)
        self.e02_g2 = graph_edge.DirectedWeightedGraphEdge(
            self.graph2, self.v0_g2, self.v2_g2, 11)
        self.e04_g2 = graph_edge.DirectedWeightedGraphEdge(
            self.graph2, self.v0_g2, self.v4_g2, 12)
        self.e13_g2 = graph_edge.DirectedWeightedGraphEdge(
            self.graph2, self.v1_g2, self.v3_g2, 13)
        self.e14_g2 = graph_edge.DirectedWeightedGraphEdge(
            self.graph2, self.v1_g2, self.v4_g2, 14)
        self.e27_g2 = graph_edge.DirectedWeightedGraphEdge(
            self.graph2, self.v2_g2, self.v7_g2, 15)
        self.e25_g2 = graph_edge.DirectedWeightedGraphEdge(
            self.graph2, self.v2_g2, self.v5_g2, 16)
        self.e36_g2 = graph_edge.DirectedWeightedGraphEdge(
            self.graph2, self.v3_g2, self.v6_g2, 17)
        self.e46_g2 = graph_edge.DirectedWeightedGraphEdge(
            self.graph2, self.v4_g2, self.v6_g2, 18)
        self.e48_g2 = graph_edge.DirectedWeightedGraphEdge(
            self.graph2, self.v4_g2, self.v8_g2, 19)
        self.e47_g2 = graph_edge.DirectedWeightedGraphEdge(
            self.graph2, self.v4_g2, self.v7_g2, 20)
        self.e57_g2 = graph_edge.DirectedWeightedGraphEdge(
            self.graph2, self.v5_g2, self.v7_g2, 21)
        self.e68_g2 = graph_edge.DirectedWeightedGraphEdge(
            self.graph2, self.v6_g2, self.v8_g2, 22)
        self.e78_g2 = graph_edge.DirectedWeightedGraphEdge(
            self.graph2, self.v7_g2, self.v8_g2, 23)

        self.graph2.add_edge(self.v0_g2, self.v1_g2, 10)  # a -> b
        self.graph2.add_edge(self.v0_g2, self.v2_g2, 11)  # a -> c
        self.graph2.add_edge(self.v0_g2, self.v4_g2, 12)  # a -> e
        self.graph2.add_edge(self.v1_g2, self.v3_g2, 13)  # b -> d
        self.graph2.add_edge(self.v1_g2, self.v4_g2, 14)  # b -> e
        self.graph2.add_edge(self.v2_g2, self.v7_g2, 15)  # c -> h
        self.graph2.add_edge(self.v2_g2, self.v5_g2, 16)  # c -> f
        self.graph2.add_edge(self.v3_g2, self.v6_g2, 17)  # d -> g
        self.graph2.add_edge(self.v4_g2, self.v6_g2, 18)  # e -> g
        self.graph2.add_edge(self.v4_g2, self.v8_g2, 19)  # e -> i
        self.graph2.add_edge(self.v4_g2, self.v7_g2, 20)  # e -> h
        self.graph2.add_edge(self.v5_g2, self.v7_g2, 21)  # f -> h
        self.graph2.add_edge(self.v6_g2, self.v8_g2, 22)  # g -> i
        self.graph2.add_edge(self.v7_g2, self.v8_g2, 24)  # h -> i