Skip to content

Commit ca0845e

Browse files
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent d4a8dcc commit ca0845e

File tree

1 file changed

+20
-13
lines changed

1 file changed

+20
-13
lines changed

graphs/bellman_ford.py

+20-13
Original file line numberDiff line numberDiff line change
@@ -17,25 +17,28 @@ def print_distance_and_paths(distance: list[float], paths: list[list[int]], src:
1717
print(f"{vertex}\t\t{dist}\t\t\t\t{path_str}")
1818

1919

20-
def check_negative_cycle(graph: list[Edge], distance: list[float],
21-
predecessor: list[int]) -> bool:
20+
def check_negative_cycle(
21+
graph: list[Edge], distance: list[float], predecessor: list[int]
22+
) -> bool:
2223
"""
2324
Checks if there is a negative weight cycle reachable from the source vertex.
2425
If found, return True, indicating a negative cycle.
2526
"""
2627
for edge in graph:
27-
if (distance[edge.src] != float("inf") and
28-
distance[edge.src] + edge.weight
29-
< distance[edge.dst]):
28+
if (
29+
distance[edge.src] != float("inf")
30+
and distance[edge.src] + edge.weight < distance[edge.dst]
31+
):
3032
# Update predecessors to indicate a cycle for affected paths
3133
# Use -1 as a marker for negative cycle detection
3234
predecessor[edge.dst] = -1
3335
return True
3436
return False
3537

3638

37-
def reconstruct_paths(predecessor: list[int],
38-
vertex_count: int, src: int) -> list[list[int]]:
39+
def reconstruct_paths(
40+
predecessor: list[int], vertex_count: int, src: int
41+
) -> list[list[int]]:
3942
"""
4043
Reconstructs the shortest paths from the source vertex to
4144
each vertex using the predecessor list.
@@ -56,8 +59,9 @@ def reconstruct_paths(predecessor: list[int],
5659
return paths
5760

5861

59-
def bellman_ford(graph: list[Edge],
60-
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]]]:
6165
"""
6266
Returns the shortest paths from a vertex src to all other vertices,
6367
including path reconstruction.
@@ -69,8 +73,10 @@ def bellman_ford(graph: list[Edge],
6973
# Step 1: Relax edges repeatedly
7074
for _ in range(vertex_count - 1):
7175
for edge in graph:
72-
if (distance[edge.src] != float("inf") and
73-
distance[edge.src] + edge.weight < distance[edge.dst]):
76+
if (
77+
distance[edge.src] != float("inf")
78+
and distance[edge.src] + edge.weight < distance[edge.dst]
79+
):
7480
distance[edge.dst] = distance[edge.src] + edge.weight
7581
predecessor[edge.dst] = edge.src
7682

@@ -97,8 +103,9 @@ def bellman_ford(graph: list[Edge],
97103

98104
for i in range(E):
99105
print(f"Edge {i + 1}")
100-
src, dest, weight = map(int,
101-
input("Enter source, destination, weight: ").strip().split())
106+
src, dest, weight = map(
107+
int, input("Enter source, destination, weight: ").strip().split()
108+
)
102109
if src < 0 or src >= V or dest < 0 or dest >= V:
103110
print(f"Invalid vertices: src and dest should be between 0 and {V - 1}")
104111
continue

0 commit comments

Comments
 (0)