add visited set

This commit is contained in:
onyx-and-iris 2024-02-19 00:03:44 +00:00
parent 1d22356ce5
commit ab09aa5269
3 changed files with 15 additions and 0 deletions

View File

@ -20,10 +20,15 @@ def dijkstra(graph, node):
costs[node] = 0
parents = {node: None for node in graph}
queue = [(0, node)]
visited = set()
while queue:
current_cost, current_node = heapq.heappop(queue)
if current_node in visited:
continue
logger.debug(f"node {current_node} with cost {current_cost} popped from pqueue")
visited.add(current_node)
for next_node, weight in graph[current_node].items():
new_cost = current_cost + weight

View File

@ -19,10 +19,15 @@ def dijkstra(graph, node):
costs[node] = 0
parents = {node: None for node in graph}
queue = [(0, node)]
visited = set()
while queue:
current_cost, current_node = heapq.heappop(queue)
if current_node in visited:
continue
logger.debug(f"node {current_node} with cost {current_cost} popped from pqueue")
visited.add(current_node)
for next_node, weight in graph[current_node].items():
new_cost = current_cost + weight

View File

@ -19,10 +19,15 @@ def dijkstra(graph, node):
costs[node] = 0
parents = {node: None for node in graph}
queue = [(0, node)]
visited = set()
while queue:
current_cost, current_node = heapq.heappop(queue)
if current_node in visited:
continue
logger.debug(f"node {current_node} with cost {current_cost} popped from pqueue")
visited.add(current_node)
for next_node, weight in graph[current_node].items():
new_cost = current_cost + weight