Exemple #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))
 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]))
Exemple #3
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!"))
Exemple #4
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]]))
 def run(self, graph):
     edges = graph.edges
     vertices = graph.vertices
     edges = self.__sort_edges(edges)
     self.__intial_joint_lists(vertices)
     tree_edges = list()
     tree_vertices = list()
     for edge in edges:
         if (len(self.__joint_lists) == 1):
             break
         if self.__joint(edge.source, edge.destination):
             tree_edges.append(edge)
     tree_graph = Graph()
     tree_graph.vertices = vertices.copy()
     tree_graph.edges = tree_edges.copy()
     return tree_graph
Exemple #7
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))
Exemple #8
0
    def run(self, graph):
        vertices = graph.vertices
        self.__initial_algorithm(vertices)
        for edge in vertices[3].edges:
            self.__queue.append(edge)

        tree_edges = list()
        counter = 0
        while False in self.__is_visited.values():
            self.__queue = sorted(self.__queue, key=lambda edge: edge.weight)
            min_edge = self.__queue.pop(0)
            tree_edges.append(min_edge)
            self.__is_visited[min_edge.source] = True
            self.__is_visited[min_edge.destination] = True
            for edge in min_edge.destination.edges:
                if not self.__is_visited[edge.destination]:
                    self.__queue.append(edge)

        tree_graph = Graph()
        tree_graph.vertices = vertices.copy()
        tree_graph.edges = tree_edges.copy()
        return tree_graph
Exemple #9
0
 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]]