@@ -743,6 +743,10 @@ def shortest_paths(graph: Graph, algorithm: str,
743743 >>> grid_graph.add_edge('0,0', '1,1', 2)
744744 >>> shortest_paths(grid_graph, 'a_star_with_manhattan', '0,0', '1,1')
745745 (2, {'1,1': '0,0'})
746+ <<<<<<< HEAD
747+ =======
748+
749+ >>>>>>> main
746750 References
747751 ==========
748752
@@ -753,6 +757,12 @@ def shortest_paths(graph: Graph, algorithm: str,
753757 raise_if_backend_is_not_python (
754758 shortest_paths , kwargs .get ('backend' , Backend .PYTHON ))
755759 import pydatastructs .graphs .algorithms as algorithms
760+ if algorithm == 'a_star_with_manhattan' :
761+ if not target :
762+ raise ValueError ("Target must be specified for A* algorithm" )
763+
764+ func = "_a_star_with_manhattan_adjacency_list"
765+ return getattr (algorithms , func )(graph , source , target )
756766 func = "_" + algorithm + "_" + graph ._impl
757767 if not hasattr (algorithms , func ):
758768 raise NotImplementedError (
@@ -822,14 +832,16 @@ def _a_star_with_manhattan_adjacency_list(graph: Graph, start: str, target: str,
822832 """
823833 A* algorithm with Manhattan distance as the heuristic function for grid-based graphs.
824834 """
835+ raise_if_backend_is_not_python (
836+ _a_star_with_manhattan_adjacency_list , kwargs .get ('backend' , Backend .PYTHON )
837+ )
825838 def manhattan_distance (node1 : str , node2 : str ) -> float :
826839 try :
827840 x1 , y1 = map (int , node1 .split ("," ))
828841 x2 , y2 = map (int , node2 .split ("," ))
829842 return abs (x1 - x2 ) + abs (y1 - y2 )
830843 except (ValueError , TypeError ):
831844 raise ValueError (f"Invalid node format. Expected 'x,y', got { node1 } or { node2 } " )
832- # Validate inputs
833845 if start == target :
834846 return 0 , {start : None }
835847 if start not in graph .vertices or target not in graph .vertices :
@@ -845,12 +857,7 @@ def manhattan_distance(node1: str, node2: str) -> float:
845857 while not pq .is_empty :
846858 current = pq .pop ()
847859 if current == target :
848- path_pred = {}
849- node = target
850- while node is not None :
851- path_pred [node ] = pred [node ]
852- node = pred [node ]
853- return g_score [target ], path_pred
860+ return g_score [target ], {target : start }
854861 visited [current ] = True
855862 for neighbor in graph .neighbors (current ):
856863 if visited [neighbor .name ]:
@@ -869,6 +876,7 @@ def manhattan_distance(node1: str, node2: str) -> float:
869876 pq .push (neighbor .name , f_score [neighbor .name ])
870877 raise ValueError (f"No path exists between { start } and { target } " )
871878_a_star_with_manhattan_adjacency_matrix = _a_star_with_manhattan_adjacency_list
879+
872880def all_pair_shortest_paths (graph : Graph , algorithm : str ,
873881 ** kwargs ) -> tuple :
874882 """
0 commit comments