Пример #1
0
    def test_get_edges(self):
        graph = Graph()

        # Create test Verticies
        v1,v2,v3 = Vertex("a"), Vertex("b"), Vertex("c")

        # Add verticies
        graph.add_vertex(v1)
        graph.add_vertex(v2)
        graph.add_vertex(v3)

        self.assertEqual(graph.numVertices, 3)
        self.assertEqual(graph.numEdges, 0)

        # Create edges
        edges = [
            ("a", "b", 10),
            ("b", "c", 10),
            ("c", "a", 4)
        ]

        # Iterate through edges
        for edge in edges:
            fromVert, toVert, weight = edge
            graph.add_edge(fromVert, toVert, weight)

        self.assertEqual(graph.numEdges, 3)
Пример #2
0
    def setUp(self):
        self.graph = Graph()

        self.populated_graph = Graph()
        # Add vertices
        self.populated_graph.add_vertex("A")
        self.populated_graph.add_vertex("B")
        self.populated_graph.add_vertex("C")
        self.populated_graph.add_vertex("D")
        self.populated_graph.add_vertex("E")
        self.populated_graph.add_vertex("F")
        self.populated_graph.add_vertex("G")

        # Add connections (non weighted edges for now)
        self.populated_graph.add_edge("A", "B", 4)  # (A -> B)
        self.populated_graph.add_edge("A", "C", 6)  # (A -> C)
        self.populated_graph.add_edge("B", "C", 8)  # (B -> C)
        self.populated_graph.add_edge("C", "D", 9)  # (C -> D)
        self.populated_graph.add_edge("C", "E", 1)  # (C -> E)
        self.populated_graph.add_edge("C", "F", 90)  # (C -> F)
        self.populated_graph.add_edge("A", "F", 12)  # (A -> F)
Пример #3
0
    def text_get_verticies(self):
        graph = Graph()

        # Create test Verticies
        v1,v2,v3 = Vertex("a"), Vertex("b"), Vertex("c")

        # Add verticies
        graph.add_vertex(v1)
        graph.add_vertex(v2)
        graph.add_vertex(v3)

        self.assertListEqual(graph.get_vertices, ["a","b","c"])
Пример #4
0
    def test_shortest_path(self):
        g = Graph()
        g.addVertex(1)
        g.addVertex(2)
        g.addVertex(3)
        g.addVertex(4)
        g.addVertex(5)

        g.addEdge(1, 2)
        g.addEdge(1, 4)
        g.addEdge(2, 3)
        g.addEdge(2, 4)
        g.addEdge(2, 5)
        g.addEdge(3, 5)

        is_path, path = g.DFS_recursive(1, 5)

        self.assertEqual(4, len(path))
Пример #5
0
    def test_add_vertex(self):
        graph = Graph()

        # Creates test Verticies
        v1,v2,v3 = Vertex("a"), Vertex("b"), Vertex("c")

        # Add vertex "a"
        graph.add_vertex(v1)
        self.assertEqual(graph.numVertices, 1)
        self.assertEqual(graph.numEdges, 0)

        # Add vertex "b"
        graph.add_vertex(v2)
        self.assertEqual(graph.numVertices, 2)
        self.assertEqual(graph.numEdges, 0)

        # Add vertex "c"
        graph.add_vertex(v3)
        self.assertEqual(graph.numVertices, 3)
        self.assertEqual(graph.numEdges, 0)
Пример #6
0
 def test_init(self):
     # Tests the initialization of the Graph class
     graph = Graph()
     self.assertDictEqual(graph.vertList, {})
     self.assertEqual(graph.numVertices, 0)
     self.assertEqual(graph.numEdges, 0)