Changed implementation of Dijkstra algorithm a little.

This commit is contained in:
Bartosz 2022-08-28 18:03:26 +02:00
parent cab460feb8
commit 256e8b3908

View File

@ -61,16 +61,18 @@ def dijkstra_algorithm(graph, start, goal, is_directed=False):
else: else:
new_dist = dist[current[1]] + graph[(current[1], neighbor)] new_dist = dist[current[1]] + graph[(current[1], neighbor)]
if new_dist < dist[neighbor]: if new_dist < dist[neighbor]:
dist[neighbor] = new_dist if dist[neighbor] == float('inf'):
for k in range(len(queue)): dist[neighbor] = new_dist
if queue[k][1] == neighbor: prev[neighbor] = current[1]
queue[k] = (new_dist, neighbor) heap.heappush(queue, (new_dist, neighbor))
heap.heapify(queue) else:
prev[neighbor] = current[1] dist[neighbor] = new_dist
prev[neighbor] = current[1]
for k in range(len(queue)):
if queue[k][1] == neighbor:
queue[k] = (new_dist, neighbor)
heap.heapify(queue)
if not visited[neighbor]:
visited[neighbor] = True
heap.heappush(queue, (new_dist, neighbor))
temp = goal temp = goal
shortest_path = [goal] shortest_path = [goal]
while prev[temp] is not None: while prev[temp] is not None: