diff --git a/algorithms/dijkstra.py b/algorithms/dijkstra.py index 7e9cfac..5d89f9b 100644 --- a/algorithms/dijkstra.py +++ b/algorithms/dijkstra.py @@ -61,16 +61,18 @@ def dijkstra_algorithm(graph, start, goal, is_directed=False): else: new_dist = dist[current[1]] + graph[(current[1], neighbor)] if new_dist < dist[neighbor]: - dist[neighbor] = new_dist - for k in range(len(queue)): - if queue[k][1] == neighbor: - queue[k] = (new_dist, neighbor) - heap.heapify(queue) - prev[neighbor] = current[1] + if dist[neighbor] == float('inf'): + dist[neighbor] = new_dist + prev[neighbor] = current[1] + heap.heappush(queue, (new_dist, neighbor)) + else: + 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 shortest_path = [goal] while prev[temp] is not None: