@@ -14,54 +14,54 @@ def __init__(self) -> None:
14
14
self .graph : dict [str , list [tuple [str , int ]]] = {}
15
15
16
16
# add vertices for a graph
17
- def add_vertices (self , u : str ) -> None :
17
+ def add_vertices (self , vertex : str ) -> None :
18
18
"""
19
19
Adds a vertex `u` to the graph with an empty adjacency list.
20
20
"""
21
- self .graph [u ] = []
21
+ self .graph [vertex ] = []
22
22
23
23
# assign weights for each edges formed of the directed graph
24
- def add_edge (self , u : str , v : str , w : int ) -> None :
24
+ def add_edge (self , vertex_a : str , vertex_b : str , weight : int ) -> None :
25
25
"""
26
26
Adds a directed edge from vertex `u` to vertex `v` with weight `w`.
27
27
"""
28
- self .edges .append ((u , v , w ))
29
- self .graph [u ].append ((v , w ))
28
+ self .edges .append ((vertex_a , vertex_b , weight ))
29
+ self .graph [vertex_a ].append ((vertex_b , weight ))
30
30
31
31
# perform a dijkstra algorithm on a directed graph
32
- def dijkstra (self , s : str ) -> dict :
32
+ def dijkstra (self , start : str ) -> dict :
33
33
"""
34
34
Computes the shortest path from vertex `s`
35
35
to all other vertices using Dijkstra's algorithm.
36
36
"""
37
37
distances = {vertex : sys .maxsize - 1 for vertex in self .graph }
38
- pq = [(0 , s )]
39
- distances [s ] = 0
38
+ pq = [(0 , start )]
39
+ distances [start ] = 0
40
40
while pq :
41
- weight , v = heapq .heappop (pq )
41
+ weight , vertex = heapq .heappop (pq )
42
42
43
- if weight > distances [v ]:
43
+ if weight > distances [vertex ]:
44
44
continue
45
45
46
- for node , w in self .graph [v ]:
47
- if distances [v ] + w < distances [node ]:
48
- distances [node ] = distances [v ] + w
46
+ for node , node_weight in self .graph [vertex ]:
47
+ if distances [vertex ] + node_weight < distances [node ]:
48
+ distances [node ] = distances [vertex ] + node_weight
49
49
heapq .heappush (pq , (distances [node ], node ))
50
50
return distances
51
51
52
52
# carry out the bellman ford algorithm for a node and estimate its distance vector
53
- def bellman_ford (self , s : str ) -> dict :
53
+ def bellman_ford (self , start : str ) -> dict :
54
54
"""
55
55
Computes the shortest path from vertex `s`
56
56
to all other vertices using the Bellman-Ford algorithm.
57
57
"""
58
58
distances = {vertex : sys .maxsize - 1 for vertex in self .graph }
59
- distances [s ] = 0
59
+ distances [start ] = 0
60
60
61
- for u in self .graph :
62
- for u , v , w in self .edges :
63
- if distances [u ] != sys .maxsize - 1 and distances [u ] + w < distances [v ]:
64
- distances [v ] = distances [u ] + w
61
+ for vertex_a in self .graph :
62
+ for vertex_a , vertex_b , weight in self .edges :
63
+ if distances [vertex_a ] != sys .maxsize - 1 and distances [vertex_a ] + weight < distances [vertex_b ]:
64
+ distances [vertex_b ] = distances [vertex_a ] + weight
65
65
66
66
return distances
67
67
@@ -74,28 +74,28 @@ def johnson_algo(self) -> list[dict]:
74
74
all pairs of vertices using Johnson's algorithm.
75
75
"""
76
76
self .add_vertices ("#" )
77
- for v in self .graph :
78
- if v != "#" :
79
- self .add_edge ("#" , v , 0 )
77
+ for vertex in self .graph :
78
+ if vertex != "#" :
79
+ self .add_edge ("#" , vertex , 0 )
80
80
81
- n = self .bellman_ford ("#" )
81
+ hash_path = self .bellman_ford ("#" )
82
82
83
83
for i in range (len (self .edges )):
84
- u , v , weight = self .edges [i ]
85
- self .edges [i ] = (u , v , weight + n [ u ] - n [ v ])
84
+ vertex_a , vertex_b , weight = self .edges [i ]
85
+ self .edges [i ] = (vertex_a , vertex_b , weight + hash_path [ vertex_a ] - hash_path [ vertex_b ])
86
86
87
87
self .graph .pop ("#" )
88
- self .edges = [(u , v , w ) for u , v , w in self .edges if u != "#" ]
88
+ self .edges = [(vertex1 , vertex2 , node_weight ) for vertex1 , vertex2 , node_weight in self .edges if vertex1 != "#" ]
89
89
90
- for u in self .graph :
91
- self .graph [u ] = [(v , weight ) for x , v , weight in self .edges if x == u ]
90
+ for vertex in self .graph :
91
+ self .graph [vertex ] = [(vertex2 , node_weight ) for vertex1 , vertex2 , node_weight in self .edges if vertex1 == vertex ]
92
92
93
93
distances = []
94
- for u in self .graph :
95
- new_dist = self .dijkstra (u )
96
- for v in self .graph :
97
- if new_dist [v ] < sys .maxsize - 1 :
98
- new_dist [v ] += n [ v ] - n [ u ]
94
+ for vertex1 in self .graph :
95
+ new_dist = self .dijkstra (vertex1 )
96
+ for vertex2 in self .graph :
97
+ if new_dist [vertex2 ] < sys .maxsize - 1 :
98
+ new_dist [vertex2 ] += hash_path [ vertex1 ] - hash_path [ vertex2 ]
99
99
distances .append (new_dist )
100
100
return distances
101
101
0 commit comments