Skip to content

Commit 280dd62

Browse files
committed
added print statements at necessary positions for testing the errors
1 parent b9dcaef commit 280dd62

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

pydatastructs/graphs/algorithms.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -839,24 +839,33 @@ def _a_star_adjacency_list(graph: Graph, source: str, target: str) -> tuple:
839839
"""
840840
def heuristic(node: str, goal: str) -> float:
841841
"""Manhattan distance heuristic for A*"""
842-
x1, y1 = map(int, node.split(','))
843-
x2, y2 = map(int, goal.split(','))
844-
return abs(x1 - x2) + abs(y1 - y2)
842+
try:
843+
x1, y1 = map(int, node.split(','))
844+
x2, y2 = map(int, goal.split(','))
845+
return abs(x1 - x2) + abs(y1 - y2)
846+
except ValueError:
847+
raise ValueError(f"Invalid node format: {node}. Expected 'x,y'.")
848+
if source not in graph.vertices or target not in graph.vertices:
849+
raise KeyError(f"Either source '{source}' or target '{target}' is not in the graph.")
845850
visited = {v: False for v in graph.vertices}
846851
dist = {v: float('inf') for v in graph.vertices}
847852
pred = {v: None for v in graph.vertices}
848853
dist[source] = 0
849854
# Priority queue using f-score (g_score + heuristic)
850855
pq = PriorityQueue(implementation='binomial_heap')
851856
pq.push(source, heuristic(source, target))
852-
while not pq.is_empty:
857+
while not pq.is_empty():
853858
current = pq.pop()
854859
if current == target:
860+
print(f"Returning: {dist[target]}, {pred}")
855861
return dist[target], pred
856862
if visited[current]:
857863
continue
858864
visited[current] = True
859-
for neighbor in graph.neighbors(current):
865+
neighbors = graph.neighbors(current)
866+
if not neighbors:
867+
continue
868+
for neighbor in neighbors:
860869
if visited[neighbor.name]:
861870
continue
862871
edge = graph.get_edge(current, neighbor.name)
@@ -868,6 +877,7 @@ def heuristic(node: str, goal: str) -> float:
868877
pred[neighbor.name] = current
869878
f_score = new_dist + heuristic(neighbor.name, target)
870879
pq.push(neighbor.name, f_score)
880+
print(f"Returning: {float('inf')}, {pred}")
871881
return float('inf'), pred
872882
_a_star_adjacency_matrix = _a_star_adjacency_list
873883

0 commit comments

Comments
 (0)