Skip to content

Commit 40e312a

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

File tree

1 file changed

+24
-7
lines changed

1 file changed

+24
-7
lines changed

graphs/bellman_ford.py

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,29 @@ def print_distance_and_paths(distance: List[float], paths: List[List[int]], src:
1414
print(f"{vertex}\t\t{dist}\t\t\t\t{path_str}")
1515

1616

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:
1820
"""
1921
Checks if there is a negative weight cycle reachable from the source vertex.
2022
If found, return True, indicating a negative cycle.
2123
"""
2224
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+
):
2429
# 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
2633
return True
2734
return False
2835

2936

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]]:
3140
"""
3241
Reconstructs the shortest paths from the source vertex to each vertex using the predecessor list.
3342
"""
@@ -47,7 +56,9 @@ def reconstruct_paths(predecessor: List[int], vertex_count: int, src: int) -> Li
4756
return paths
4857

4958

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]]]:
5162
"""
5263
Returns the shortest paths from a vertex src to all other vertices, including path reconstruction.
5364
"""
@@ -58,7 +69,10 @@ def bellman_ford(graph: List[Edge], vertex_count: int, src: int) -> Tuple[List[f
5869
# Step 1: Relax edges repeatedly
5970
for _ in range(vertex_count - 1):
6071
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+
):
6276
distance[edge.dst] = distance[edge.src] + edge.weight
6377
predecessor[edge.dst] = edge.src
6478

@@ -75,6 +89,7 @@ def bellman_ford(graph: List[Edge], vertex_count: int, src: int) -> Tuple[List[f
7589
if __name__ == "__main__":
7690
# Example graph input for testing purposes
7791
import doctest
92+
7893
doctest.testmod()
7994

8095
try:
@@ -85,7 +100,9 @@ def bellman_ford(graph: List[Edge], vertex_count: int, src: int) -> Tuple[List[f
85100

86101
for i in range(E):
87102
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+
)
89106
if src < 0 or src >= V or dest < 0 or dest >= V:
90107
print(f"Invalid vertices: src and dest should be between 0 and {V - 1}")
91108
continue

0 commit comments

Comments
 (0)