mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-07-05 17:34:49 +08:00
Corrected wrong Dijkstra priority queue implementation (#909)
* Corrected wrong DFS implementation * changed list into hash set for dijkstra priority queue implementation.
This commit is contained in:
@ -20,12 +20,12 @@ import heapq
|
|||||||
|
|
||||||
def dijkstra(graph, start, end):
|
def dijkstra(graph, start, end):
|
||||||
heap = [(0, start)] # cost from start node,end node
|
heap = [(0, start)] # cost from start node,end node
|
||||||
visited = []
|
visited = set()
|
||||||
while heap:
|
while heap:
|
||||||
(cost, u) = heapq.heappop(heap)
|
(cost, u) = heapq.heappop(heap)
|
||||||
if u in visited:
|
if u in visited:
|
||||||
continue
|
continue
|
||||||
visited.append(u)
|
visited.add(u)
|
||||||
if u == end:
|
if u == end:
|
||||||
return cost
|
return cost
|
||||||
for v, c in G[u]:
|
for v, c in G[u]:
|
||||||
|
Reference in New Issue
Block a user