@@ -722,11 +722,11 @@ def shortest_paths(graph: Graph, algorithm: str, source: str, target: str="", **
722
722
raise_if_backend_is_not_python (
723
723
shortest_paths , kwargs .get ('backend' , Backend .PYTHON ))
724
724
import pydatastructs .graphs .algorithms as algorithms
725
- func = "_" + algorithm + "_" + graph ._impl
726
- if algorithm not in ['bellman_ford' , 'dijkstra' , 'A_star' ]:
727
- raise NotImplementedError (f"Algorithm { algorithm } is not implemented." )
725
+ func = "_" + algorithm .lower () + "_" + graph ._impl
728
726
if not hasattr (algorithms , func ):
729
- raise NotImplementedError (f"Currently { algorithm } algorithm isn't implemented for finding shortest paths in graphs." )
727
+ raise NotImplementedError (
728
+ "Currently %s algorithm isn't implemented for "
729
+ "finding shortest paths in graphs." % (algorithm ))
730
730
return getattr (algorithms , func )(graph , source , target )
731
731
732
732
def _bellman_ford_adjacency_list (graph : Graph , source : str , target : str ) -> tuple :
@@ -797,22 +797,23 @@ def _a_star_adjacency_list(graph: Graph, source: str, target: str) -> tuple:
797
797
798
798
from pydatastructs .miscellaneous_data_structures .queue import PriorityQueue
799
799
pq = PriorityQueue (implementation = 'binomial_heap' )
800
- pq .push (source , distances [source ] + heuristic ( source , target )) # Fixed push
800
+ pq .push (source , distances [source ])
801
801
802
802
def heuristic (node : str , goal : str ) -> float :
803
+ """Manhattan distance heuristic for A*"""
803
804
try :
804
- x1 , y1 = map (int , node .split (',' ))
805
- x2 , y2 = map (int , goal .split (',' ))
806
- return abs (x1 - x2 ) + abs (y1 - y2 )
805
+ if "," in node and "," in goal : # Check if node names are in "x,y" format
806
+ x1 , y1 = map (int , node .split (',' ))
807
+ x2 , y2 = map (int , goal .split (',' ))
808
+ return abs (x1 - x2 ) + abs (y1 - y2 )
809
+ else :
810
+ return 0 # If not in coordinate format, return 0 heuristic
807
811
except ValueError :
808
- raise ValueError ( f"Invalid node format: { node } . Expected 'x,y'." )
812
+ return 0 # Fallback heuristic if parsing fails
809
813
810
814
while not pq .is_empty :
811
815
current = pq .pop ()
812
816
813
- if current == target :
814
- return (distances [target ], predecessor )
815
-
816
817
neighbors = graph .neighbors (current )
817
818
for neighbor in neighbors :
818
819
edge = graph .get_edge (current , neighbor .name )
@@ -823,10 +824,15 @@ def heuristic(node: str, goal: str) -> float:
823
824
predecessor [neighbor .name ] = current
824
825
pq .push (neighbor .name , new_dist + heuristic (neighbor .name , target ))
825
826
826
- if distances [target ] == float ('inf' ):
827
- raise ValueError (f"Either source '{ source } ' and target '{ target } ' have no path between them." )
827
+ # ✅ Handle case when target is empty (all-pairs shortest paths)
828
+ if target == "" :
829
+ return (distances , predecessor )
828
830
829
- return (distances , predecessor )
831
+ # ✅ Handle no path found case properly
832
+ if target not in distances or distances [target ] == float ('inf' ):
833
+ return (float ('inf' ), predecessor )
834
+
835
+ return (distances [target ], predecessor )
830
836
831
837
_a_star_adjacency_matrix = _a_star_adjacency_list # Ensure matrix version exists
832
838
0 commit comments