-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dijkstra'sAlgorithm.py
65 lines (50 loc) · 1.83 KB
/
Dijkstra'sAlgorithm.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import heapq
"""
@Chatbot AI
@Mopheshi
I used a priority queue in this implementation, 'graph' is a dictionary that contains the graph represented as an
adjacency list, where each key represents a node and its value is another dictionary that contains its adjacent nodes
and their weights.
For example, the following graph can be represented as a dictionary:
7
A ----- B
|\ |\
| \ | \
4 | 2 | 3
| \ | \
| \ | \
C----- D ----- E
5 2
"""
# This returns a dictionary with the shortest distances from node A to all other nodes in the graph.
def dijkstra(graph, start):
# Initialize distances to all other nodes as infinity
dists = {node: float('inf') for node in graph}
dists[start] = 0
# Use min-heap to keep track of next node to visit
heap = [(0, start)]
while heap:
# Pop the node with the smallest distance
current_dist, current_node = heapq.heappop(heap)
# If distance to current node is already less than the distance in heap, ignore it
if current_dist > dists[current_node]:
continue
# Visit adjacent nodes and update distances if a shorter path is found
for neighbor, weight in graph[current_node].items():
new_distance = current_dist + weight
if new_distance < dists[neighbor]:
dists[neighbor] = new_distance
heapq.heappush(heap, (new_distance, neighbor))
return dists
# Declare the graph as a dictionary
graph = {
'A': {'B': 7, 'C': 4},
'B': {'A': 7, 'D': 3, 'E': 2},
'C': {'A': 4, 'D': 6},
'D': {'B': 3, 'C': 6, 'E': 2},
'E': {'B': 2, 'D': 2}
}
# To find the shortest path from node A to all other nodes, we can call the 'dijkstra' function with the graph and the
# starting node
distances = dijkstra(graph, 'A')
print(distances)