@@ -14,20 +14,29 @@ def print_distance_and_paths(distance: List[float], paths: List[List[int]], src:
14
14
print (f"{ vertex } \t \t { dist } \t \t \t \t { path_str } " )
15
15
16
16
17
- def check_negative_cycle (graph : List [Edge ], distance : List [float ], predecessor : List [int ]) -> bool :
17
+ def check_negative_cycle (
18
+ graph : List [Edge ], distance : List [float ], predecessor : List [int ]
19
+ ) -> bool :
18
20
"""
19
21
Checks if there is a negative weight cycle reachable from the source vertex.
20
22
If found, return True, indicating a negative cycle.
21
23
"""
22
24
for edge in graph :
23
- if distance [edge .src ] != float ("inf" ) and distance [edge .src ] + edge .weight < distance [edge .dst ]:
25
+ if (
26
+ distance [edge .src ] != float ("inf" )
27
+ and distance [edge .src ] + edge .weight < distance [edge .dst ]
28
+ ):
24
29
# Update predecessors to indicate a cycle for affected paths
25
- predecessor [edge .dst ] = - 1 # Use -1 as a marker for negative cycle detection
30
+ predecessor [
31
+ edge .dst
32
+ ] = - 1 # Use -1 as a marker for negative cycle detection
26
33
return True
27
34
return False
28
35
29
36
30
- def reconstruct_paths (predecessor : List [int ], vertex_count : int , src : int ) -> List [List [int ]]:
37
+ def reconstruct_paths (
38
+ predecessor : List [int ], vertex_count : int , src : int
39
+ ) -> List [List [int ]]:
31
40
"""
32
41
Reconstructs the shortest paths from the source vertex to each vertex using the predecessor list.
33
42
"""
@@ -47,7 +56,9 @@ def reconstruct_paths(predecessor: List[int], vertex_count: int, src: int) -> Li
47
56
return paths
48
57
49
58
50
- def bellman_ford (graph : List [Edge ], vertex_count : int , src : int ) -> Tuple [List [float ], List [List [int ]]]:
59
+ def bellman_ford (
60
+ graph : List [Edge ], vertex_count : int , src : int
61
+ ) -> Tuple [List [float ], List [List [int ]]]:
51
62
"""
52
63
Returns the shortest paths from a vertex src to all other vertices, including path reconstruction.
53
64
"""
@@ -58,7 +69,10 @@ def bellman_ford(graph: List[Edge], vertex_count: int, src: int) -> Tuple[List[f
58
69
# Step 1: Relax edges repeatedly
59
70
for _ in range (vertex_count - 1 ):
60
71
for edge in graph :
61
- if distance [edge .src ] != float ("inf" ) and distance [edge .src ] + edge .weight < distance [edge .dst ]:
72
+ if (
73
+ distance [edge .src ] != float ("inf" )
74
+ and distance [edge .src ] + edge .weight < distance [edge .dst ]
75
+ ):
62
76
distance [edge .dst ] = distance [edge .src ] + edge .weight
63
77
predecessor [edge .dst ] = edge .src
64
78
@@ -75,6 +89,7 @@ def bellman_ford(graph: List[Edge], vertex_count: int, src: int) -> Tuple[List[f
75
89
if __name__ == "__main__" :
76
90
# Example graph input for testing purposes
77
91
import doctest
92
+
78
93
doctest .testmod ()
79
94
80
95
try :
@@ -85,7 +100,9 @@ def bellman_ford(graph: List[Edge], vertex_count: int, src: int) -> Tuple[List[f
85
100
86
101
for i in range (E ):
87
102
print (f"Edge { i + 1 } " )
88
- src , dest , weight = map (int , input ("Enter source, destination, weight: " ).strip ().split ())
103
+ src , dest , weight = map (
104
+ int , input ("Enter source, destination, weight: " ).strip ().split ()
105
+ )
89
106
if src < 0 or src >= V or dest < 0 or dest >= V :
90
107
print (f"Invalid vertices: src and dest should be between 0 and { V - 1 } " )
91
108
continue
0 commit comments