From 7ae1534e6855ba671e77646ba277739b80fef70f Mon Sep 17 00:00:00 2001 From: lighting9999 <120090117+lighting9999@users.noreply.github.com> Date: Sat, 7 Jun 2025 16:26:52 +0800 Subject: [PATCH 1/2] fix problem ford_fulkerson.py --- networking_flow/ford_fulkerson.py | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/networking_flow/ford_fulkerson.py b/networking_flow/ford_fulkerson.py index b47d3b68f3d1..5d44365b6f4e 100644 --- a/networking_flow/ford_fulkerson.py +++ b/networking_flow/ford_fulkerson.py @@ -1,12 +1,3 @@ -""" -Ford-Fulkerson Algorithm for Maximum Flow Problem -* https://en.wikipedia.org/wiki/Ford%E2%80%93Fulkerson_algorithm - -Description: - (1) Start with initial flow as 0 - (2) Choose the augmenting path from source to sink and add the path to flow -""" - graph = [ [0, 16, 13, 0, 0, 0], [0, 0, 10, 12, 0, 0], @@ -16,7 +7,6 @@ [0, 0, 0, 0, 0, 0], ] - def breadth_first_search(graph: list, source: int, sink: int, parents: list) -> bool: """ This function returns True if there is a node that has not iterated. @@ -54,7 +44,6 @@ def breadth_first_search(graph: list, source: int, sink: int, parents: list) -> parents[ind] = u return visited[sink] - def ford_fulkerson(graph: list, source: int, sink: int) -> int: """ This function returns the maximum flow from source to sink in the given graph. @@ -99,15 +88,20 @@ def ford_fulkerson(graph: list, source: int, sink: int) -> int: while v != source: u = parent[v] + # Update residual capacities graph[u][v] -= path_flow + # Ensure reverse edge exists + if graph[v][u] == 0: + graph[v][u] = 0 # Explicitly initialize if needed (though usually already 0) graph[v][u] += path_flow v = parent[v] return max_flow - if __name__ == "__main__": from doctest import testmod testmod() - print(f"{ford_fulkerson(graph, source=0, sink=5) = }") + # Make a copy of the original graph to preserve it + graph_copy = [row[:] for row in graph] + print(f"{ford_fulkerson(graph_copy, source=0, sink=5) = }") From 13e974e7f20260a907464d608e492eae1adf5745 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 7 Jun 2025 08:29:38 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- networking_flow/ford_fulkerson.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/networking_flow/ford_fulkerson.py b/networking_flow/ford_fulkerson.py index 5d44365b6f4e..b613b7505ae9 100644 --- a/networking_flow/ford_fulkerson.py +++ b/networking_flow/ford_fulkerson.py @@ -7,6 +7,7 @@ [0, 0, 0, 0, 0, 0], ] + def breadth_first_search(graph: list, source: int, sink: int, parents: list) -> bool: """ This function returns True if there is a node that has not iterated. @@ -44,6 +45,7 @@ def breadth_first_search(graph: list, source: int, sink: int, parents: list) -> parents[ind] = u return visited[sink] + def ford_fulkerson(graph: list, source: int, sink: int) -> int: """ This function returns the maximum flow from source to sink in the given graph. @@ -92,12 +94,15 @@ def ford_fulkerson(graph: list, source: int, sink: int) -> int: graph[u][v] -= path_flow # Ensure reverse edge exists if graph[v][u] == 0: - graph[v][u] = 0 # Explicitly initialize if needed (though usually already 0) + graph[v][u] = ( + 0 # Explicitly initialize if needed (though usually already 0) + ) graph[v][u] += path_flow v = parent[v] return max_flow + if __name__ == "__main__": from doctest import testmod