Exemplo n.º 1
0
 def test_dijkstra_correctness_3_fifth_vert(self):
     graph_matrix = [[0, 0, 1, 3, 0, 4, 1], [0, 0, 0, 2, 0, 0, 0],
                     [1, 0, 0, 0, 0, 2, 0], [3, 2, 0, 0, 1, 0, 0],
                     [0, 0, 0, 1, 0, 2, 3], [4, 0, 2, 0, 2, 0, 0],
                     [1, 0, 0, 0, 3, 0, 0]]
     graph = Graph(7, graph_matrix)
     shortest_distances_five = graph.dijkstra(5)
     self.assertTrue(shortest_distances_five == [3, 5, 2, 3, 2, 0, 4])
Exemplo n.º 2
0
 def test_dijkstra_correctness_3_six_vert(self):
     graph_matrix = [[0, 0, 1, 3, 0, 4, 1], [0, 0, 0, 2, 0, 0, 0],
                     [1, 0, 0, 0, 0, 2, 0], [3, 2, 0, 0, 1, 0, 0],
                     [0, 0, 0, 1, 0, 2, 3], [4, 0, 2, 0, 2, 0, 0],
                     [1, 0, 0, 0, 3, 0, 0]]
     graph = Graph(7, graph_matrix)
     shortest_distances_six = graph.dijkstra(6)
     self.assertTrue(shortest_distances_six == [1, 6, 2, 4, 3, 4, 0])
Exemplo n.º 3
0
 def test_dijkstra_correctness_3_first_vert(self):
     graph_matrix = [[0, 0, 1, 3, 0, 4, 1], [0, 0, 0, 2, 0, 0, 0],
                     [1, 0, 0, 0, 0, 2, 0], [3, 2, 0, 0, 1, 0, 0],
                     [0, 0, 0, 1, 0, 2, 3], [4, 0, 2, 0, 2, 0, 0],
                     [1, 0, 0, 0, 3, 0, 0]]
     graph = Graph(7, graph_matrix)
     shortest_distances_one = graph.dijkstra(1)
     self.assertTrue(shortest_distances_one == [5, 0, 6, 2, 3, 5, 6])
Exemplo n.º 4
0
 def test_dijkstra_correctness_3_second_vert(self):
     graph_matrix = [[0, 0, 1, 3, 0, 4, 1], [0, 0, 0, 2, 0, 0, 0],
                     [1, 0, 0, 0, 0, 2, 0], [3, 2, 0, 0, 1, 0, 0],
                     [0, 0, 0, 1, 0, 2, 3], [4, 0, 2, 0, 2, 0, 0],
                     [1, 0, 0, 0, 3, 0, 0]]
     graph = Graph(7, graph_matrix)
     shortest_distances_two = graph.dijkstra(2)
     self.assertTrue(shortest_distances_two == [1, 6, 0, 4, 4, 2, 2])
Exemplo n.º 5
0
 def test_dijkstra_correctness_3_zero_vert(self):
     graph_matrix = [[0, 0, 1, 3, 0, 4, 1], [0, 0, 0, 2, 0, 0, 0],
                     [1, 0, 0, 0, 0, 2, 0], [3, 2, 0, 0, 1, 0, 0],
                     [0, 0, 0, 1, 0, 2, 3], [4, 0, 2, 0, 2, 0, 0],
                     [1, 0, 0, 0, 3, 0, 0]]
     graph = Graph(7, graph_matrix)
     shortest_distances_zero = graph.dijkstra(0)
     self.assertTrue(shortest_distances_zero == [0, 5, 1, 3, 4, 3, 1])
 def run_dijkstra(self, matrix):
     try:
         weights = [[int(val) for val in row] for row in matrix]
         graph = Graph(int(self.num_vertex), weights)
         result = graph.dijkstra(int(self.start_vertex))
         self.errors = ''
         return result
     except Exception as e:
         self.errors = str(e)
         return []
Exemplo n.º 7
0
 def run_dijkstra(self, matrix):
     self.logger.log('Running dijkstra algorithm')
     try:
         weights = [[int(val) for val in row] for row in matrix]
         graph = Graph(int(self.num_vertex), weights)
         result = graph.dijkstra(int(self.start_vertex))
         self.errors = ''
         self.logger.log('Dijkstra algorithm successfully completed')
         return result
     except Exception as e:
         self.errors = str(e)
         self.logger.log('Error: {}'.format(self.errors))
         return []
Exemplo n.º 8
0
 def test_dijkstra_correctness_zero_vert(self):
     graph_matrix = [[0, 2, 1], [2, 0, 3], [1, 3, 0]]
     graph = Graph(3, graph_matrix)
     shortest_distances_zero = graph.dijkstra(0)
     self.assertTrue(shortest_distances_zero == [0, 2, 1])
Exemplo n.º 9
0
 def test_dijkstra_one_element_graph(self):
     graph_matrix = [[0]]
     graph = Graph(1, graph_matrix)
     shortest_distances = graph.dijkstra(0)
     self.assertTrue(shortest_distances == [0])
Exemplo n.º 10
0
 def test_dijkstra_correctness_2_float_third_vert(self):
     graph_matrix = [[0, 4, 1.2, 3], [4, 0, 1.1, 0], [1.2, 1.1, 0, 1],
                     [3, 0, 1, 0]]
     graph = Graph(4, graph_matrix)
     shortest_distances_three = graph.dijkstra(3)
     self.assertTrue(shortest_distances_three == [2.2, 2.1, 1, 0])
Exemplo n.º 11
0
 def test_dijkstra_correctness_2_float_second_vert(self):
     graph_matrix = [[0, 4, 1.2, 3], [4, 0, 1.1, 0], [1.2, 1.1, 0, 1],
                     [3, 0, 1, 0]]
     graph = Graph(4, graph_matrix)
     shortest_distances_two = graph.dijkstra(2)
     self.assertTrue(shortest_distances_two == [1.2, 1.1, 0, 1])
Exemplo n.º 12
0
 def test_dijkstra_correctness_2_float_first_vert(self):
     graph_matrix = [[0, 4, 1.2, 3], [4, 0, 1.1, 0], [1.2, 1.1, 0, 1],
                     [3, 0, 1, 0]]
     graph = Graph(4, graph_matrix)
     shortest_distances_one = graph.dijkstra(1)
     self.assertTrue(shortest_distances_one == [2.3, 0, 1.1, 2.1])
Exemplo n.º 13
0
 def test_dijkstra_correctness_2_float_zero_vert(self):
     graph_matrix = [[0, 4, 1.2, 3], [4, 0, 1.1, 0], [1.2, 1.1, 0, 1],
                     [3, 0, 1, 0]]
     graph = Graph(4, graph_matrix)
     shortest_distances_zero = graph.dijkstra(0)
     self.assertTrue(shortest_distances_zero == [0, 2.3, 1.2, 2.2])
Exemplo n.º 14
0
 def test_dijkstra_correctness_first_vert(self):
     graph_matrix = [[0, 2, 1], [2, 0, 3], [1, 3, 0]]
     graph = Graph(3, graph_matrix)
     shortest_distances_one = graph.dijkstra(1)
     self.assertTrue(shortest_distances_one == [2, 0, 3])