Пример #1
0
 def test_GivenAGraphWithNoCycleThenAlgorithmShouldReturnFalse(self):
     graph = Graph()
     matric = [[0, 1, 0, 0], [1, 0, 1, 0], [0, 1, 0, 1], [0, 0, 1, 0]]
     graph.create(matric, 4, False)
     cycle_detection = UndirectedCycleDetection()
     is_cycle = cycle_detection.run(graph)
     assert_that(is_cycle, equal_to(False))
Пример #2
0
class TestGraph:
    def setup_method(self, method):
        self.__graph = Graph()
        self.__matrix = [[0, 1, 1, 1], [1, 0, 0, 1], [1, 0, 0, 1],
                         [1, 1, 1, 0]]

    def test_GivenAnUndirectedGraphMatrixThenCheckVertices(self):
        vertices = self.__graph.create(self.__matrix, 4, False)
        assert_that(len(vertices[0].edges), equal_to(3))
        assert_that(len(vertices[1].edges), equal_to(2))
        assert_that(len(vertices[2].edges), equal_to(2))
        assert_that(len(vertices[3].edges), equal_to(3))

    def test_GivenADirectedGraphMatrixThenCheckVertices(self):
        self.__graph.create(self.__matrix, 4, True)
        edges = self.__graph.edges
        counter = 0
        for row in range(4):
            for col in range(4):
                if self.__matrix[row][col] != 0:
                    assert_that(edges[counter].source.ID, equal_to(row))
                    assert_that(edges[counter].destination.ID, equal_to(col))
                    assert_that(edges[counter].weight,
                                equal_to(self.__matrix[row][col]))
                    counter = counter + 1

    def test_GiveAGraphWhenCallGetMatrixThenItShouldReturnGraphMatrix(self):
        self.__graph.create(self.__matrix, 4, False)
        assert_that(self.__graph.get_matrix(), equal_to(self.__matrix))
Пример #3
0
 def test_GivenAGraphToAlgorithmThenCheckTheOutput(self):
     graph = Graph()
     matric = [[0, 1, 1, 0, 0, 0], [1, 0, 0, 1, 1, 0], [1, 0, 0, 0, 0, 1],
               [0, 1, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0]]
     graph.create(matric, 6, False)
     bfs = BFS()
     output_vertices = bfs.run(graph)
     vertices_id = list()
     for vertex in output_vertices:
         vertices_id.append(vertex.ID)
     assert_that(vertices_id, equal_to([0, 1, 2, 3, 4, 5]))
Пример #4
0
 def test_GivenAGraphWithNegativeCycleThenAlgorithmShouldReturnNone(self):
     graph = Graph()
     matric = [[0, 3, 5, 0, 0, 0], [0, 0, 2, 0, 0, 0], [0, 0, 0, -6, 2, 0],
               [0, 3, 0, 0, 0, 4], [0, 0, 0, 0, 0, 3], [0, 0, 0, 0, 0, 0]]
     graph.create(matric, 6, True)
     dijkstra = Bellman_Ford()
     try:
         output = dijkstra.run(graph, graph.vertices[0], graph.vertices[5])
         assert False
     except ValueError as error:
         assert_that(error.args[0], equal_to("Negative Cycle Exists!"))
Пример #5
0
 def test_GivenAGraphAndSourceAndDestinationVertexThenAlgorihtmShouldReturnShortestPath(
         self):
     graph = Graph()
     matric = [[0, 3, 5, 0, 0, 0], [0, 0, 8, 0, 0, 0], [0, 0, 0, -3, 2, 0],
               [0, 0, 0, 0, 0, 4], [0, 0, 0, 0, 0, 3], [0, 0, 0, 0, 0, 0]]
     graph.create(matric, 6, True)
     dijkstra = Bellman_Ford()
     output = dijkstra.run(graph, graph.vertices[0], graph.vertices[5])
     path_weight = output['distance']
     vertices_id = list()
     for vertex in output['vertices']:
         vertices_id.append(vertex.ID)
     assert_that(vertices_id, equal_to([0, 2, 3, 5]))
 def test_GiveAGraphThenAlgorithmShouldReturnTheMinimumSpanningTree(self):
     graph = Graph()
     matric = [[0, 2, 6, 0, 5, 10, 0], [2, 0, 0, 3, 3, 0, 0],
               [6, 0, 0, 1, 0, 2, 0], [0, 3, 1, 0, 4, 0, 5],
               [5, 3, 0, 4, 0, 0, 0], [10, 0, 2, 0, 0, 0, 3],
               [0, 0, 0, 5, 0, 5, 0]]
     graph.create(matric, 7, False)
     prim_jarnik = Prim_Jarnik()
     tree = prim_jarnik.run(graph)
     assert_that(
         tree.get_matrix(),
         equal_to([[0, 2, 0, 0, 0, 0, 0], [2, 0, 0, 3, 3, 0, 0],
                   [0, 0, 0, 1, 0, 2, 0], [0, 3, 1, 0, 0, 0, 0],
                   [0, 3, 0, 0, 0, 0, 0], [0, 0, 2, 0, 0, 0, 3],
                   [0, 0, 0, 0, 0, 3, 0]]))