|
| 1 | +import pytest |
1 | 2 | from pydatastructs import (breadth_first_search, Graph, |
2 | 3 | breadth_first_search_parallel, minimum_spanning_tree, |
3 | 4 | minimum_spanning_tree_parallel, strongly_connected_components, |
@@ -314,13 +315,56 @@ def _test_shortest_paths_negative_edges(ds, algorithm): |
314 | 315 | dist, pred = shortest_paths(graph, algorithm, 's', 'd') |
315 | 316 | assert dist == 2 |
316 | 317 | assert pred == {'s': None, 'a': 'b', 'b': 's', 'c': 'a', 'd': 'c'} |
317 | | - |
| 318 | + def _test_a_star_manhattan(ds): |
| 319 | + import pydatastructs.utils.misc_util as utils |
| 320 | + GraphNode = getattr(utils, "Adjacency" + ds + "GraphNode") |
| 321 | + vertices = [ |
| 322 | + GraphNode("0,0"), |
| 323 | + GraphNode("1,1"), |
| 324 | + GraphNode("2,2") |
| 325 | + ] |
| 326 | + graph = Graph(*vertices) |
| 327 | + graph.add_edge("0,0", "1,1", 2) |
| 328 | + graph.add_edge("1,1", "2,2", 2) |
| 329 | + distance, pred = shortest_paths(graph, 'a_star', "0,0", "2,2") |
| 330 | + assert distance == 4 |
| 331 | + assert pred == {'0,0': None, '1,1': '0,0', '2,2': '1,1'} |
| 332 | + no_path_graph = Graph( |
| 333 | + GraphNode("0,0"), |
| 334 | + GraphNode("1,1"), |
| 335 | + GraphNode("2,2") |
| 336 | + ) |
| 337 | + with pytest.raises(ValueError, match="Either source '0,0' and target '2,2' have no path between them."): |
| 338 | + shortest_paths(no_path_graph, 'a_star', "0,0", "2,2") |
| 339 | + same_node_graph = Graph(GraphNode("1,1")) |
| 340 | + distance, pred = shortest_paths(same_node_graph, 'a_star', "1,1", "1,1") |
| 341 | + assert distance == 0 |
| 342 | + assert pred == {'1,1': None} |
| 343 | + invalid_graph = Graph(GraphNode("invalid")) |
| 344 | + with pytest.raises(ValueError, match="Invalid node format: invalid. Expected 'x,y'."): |
| 345 | + shortest_paths(invalid_graph, 'a_star', "invalid", "invalid") |
| 346 | + complex_vertices = [ |
| 347 | + GraphNode("0,0"), |
| 348 | + GraphNode("0,1"), |
| 349 | + GraphNode("1,0"), |
| 350 | + GraphNode("1,1") |
| 351 | + ] |
| 352 | + complex_graph = Graph(*complex_vertices) |
| 353 | + complex_graph.add_edge("0,0", "0,1", 1) |
| 354 | + complex_graph.add_edge("0,1", "1,1", 1) |
| 355 | + complex_graph.add_edge("0,0", "1,0", 2) |
| 356 | + complex_graph.add_edge("1,0", "1,1", 1) |
| 357 | + distance, pred = shortest_paths(complex_graph, 'a_star', "0,0", "1,1") |
| 358 | + assert distance == 2 |
| 359 | + assert pred == {'0,0': None, '0,1': '0,0', '1,1': '0,1', '1,0': '0,0'} |
318 | 360 | _test_shortest_paths_positive_edges("List", 'bellman_ford') |
319 | 361 | _test_shortest_paths_positive_edges("Matrix", 'bellman_ford') |
320 | 362 | _test_shortest_paths_negative_edges("List", 'bellman_ford') |
321 | 363 | _test_shortest_paths_negative_edges("Matrix", 'bellman_ford') |
322 | 364 | _test_shortest_paths_positive_edges("List", 'dijkstra') |
323 | 365 | _test_shortest_paths_positive_edges("Matrix", 'dijkstra') |
| 366 | + _test_a_star_manhattan("List") |
| 367 | + _test_a_star_manhattan("Matrix") |
324 | 368 |
|
325 | 369 | def test_all_pair_shortest_paths(): |
326 | 370 |
|
|
0 commit comments