mirror of
https://github.com/onyx-and-iris/grokking-algorithms.git
synced 2024-11-15 17:30:52 +00:00
add visited set
This commit is contained in:
parent
1d22356ce5
commit
ab09aa5269
@ -20,10 +20,15 @@ def dijkstra(graph, node):
|
|||||||
costs[node] = 0
|
costs[node] = 0
|
||||||
parents = {node: None for node in graph}
|
parents = {node: None for node in graph}
|
||||||
queue = [(0, node)]
|
queue = [(0, node)]
|
||||||
|
visited = set()
|
||||||
|
|
||||||
while queue:
|
while queue:
|
||||||
current_cost, current_node = heapq.heappop(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")
|
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():
|
for next_node, weight in graph[current_node].items():
|
||||||
new_cost = current_cost + weight
|
new_cost = current_cost + weight
|
||||||
|
@ -19,10 +19,15 @@ def dijkstra(graph, node):
|
|||||||
costs[node] = 0
|
costs[node] = 0
|
||||||
parents = {node: None for node in graph}
|
parents = {node: None for node in graph}
|
||||||
queue = [(0, node)]
|
queue = [(0, node)]
|
||||||
|
visited = set()
|
||||||
|
|
||||||
while queue:
|
while queue:
|
||||||
current_cost, current_node = heapq.heappop(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")
|
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():
|
for next_node, weight in graph[current_node].items():
|
||||||
new_cost = current_cost + weight
|
new_cost = current_cost + weight
|
||||||
|
@ -19,10 +19,15 @@ def dijkstra(graph, node):
|
|||||||
costs[node] = 0
|
costs[node] = 0
|
||||||
parents = {node: None for node in graph}
|
parents = {node: None for node in graph}
|
||||||
queue = [(0, node)]
|
queue = [(0, node)]
|
||||||
|
visited = set()
|
||||||
|
|
||||||
while queue:
|
while queue:
|
||||||
current_cost, current_node = heapq.heappop(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")
|
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():
|
for next_node, weight in graph[current_node].items():
|
||||||
new_cost = current_cost + weight
|
new_cost = current_cost + weight
|
||||||
|
Loading…
Reference in New Issue
Block a user