From 40e312af1b6197a9f775b880ff19a9caeb0399d7 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 10 Oct 2024 22:19:44 +0000 Subject: [PATCH] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- graphs/bellman_ford.py | 31 ++++++++++++++++++++++++------- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/graphs/bellman_ford.py b/graphs/bellman_ford.py index f69572d90..79985c1bc 100644 --- a/graphs/bellman_ford.py +++ b/graphs/bellman_ford.py @@ -14,20 +14,29 @@ def print_distance_and_paths(distance: List[float], paths: List[List[int]], src: print(f"{vertex}\t\t{dist}\t\t\t\t{path_str}") -def check_negative_cycle(graph: List[Edge], distance: List[float], predecessor: List[int]) -> bool: +def check_negative_cycle( + graph: List[Edge], distance: List[float], predecessor: List[int] +) -> bool: """ Checks if there is a negative weight cycle reachable from the source vertex. If found, return True, indicating a negative cycle. """ for edge in graph: - if distance[edge.src] != float("inf") and distance[edge.src] + edge.weight < distance[edge.dst]: + if ( + distance[edge.src] != float("inf") + and distance[edge.src] + edge.weight < distance[edge.dst] + ): # Update predecessors to indicate a cycle for affected paths - predecessor[edge.dst] = -1 # Use -1 as a marker for negative cycle detection + predecessor[ + edge.dst + ] = -1 # Use -1 as a marker for negative cycle detection return True return False -def reconstruct_paths(predecessor: List[int], vertex_count: int, src: int) -> List[List[int]]: +def reconstruct_paths( + predecessor: List[int], vertex_count: int, src: int +) -> List[List[int]]: """ Reconstructs the shortest paths from the source vertex to each vertex using the predecessor list. """ @@ -47,7 +56,9 @@ def reconstruct_paths(predecessor: List[int], vertex_count: int, src: int) -> Li return paths -def bellman_ford(graph: List[Edge], vertex_count: int, src: int) -> Tuple[List[float], List[List[int]]]: +def bellman_ford( + graph: List[Edge], vertex_count: int, src: int +) -> Tuple[List[float], List[List[int]]]: """ Returns the shortest paths from a vertex src to all other vertices, including path reconstruction. """ @@ -58,7 +69,10 @@ def bellman_ford(graph: List[Edge], vertex_count: int, src: int) -> Tuple[List[f # Step 1: Relax edges repeatedly for _ in range(vertex_count - 1): for edge in graph: - if distance[edge.src] != float("inf") and distance[edge.src] + edge.weight < distance[edge.dst]: + if ( + distance[edge.src] != float("inf") + and distance[edge.src] + edge.weight < distance[edge.dst] + ): distance[edge.dst] = distance[edge.src] + edge.weight predecessor[edge.dst] = edge.src @@ -75,6 +89,7 @@ def bellman_ford(graph: List[Edge], vertex_count: int, src: int) -> Tuple[List[f if __name__ == "__main__": # Example graph input for testing purposes import doctest + doctest.testmod() try: @@ -85,7 +100,9 @@ if __name__ == "__main__": for i in range(E): print(f"Edge {i + 1}") - src, dest, weight = map(int, input("Enter source, destination, weight: ").strip().split()) + src, dest, weight = map( + int, input("Enter source, destination, weight: ").strip().split() + ) if src < 0 or src >= V or dest < 0 or dest >= V: print(f"Invalid vertices: src and dest should be between 0 and {V - 1}") continue