@@ -17,22 +17,28 @@ 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 (graph : list [Edge ], distance : list [float ],
21
+ predecessor : list [int ]) -> bool :
21
22
"""
22
23
Checks if there is a negative weight cycle reachable from the source vertex.
23
24
If found, return True, indicating a negative cycle.
24
25
"""
25
26
for edge in graph :
26
- if distance [edge .src ] != float ("inf" ) and distance [edge .src ] + edge .weight < distance [edge .dst ]:
27
+ if (distance [edge .src ] != float ("inf" ) and
28
+ distance [edge .src ] + edge .weight
29
+ < distance [edge .dst ]):
27
30
# Update predecessors to indicate a cycle for affected paths
28
- predecessor [edge .dst ] = - 1 # Use -1 as a marker for negative cycle detection
31
+ # Use -1 as a marker for negative cycle detection
32
+ predecessor [edge .dst ] = - 1
29
33
return True
30
34
return False
31
35
32
36
33
- def reconstruct_paths (predecessor : list [int ], vertex_count : int , src : int ) -> list [list [int ]]:
37
+ def reconstruct_paths (predecessor : list [int ],
38
+ vertex_count : int , src : int ) -> list [list [int ]]:
34
39
"""
35
- Reconstructs the shortest paths from the source vertex to each vertex using the predecessor list.
40
+ Reconstructs the shortest paths from the source vertex to
41
+ each vertex using the predecessor list.
36
42
"""
37
43
paths = [[] for _ in range (vertex_count )]
38
44
for vertex in range (vertex_count ):
@@ -50,9 +56,11 @@ def reconstruct_paths(predecessor: list[int], vertex_count: int, src: int) -> li
50
56
return paths
51
57
52
58
53
- def bellman_ford (graph : list [Edge ], vertex_count : int , src : int ) -> tuple [list [float ], list [list [int ]]]:
59
+ def bellman_ford (graph : list [Edge ],
60
+ vertex_count : int , src : int ) -> tuple [list [float ], list [list [int ]]]:
54
61
"""
55
- Returns the shortest paths from a vertex src to all other vertices, including path reconstruction.
62
+ Returns the shortest paths from a vertex src to all other vertices,
63
+ including path reconstruction.
56
64
"""
57
65
distance = [float ("inf" )] * vertex_count
58
66
predecessor = [None ] * vertex_count # Keeps track of the path predecessors
@@ -61,7 +69,8 @@ def bellman_ford(graph: list[Edge], vertex_count: int, src: int) -> tuple[list[f
61
69
# Step 1: Relax edges repeatedly
62
70
for _ in range (vertex_count - 1 ):
63
71
for edge in graph :
64
- if distance [edge .src ] != float ("inf" ) and distance [edge .src ] + edge .weight < distance [edge .dst ]:
72
+ if (distance [edge .src ] != float ("inf" ) and
73
+ distance [edge .src ] + edge .weight < distance [edge .dst ]):
65
74
distance [edge .dst ] = distance [edge .src ] + edge .weight
66
75
predecessor [edge .dst ] = edge .src
67
76
@@ -88,7 +97,8 @@ def bellman_ford(graph: list[Edge], vertex_count: int, src: int) -> tuple[list[f
88
97
89
98
for i in range (E ):
90
99
print (f"Edge { i + 1 } " )
91
- src , dest , weight = map (int , input ("Enter source, destination, weight: " ).strip ().split ())
100
+ src , dest , weight = map (int ,
101
+ input ("Enter source, destination, weight: " ).strip ().split ())
92
102
if src < 0 or src >= V or dest < 0 or dest >= V :
93
103
print (f"Invalid vertices: src and dest should be between 0 and { V - 1 } " )
94
104
continue
@@ -101,6 +111,4 @@ def bellman_ford(graph: list[Edge], vertex_count: int, src: int) -> tuple[list[f
101
111
shortest_distance , paths = bellman_ford (graph , V , source )
102
112
print_distance_and_paths (shortest_distance , paths , source )
103
113
except ValueError :
104
- print ("Please enter valid integer inputs." )
105
- except Exception as e :
106
- print (e )
114
+ print ("Invalid input. Please enter valid integers." )
0 commit comments