-
Notifications
You must be signed in to change notification settings - Fork 108
/
Copy pathDijkstra's algorithm.py
30 lines (24 loc) · 1.1 KB
/
Dijkstra's algorithm.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import heapq
def shortest_path(graph, source, destination):
# Initialize distances and predecessors
distances = {node: float('infinity') for node in graph}
predecessors = {node: None for node in graph}
distances[source] = 0
# Initialize priority queue with source node
priority_queue = [(0, source)]
while priority_queue:
current_distance, current_node = heapq.heappop(priority_queue)
if current_node == destination:
path = []
while predecessors[current_node] is not None:
path.insert(0, current_node)
current_node = predecessors[current_node]
path.insert(0, source)
return path, distances[destination]
for neighbor, weight in graph[current_node].items():
distance = current_distance + weight
if distance < distances[neighbor]:
distances[neighbor] = distance
predecessors[neighbor] = current_node
heapq.heappush(priority_queue, (distance, neighbor))
return None, float('infinity')