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