Пример #1
0
 def read(file):
     """
     Функция считывает входной файл и возвращает граф.
     """
     title = frozenset(
         map(lambda value: value.casefold(), REGEX_TITLE.findall(file[0])))
     file = file[1:]
     node_count = 0
     matrix = []
     if 'adjmat' in title:
         for line in file:
             node_count = Parser._graph_adjacency_matrix_read(
                 node_count, matrix, line)
     elif 'adjlist' in title:
         for line in file:
             node_count = Parser._graph_adjacency_list_read(
                 node_count, matrix, line)
     elif 'edlist' in title:
         for line in file:
             node_count = Parser._graph_edge_list_read(
                 node_count, matrix, line)
     else:
         return None
     for line in matrix:
         line.extend([None] * (node_count - len(line)))
     weighted = 'w' in title
     ordered = 'o' in title
     return GraphFactory.create_graph(node_count, matrix, weighted, ordered)
Пример #2
0
 def test_little_weights_path(self):
     graph = GraphFactory.create_graph(
         5, [[None, 5, 1, None, None], [5, None, None, None, 1],
             [1, None, None, 1, None], [None, None, 1, None, 1],
             [None, 1, None, 1, None]], True, False)
     self.assertListEqual(Dijkstra().find_path(graph, 0, 1),
                          [1, 3, 4, 5, 2])
Пример #3
0
 def test_no_planar_graph(self):
     g = GraphFactory.create_graph(
         5, [[None, 1, 1, 1, 1], [1, None, 1, 1, 1], [1, 1, None, 1, 1],
             [1, 1, 1, None, 1], [1, 1, 1, 1, None]], True, False)
     with self.assertRaises(TypeError) as context:
         AStar().find_path(g, 0, 1)
     self.assertIsInstance(context.exception, TypeError)
Пример #4
0
 def test_little_weights_path(self):
     graph = GraphFactory.create_graph(
         5, [[None, 1, None, None, -1], [1, None, 1, None, None],
             [None, 1, None, None, None], [None, None, 1, None, None],
             [None, None, None, -1, None]], True, True)
     self.assertListEqual(FordBellman().find_path(graph, 0, 1),
                          [1, 5, 4, 3, 2])
Пример #5
0
 def test_weighted_graph(self):
     graph = GraphFactory.create_graph(
         5, [[None, 1, 2, 3, 4], [None, None, 2, 3, 4],
             [None, None, None, 3, 4], [None, None, None, None, 4],
             [4, 4, 4, 4, None]], True, True)
     with self.assertRaises(TypeError) as context:
         Bfs().find_path(graph, 0, 4)
     self.assertIsInstance(context.exception, TypeError)
Пример #6
0
 def test_shortest_path(self):
     graph = GraphFactory.create_graph(
         7, [[None, 1, 1, None, None, None, None],
             [None, None, None, 1, None, None, None],
             [None, None, None, 1, None, None, None],
             [None, None, None, None, 1, None, None],
             [None, None, None, None, None, 1, 1],
             [None, None, None, None, None, None, None],
             [None, None, None, None, None, 1, None]], False, True)
     self.assertListEqual(Bfs().find_path(graph, 0, 5), [1, 2, 4, 5, 6])
Пример #7
0
 def test_no_nodes(self):
     graph = GraphFactory.create_graph(
         3, [[None, 1, None], [None, None, None], [None, 1, None]], False,
         True)
     self.assertIsNone(Bfs().find_path(graph, 1, 12))
Пример #8
0
 def test_unweighted_graph(self):
     graph = GraphFactory.create_graph(2, [[None, 1], [1, None]], False,
                                       True)
     with self.assertRaises(TypeError) as context:
         FordBellman().find_path(graph, 1, 2)
     self.assertIsInstance(context.exception, TypeError)
Пример #9
0
 def test_no_path(self):
     graph = GraphFactory.create_graph(2, [[0, None], [2, 1]], True, True)
     self.assertIsNone(Dijkstra().find_path(graph, 0, 1))
Пример #10
0
 def test_simple_path(self):
     graph = GraphFactory.create_graph(
         4, [[None, 1, None, None], [1, None, 2, None], [None, 2, None, 3],
             [None, None, 3, None]], True, False)
     self.assertListEqual(Dijkstra().find_path(graph, 0, 2), [1, 2, 3])
     self.assertListEqual(Dijkstra().find_path(graph, 3, 1), [4, 3, 2])
