-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dijkstra's algorithm.py
40 lines (35 loc) · 1.44 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
31
32
33
34
35
36
37
38
39
40
import heapq # heapq is used to implement priority queues
def dijkstra(graph, start):
# initialize all distances to infinity
distances = {node: float('inf') for node in graph}
# set the distance to the start node to 0
distances[start] = 0
# initialize the priority queue with the start node and its distance
pq = [(0, start)]
# loop while there are still nodes to visit
while pq:
# pop the node with the smallest distance from the priority queue
(dist, node) = heapq.heappop(pq)
# if the distance to the node is already known, skip it
if dist > distances[node]:
continue
# loop over the node's neighbors
for neighbor, weight in graph[node].items():
# calculate the distance to the neighbor through the current node
distance = dist + weight
# if this distance is shorter than the previously known distance, update it
if distance < distances[neighbor]:
distances[neighbor] = distance
# add the neighbor to the priority queue with its new distance
heapq.heappush(pq, (distance, neighbor))
# return the dictionary of shortest distances from the start node to each node
return distances
graph = {
'A': {'B': 3, 'C': 1},
'B': {'C': 7, 'D': 5},
'C': {'D': 2, 'E': 7},
'D': {'E': 1},
'E': {}
}
distances = dijkstra(graph, 'A')
print(distances)