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)
Beispiel #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)
    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"])
    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)
Beispiel #5
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))
Beispiel #6
0
class GraphTests(unittest.TestCase):
    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)

    def test_init(self):
        assert not self.graph.vertices_dict
        assert self.graph.num_vertices is 0

    def test_add_vertex(self):
        # Add one vertex
        self.graph.add_vertex("B")
        assert self.graph.num_vertices is 1
        assert len(self.graph.vertices_dict.keys()) is 1
        assert "B" in self.graph.vertices_dict

        # Add another vertex
        self.graph.add_vertex("A")
        assert self.graph.num_vertices is 2
        assert len(self.graph.vertices_dict.keys()) is 2
        assert "A" in self.graph.vertices_dict

        # Should avodata adding duplicate
        self.graph.add_vertex("B")
        assert self.graph.num_vertices is 2
        assert len(self.graph.vertices_dict.keys()) is 2

    def test_get_vertex(self):
        # Add vertex
        self.graph.add_vertex("B")

        # Find the newly added vertex
        found_vertex = self.graph.get_vertex("B")
        assert found_vertex.data is "B"

        # Test for non existing vertex
        assert self.graph.get_vertex("C") is None

    def test_add_edge(self):
        self.graph.undirected = True
        # Populate the graph with vertices A, B, C, D, E, F, G
        self.graph.add_vertex("A")
        self.graph.add_vertex("B")
        self.graph.add_vertex("C")
        self.graph.add_vertex("D")
        self.graph.add_vertex("E")
        self.graph.add_vertex("F")
        self.graph.add_vertex("G")

        # Add edge from A to B with the cost of 10
        self.graph.add_edge("A", "B", 10)
        vertex_a = self.graph.get_vertex("A")
        vertex_b = self.graph.get_vertex("B")
        vertex_c = self.graph.get_vertex("C")

        # Vertex A should have an edge to vertex B with the cost of 10
        assert "B" in vertex_a.neighbors
        assert vertex_a.get_edge_weight("B") is 10
        print(vertex_b.neighbors)
        assert "A" in vertex_b.neighbors
        assert vertex_b.get_edge_weight("A") is 10

        self.graph.add_edge("A", "C", 5)
        self.graph.add_edge("B", "C", 10)

        assert "C" in vertex_a.neighbors and "C" in vertex_b.neighbors
        assert vertex_a.get_edge_weight("C") is 5
        assert vertex_b.get_edge_weight("C") is 10
        assert vertex_c.get_edge_weight("A") is 5 and vertex_c.get_edge_weight(
            "B") is 10

        # Test for bad inputs
        assert self.graph.add_edge("O", "B", 12) is None

    def test_get_verticles(self):
        # Populate the graph with vertices A, B, C
        self.graph.add_vertex("B")
        self.graph.add_vertex("A")
        self.graph.add_vertex("C")

        vertices_list = self.graph.get_vertices()
        assert list
        assert len(vertices_list) is 3
        assert "A" in vertices_list
        assert "B" in vertices_list
        assert "C" in vertices_list

    def test_get_edges(self):
        # Populate the graph with vertices A, B, C
        self.graph.add_vertex("B")
        self.graph.add_vertex("A")
        self.graph.add_vertex("C")

        self.graph.add_edge("A", "B", 10)
        self.graph.add_edge("A", "C", 5)
        self.graph.add_edge("B", "C", 10)

        edges_set = self.graph.get_edges()

        assert len(edges_set) is 3
        assert ("A", "B", 10) in edges_set or ("B", "A", 10)
        assert ("A", "C", 5) in edges_set or ("C", "A", 5) in edges_set
        assert ("B", "C", 10) in edges_set or ("C", "B", 10) in edges_set

    def test_reading_file(self):
        filename = 'graph_data.txt'
        self.graph.read_file(filename)

        # D
        # 1, 2, 3, 4, 5
        # (1, 2)
        # (1, 4)
        # (2, 3)
        # (2, 4)
        # (2, 5)
        # (3, 5)
        # (5, 2)

        # Graph should have expected vertices
        expected_vertices = ['1', '2', '3', '4', '5']
        assert len(self.graph.get_vertices()) is 5

        for key in expected_vertices:
            assert key in self.graph.vertices_dict

        # Graph should have expected edges
        expected_edges = [
            ('1', '2', 0),
            ('1', '4', 0),
            ('2', '3', 0),
            ('2', '4', 0),
            ('2', '5', 0),
            ('3', '5', 0),
        ]
        assert len(self.graph.edges_list) is 6

        for edge in expected_edges:
            assert edge in self.graph.edges_list

    def test_depth_first_search(self):
        found, path = self.populated_graph.depth_first_search_iter('A', 'E')

        assert found is True
        assert len(path) is 5

        expected_keys = ['A', 'B', 'C', 'D', 'E']

        for key in expected_keys:
            assert key in expected_keys

        # Test bad input
        found, path = self.populated_graph.depth_first_search_iter('A', 'R')
        assert found is False
        assert len(path) is 0

        # Test edges cases
        # Can't find a path for a disjointed graph
        self.populated_graph.add_vertex('O')

        found, path = self.populated_graph.depth_first_search_iter('A', 'R')
        assert found is False
        assert len(path) is 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)