-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDijkstra.py
More file actions
48 lines (38 loc) · 1.71 KB
/
Dijkstra.py
File metadata and controls
48 lines (38 loc) · 1.71 KB
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
import csv
from heapq import heappop, heappush
from collections import defaultdict
class Dijkstra:
def __init__(self, csv_path):
self.graph = defaultdict(list)
self.read_csv(csv_path)
def read_csv(self, csv_path):
with open(csv_path, 'r', encoding='utf-8') as file:
reader = csv.reader(file)
next(reader) # 跳过标题行
for start, end, weight in reader:
self.graph[int(start)].append((int(end), float(weight)))
self.graph[int(end)].append((int(start), float(weight))) # 无向图,需要添加反向边
def find_shortest_path(self, start, end):
distances = {node: float('inf') for node in self.graph}
distances[start] = 0
priority_queue = [(0, start)]
predecessors = {node: None for node in self.graph}
while priority_queue:
current_distance, current_node = heappop(priority_queue)
if current_node == end:
break
for neighbor, weight in self.graph[current_node]:
distance = current_distance + weight
if distance < distances[neighbor]:
distances[neighbor] = distance
predecessors[neighbor] = current_node
heappush(priority_queue, (distance, neighbor))
return distances, predecessors
def get_shortest_path(self, start, end):
distances, predecessors = self.find_shortest_path(start, end)
path = []
current = end
while current is not None:
path.insert(0, current)
current = predecessors[current]
return path, distances[end]