Пример #11
0
 def test_negative_weight_cycle(self):
     graph = GraphFactory.create_graph(
         3, [[None, -1, -2], [-1, None, -4], [-1, -4, None]], True, True)
     self.assertIsNone(FordBellman().find_path(graph, 0, 2))
Пример #12
0
 def test_no_path(self):
     graph = GraphFactory.create_graph(
         4, [[None, 1, None, None], [None, None, -2, None],
             [None, None, None, 3], [None, None, None, 0]], True, True)
     self.assertIsNone(FordBellman().find_path(graph, 3, 1))
Пример #13
0
 def test_simple_path(self):
     graph = GraphFactory.create_graph(
         4, [[None, 1, None, None], [None, None, -2, None],
             [None, None, None, 3], [None, None, None, 11]], True, True)
     self.assertListEqual(FordBellman().find_path(graph, 0, 2), [1, 2, 3])
Пример #14
0
 def test_no_path(self):
     graph = GraphFactory.create_graph(
         4, [[None, None, None, None], [None, None, 1, 1],
             [None, 1, None, None], [None, 1, None, None]], False, False)
     self.assertIsNone(Dfs().find_path(graph, 0, 3))
Пример #15
0
 def test_unordered_graph(self):
     graph = GraphFactory.create_graph(2, [[0, 0], [0, 0]], True, False)
     with self.assertRaises(TypeError) as context:
         FordBellman().find_path(graph, 0, 1)
     self.assertIsInstance(context.exception, TypeError)
Пример #16
0
 def test_simple_path(self):
     graph = GraphFactory.create_graph(
         3, [[None, 1, None], [1, None, 1], [None, 1, None]], False, False)
     self.assertListEqual(Dfs().find_path(graph, 0, 1), [1, 2])
Пример #17
0
 def test_unweighted_graph(self):
     graph = GraphFactory.create_graph(1, [[1]], False, False)
     with self.assertRaises(TypeError) as context:
         Dijkstra().find_path(graph, 1, 1)
     self.assertIsInstance(context.exception, TypeError)
Пример #18
0
 def test_simple_path(self):
     graph = GraphFactory.create_graph(
         4, [[None, 1, None, None], [1, None, None, None],
             [None, None, None, 3], [None, None, 3, None]], True, False)
     self.assertListEqual(AStar().find_path(graph, 0, 1), [1, 2])
Пример #19
0
 def test_no_nodes(self):
     graph = GraphFactory.create_graph(
         4, [[None, 1, None, None], [1, None, None, None],
             [None, None, None, 3], [None, None, 3, None]], True, False)
     self.assertIsNone(AStar().find_path(graph, 0, 5))
Пример #20
0
 def test_no_nodes(self):
     graph = GraphFactory.create_graph(2, [[None, 0], [0, None]], False,
                                       False)
     self.assertIsNone(Dfs().find_path(graph, 0, 3))
Пример #21
0
 def test_no_nodes(self):
     graph = GraphFactory.create_graph(2, [[0, 0], [0, 0]], True, False)
     self.assertIsNone(Dijkstra().find_path(graph, 0, 5))
Пример #22
0
 def test_little_weights_path(self):
     graph = GraphFactory.create_graph(
         4, [[None, 1, 2, None], [1, None, None, 3], [2, None, None, 3],
             [None, 3, 3, None]], True, False)
     self.assertListEqual(AStar().find_path(graph, 0, 3), [1, 2, 4])
Пример #23
0
 def test_edge_count_in_ordered_graph(self):
     graph = GraphFactory().create_graph(
         3, [[None, 1, None], [None, None, 1], [None, None, None]],
         weighted=False,
         ordered=True)
     self.assertEqual(graph.edge_count, 2)
Пример #24
0
 def test_negative_weights(self):
     graph = GraphFactory.create_graph(2, [[0, -11], [-21, 0]], True, True)
     with self.assertRaises(TypeError) as context:
         Dijkstra().find_path(graph, 0, 5)
     self.assertIsInstance(context.exception, TypeError)