-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtemp.py
41 lines (30 loc) · 1.05 KB
/
temp.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
The code has a bug. The error is a NameError("name 'sys' is not defined"). This error occurs because the 'sys' module is not imported.
The corrected code is:
```
import sys
import heapq
def dijkstra(graph, start):
distances = {node: sys.maxsize for node in graph}
distances[start] = 0
pq = [(0, start)]
while pq:
current_distance, current_node = heapq.heappop(pq)
if current_distance > distances[current_node]:
continue
for neighbor, weight in graph[current_node].items():
distance = current_distance + weight
if distance < distances[neighbor]:
distances[neighbor] = distance
heapq.heappush(pq, (distance, neighbor))
return distances
graph = {
'A': {'B': 1, 'C': 4},
'B': {'A': 1, 'C': 2, 'D': 5},
'C': {'A': 4, 'B': 2, 'D': 1},
'D': {'B': 5, 'C': 1}
}
start_node = 'A'
result = dijkstra(graph, start_node)
print(f"Shortest distances from {start_node}: {result}")
```
This code should run without errors and produce the correct output.