8
8
class JohnsonGraph :
9
9
def __init__ (self ) -> None :
10
10
self .edges : list [str ] = []
11
- self .graph : dict [str , int ] = {}
11
+ self .graph : dict [str , list ] = {}
12
12
13
13
# add vertices for a graph
14
- def add_vertices (self , u ) -> None :
14
+ def add_vertices (self , u : int ) -> None :
15
15
self .graph [u ] = []
16
16
17
17
# assign weights for each edges formed of the directed graph
18
- def add_edge (self , u , v , w ) -> None :
18
+ def add_edge (self , u : str , v : str , w : int ) -> None :
19
19
self .edges .append ((u , v , w ))
20
20
self .graph [u ].append ((v , w ))
21
21
22
22
# perform a dijkstra algorithm on a directed graph
23
- def dijkstra (self , s ) -> dict :
23
+ def dijkstra (self , s : str ) -> dict :
24
24
distances = {vertex : sys .maxsize - 1 for vertex in self .graph }
25
25
pq = [(0 , s )]
26
26
distances [s ] = 0
@@ -37,7 +37,7 @@ def dijkstra(self, s) -> dict:
37
37
return distances
38
38
39
39
# carry out the bellman ford algorithm for a node and estimate its distance vector
40
- def bellman_ford (self , s ) -> dict :
40
+ def bellman_ford (self , s : str ) -> dict :
41
41
distances = {vertex : sys .maxsize - 1 for vertex in self .graph }
42
42
distances [s ] = 0
43
43
@@ -51,7 +51,7 @@ def bellman_ford(self, s) -> dict:
51
51
# perform the johnson algorithm to handle the negative weights that
52
52
# could not be handled by either the dijkstra
53
53
# or the bellman ford algorithm efficiently
54
- def johnson_algo (self ) -> dict :
54
+ def johnson_algo (self ) -> list [ dict ] :
55
55
self .add_vertices ("#" )
56
56
for v in self .graph :
57
57
if v != "#" :
0 commit comments