Skip to content

Commit 6a40f5c

Browse files
committed
changed the implementation for Astar
1 parent 280dd62 commit 6a40f5c

File tree

6 files changed

+8
-10
lines changed

6 files changed

+8
-10
lines changed

pydatastructs/graphs/algorithms.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
'all_pair_shortest_paths',
2424
'topological_sort',
2525
'topological_sort_parallel',
26-
'max_flow'
26+
'max_flow',
2727
]
2828

2929
Stack = Queue = deque
@@ -851,20 +851,20 @@ def heuristic(node: str, goal: str) -> float:
851851
dist = {v: float('inf') for v in graph.vertices}
852852
pred = {v: None for v in graph.vertices}
853853
dist[source] = 0
854-
# Priority queue using f-score (g_score + heuristic)
855-
pq = PriorityQueue(implementation='binomial_heap')
856-
pq.push(source, heuristic(source, target))
857-
while not pq.is_empty():
854+
from pydatastructs.miscellaneous_data_structures.queue import PriorityQueue, BinomialHeapPriorityQueue
855+
pq = PriorityQueue(implementation='binomial_heap')
856+
f_score = heuristic(source, target)
857+
pq.push(source, f_score)
858+
while not pq.is_empty:
858859
current = pq.pop()
859860
if current == target:
860-
print(f"Returning: {dist[target]}, {pred}")
861861
return dist[target], pred
862862
if visited[current]:
863863
continue
864-
visited[current] = True
864+
visited[current] = True
865865
neighbors = graph.neighbors(current)
866866
if not neighbors:
867-
continue
867+
continue
868868
for neighbor in neighbors:
869869
if visited[neighbor.name]:
870870
continue
@@ -877,10 +877,8 @@ def heuristic(node: str, goal: str) -> float:
877877
pred[neighbor.name] = current
878878
f_score = new_dist + heuristic(neighbor.name, target)
879879
pq.push(neighbor.name, f_score)
880-
print(f"Returning: {float('inf')}, {pred}")
881880
return float('inf'), pred
882881
_a_star_adjacency_matrix = _a_star_adjacency_list
883-
884882
def all_pair_shortest_paths(graph: Graph, algorithm: str,
885883
**kwargs) -> tuple:
886884
"""
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)