-
Notifications
You must be signed in to change notification settings - Fork 99
/
dijkstra.py
51 lines (40 loc) · 1.48 KB
/
dijkstra.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
"""
Given directed graph
Edge weights must ALL be non-negative
(see bellman-ford for that solution)
"""
from graphs_and_trees.adjacency_list import Graph
from stacks_queues import PriorityDict
def dijkstra(graph, start):
pq = PriorityDict()
start.distance = 0
for node in graph.vertices.values():
pq[node] = node.distance
while pq:
current_node = pq.pop_smallest()
for next_node in current_node.get_neighbors():
new_dist = current_node.distance + \
current_node.get_edge_weight(next_node)
if new_dist < next_node.distance:
next_node.distance = new_dist
next_node.predecessor = current_node
pq[next_node] = new_dist
def load_graph_from_file(graph_file):
new_graph = Graph()
with open(graph_file, 'rb') as f:
for line in f.readlines():
line = " ".join(line.split())
line = line.split()
current_node, nodes = line[0], line[1:]
new_graph.add_vertex(int(current_node))
for node in nodes:
n, weight = node.split(",")
new_graph.add_edge(int(current_node), int(n), int(weight))
return new_graph
if __name__ == "__main__":
graph = load_graph_from_file("test_dijkstra.txt")
start_v = graph.get_vertex(1)
dijkstra(graph, start_v)
node_checks = [7,37,59,82,99,115,133,165,188,197]
for node in node_checks:
print(graph.get_vertex(node).distance)