mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-02-17 14:58:10 +00: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:
parent
12a16d63b7
commit
a212efee5b
|
@ -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]:
|
||||||
|
|
Loading…
Reference in New Issue
Block a user