Skip to content

Commit 0154a8a

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

File tree

1 file changed

+23
-7
lines changed

1 file changed

+23
-7
lines changed

graphs/bellman_ford.py

+23-7
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,29 @@ 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], predecessor: list[int]) -> bool:
20+
def check_negative_cycle(
21+
graph: list[Edge], distance: list[float], predecessor: list[int]
22+
) -> bool:
2123
"""
2224
Checks if there is a negative weight cycle reachable from the source vertex.
2325
If found, return True, indicating a negative cycle.
2426
"""
2527
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+
):
2732
# 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
2936
return True
3037
return False
3138

3239

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]]:
3443
"""
3544
Reconstructs the shortest paths from the source vertex to each vertex using the predecessor list.
3645
"""
@@ -50,7 +59,9 @@ def reconstruct_paths(predecessor: list[int], vertex_count: int, src: int) -> li
5059
return paths
5160

5261

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]]]:
5465
"""
5566
Returns the shortest paths from a vertex src to all other vertices, including path reconstruction.
5667
"""
@@ -61,7 +72,10 @@ def bellman_ford(graph: list[Edge], vertex_count: int, src: int) -> tuple[list[f
6172
# Step 1: Relax edges repeatedly
6273
for _ in range(vertex_count - 1):
6374
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+
):
6579
distance[edge.dst] = distance[edge.src] + edge.weight
6680
predecessor[edge.dst] = edge.src
6781

@@ -88,7 +102,9 @@ def bellman_ford(graph: list[Edge], vertex_count: int, src: int) -> tuple[list[f
88102

89103
for i in range(E):
90104
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+
)
92108
if src < 0 or src >= V or dest < 0 or dest >= V:
93109
print(f"Invalid vertices: src and dest should be between 0 and {V - 1}")
94110
continue

0 commit comments

Comments
 (0)