From 4b546746b19bc1a2d65a7f48bd0f93af917d66a4 Mon Sep 17 00:00:00 2001 From: lighting9999 Date: Fri, 3 Oct 2025 15:01:28 +0800 Subject: [PATCH 01/10] change ford_fulkerson.py --- networking_flow/ford_fulkerson.py | 79 ++++++++++++++++++------------- 1 file changed, 47 insertions(+), 32 deletions(-) diff --git a/networking_flow/ford_fulkerson.py b/networking_flow/ford_fulkerson.py index b47d3b68f3d1..ece8aaecbb71 100644 --- a/networking_flow/ford_fulkerson.py +++ b/networking_flow/ford_fulkerson.py @@ -7,6 +7,9 @@ (2) Choose the augmenting path from source to sink and add the path to flow """ +from collections import deque +from typing import List + graph = [ [0, 16, 13, 0, 0, 0], [0, 0, 10, 12, 0, 0], @@ -17,7 +20,7 @@ ] -def breadth_first_search(graph: list, source: int, sink: int, parents: list) -> bool: +def breadth_first_search(graph: List[List[int]], source: int, sink: int, parents: List[int]) -> bool: """ This function returns True if there is a node that has not iterated. @@ -37,25 +40,31 @@ def breadth_first_search(graph: list, source: int, sink: int, parents: list) -> ... IndexError: list index out of range """ - visited = [False] * len(graph) # Mark all nodes as not visited - queue = [] # breadth-first search queue - - # Source node + num_nodes = len(graph) + visited = [False] * num_nodes + queue = deque() + queue.append(source) visited[source] = True - + while queue: - u = queue.pop(0) # Pop the front node - # Traverse all adjacent nodes of u - for ind, node in enumerate(graph[u]): - if visited[ind] is False and node > 0: - queue.append(ind) - visited[ind] = True - parents[ind] = u + current_node = queue.popleft() + + # If we reached the sink, we can stop early + if current_node == sink: + return True + + # Check all adjacent nodes + for neighbor, capacity in enumerate(graph[current_node]): + if not visited[neighbor] and capacity > 0: + visited[neighbor] = True + parents[neighbor] = current_node + queue.append(neighbor) + return visited[sink] -def ford_fulkerson(graph: list, source: int, sink: int) -> int: +def ford_fulkerson(graph: List[List[int]], source: int, sink: int) -> int: """ This function returns the maximum flow from source to sink in the given graph. @@ -80,28 +89,34 @@ def ford_fulkerson(graph: list, source: int, sink: int) -> int: >>> ford_fulkerson(test_graph, 0, 5) 23 """ - # This array is filled by breadth-first search and to store path - parent = [-1] * (len(graph)) + # Create a copy of the graph to avoid modifying the original + residual_graph = [row[:] for row in graph] + num_nodes = len(residual_graph) + parents = [-1] * num_nodes max_flow = 0 - # While there is a path from source to sink - while breadth_first_search(graph, source, sink, parent): - path_flow = int(1e9) # Infinite value - s = sink - - while s != source: - # Find the minimum value in the selected path - path_flow = min(path_flow, graph[parent[s]][s]) - s = parent[s] - + # Augment the flow while there is a path from source to sink + while breadth_first_search(residual_graph, source, sink, parents): + # Find the minimum residual capacity along the path + path_flow = float('inf') + current_node = sink + + # Find the minimum capacity in the path + while current_node != source: + parent_node = parents[current_node] + path_flow = min(path_flow, residual_graph[parent_node][current_node]) + current_node = parent_node + + # Add path flow to overall flow max_flow += path_flow - v = sink - while v != source: - u = parent[v] - graph[u][v] -= path_flow - graph[v][u] += path_flow - v = parent[v] + # Update residual capacities of the edges and reverse edges + current_node = sink + while current_node != source: + parent_node = parents[current_node] + residual_graph[parent_node][current_node] -= path_flow + residual_graph[current_node][parent_node] += path_flow + current_node = parent_node return max_flow From f9bb554d2ce65ff9fbc61f8282b6bbd1f4e9d158 Mon Sep 17 00:00:00 2001 From: lighting9999 Date: Fri, 3 Oct 2025 15:07:57 +0800 Subject: [PATCH 02/10] fix ford_fulkerson.py --- networking_flow/ford_fulkerson.py | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/networking_flow/ford_fulkerson.py b/networking_flow/ford_fulkerson.py index ece8aaecbb71..c5610bad1518 100644 --- a/networking_flow/ford_fulkerson.py +++ b/networking_flow/ford_fulkerson.py @@ -8,7 +8,6 @@ """ from collections import deque -from typing import List graph = [ [0, 16, 13, 0, 0, 0], @@ -20,7 +19,9 @@ ] -def breadth_first_search(graph: List[List[int]], source: int, sink: int, parents: List[int]) -> bool: +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. @@ -43,28 +44,28 @@ def breadth_first_search(graph: List[List[int]], source: int, sink: int, parents num_nodes = len(graph) visited = [False] * num_nodes queue = deque() - + queue.append(source) visited[source] = True - + while queue: current_node = queue.popleft() - + # If we reached the sink, we can stop early if current_node == sink: return True - + # Check all adjacent nodes for neighbor, capacity in enumerate(graph[current_node]): if not visited[neighbor] and capacity > 0: visited[neighbor] = True parents[neighbor] = current_node queue.append(neighbor) - + return visited[sink] -def ford_fulkerson(graph: List[List[int]], source: int, sink: int) -> int: +def ford_fulkerson(graph: list, source: int, sink: int) -> int: """ This function returns the maximum flow from source to sink in the given graph. @@ -100,7 +101,7 @@ def ford_fulkerson(graph: List[List[int]], source: int, sink: int) -> int: # Find the minimum residual capacity along the path path_flow = float('inf') current_node = sink - + # Find the minimum capacity in the path while current_node != source: parent_node = parents[current_node] @@ -125,4 +126,4 @@ def ford_fulkerson(graph: List[List[int]], source: int, sink: int) -> int: from doctest import testmod testmod() - print(f"{ford_fulkerson(graph, source=0, sink=5) = }") + print(f"{ford_fulkerson(graph, source=0, sink=5) = }") \ No newline at end of file From 84e449c1c9ddfdb0717b7893c1a04c6b0507ca4e Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 3 Oct 2025 07:10:36 +0000 Subject: [PATCH 03/10] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- networking_flow/ford_fulkerson.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/networking_flow/ford_fulkerson.py b/networking_flow/ford_fulkerson.py index c5610bad1518..c57afb48e886 100644 --- a/networking_flow/ford_fulkerson.py +++ b/networking_flow/ford_fulkerson.py @@ -19,9 +19,7 @@ ] -def breadth_first_search( - graph: list, source: int, sink: int, parents: list -) -> bool: +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. @@ -99,7 +97,7 @@ def ford_fulkerson(graph: list, source: int, sink: int) -> int: # Augment the flow while there is a path from source to sink while breadth_first_search(residual_graph, source, sink, parents): # Find the minimum residual capacity along the path - path_flow = float('inf') + path_flow = float("inf") current_node = sink # Find the minimum capacity in the path @@ -126,4 +124,4 @@ def ford_fulkerson(graph: list, source: int, sink: int) -> int: from doctest import testmod testmod() - print(f"{ford_fulkerson(graph, source=0, sink=5) = }") \ No newline at end of file + print(f"{ford_fulkerson(graph, source=0, sink=5) = }") From d8c0f5de7128b7d6136df515fe156ee3b513ce08 Mon Sep 17 00:00:00 2001 From: lighting9999 Date: Fri, 3 Oct 2025 15:18:11 +0800 Subject: [PATCH 04/10] fix typo for ford_fulkerson.py --- networking_flow/ford_fulkerson.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/networking_flow/ford_fulkerson.py b/networking_flow/ford_fulkerson.py index c57afb48e886..f8587d885e34 100644 --- a/networking_flow/ford_fulkerson.py +++ b/networking_flow/ford_fulkerson.py @@ -19,7 +19,9 @@ ] -def breadth_first_search(graph: list, source: int, sink: int, parents: list) -> bool: +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. @@ -41,7 +43,7 @@ def breadth_first_search(graph: list, source: int, sink: int, parents: list) -> """ num_nodes = len(graph) visited = [False] * num_nodes - queue = deque() + queue: deque[int] = deque() queue.append(source) visited[source] = True @@ -97,7 +99,7 @@ def ford_fulkerson(graph: list, source: int, sink: int) -> int: # Augment the flow while there is a path from source to sink while breadth_first_search(residual_graph, source, sink, parents): # Find the minimum residual capacity along the path - path_flow = float("inf") + path_flow = 10**9 # Large integer instead of float current_node = sink # Find the minimum capacity in the path @@ -124,4 +126,4 @@ def ford_fulkerson(graph: list, source: int, sink: int) -> int: from doctest import testmod testmod() - print(f"{ford_fulkerson(graph, source=0, sink=5) = }") + print(f"{ford_fulkerson(graph, source=0, sink=5) = }") \ No newline at end of file From d2e5cc72f2ffcdf575ed6d7587d3732ba3604497 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 3 Oct 2025 07:18:31 +0000 Subject: [PATCH 05/10] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- networking_flow/ford_fulkerson.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/networking_flow/ford_fulkerson.py b/networking_flow/ford_fulkerson.py index f8587d885e34..e1f6fb28f7e5 100644 --- a/networking_flow/ford_fulkerson.py +++ b/networking_flow/ford_fulkerson.py @@ -19,9 +19,7 @@ ] -def breadth_first_search( - graph: list, source: int, sink: int, parents: list -) -> bool: +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. @@ -126,4 +124,4 @@ def ford_fulkerson(graph: list, source: int, sink: int) -> int: from doctest import testmod testmod() - print(f"{ford_fulkerson(graph, source=0, sink=5) = }") \ No newline at end of file + print(f"{ford_fulkerson(graph, source=0, sink=5) = }") From a436886984ac8e6da773b02b9033db51764205aa Mon Sep 17 00:00:00 2001 From: lighting9999 Date: Sun, 12 Oct 2025 19:27:05 +0800 Subject: [PATCH 06/10] Change ford_fulkerson.py --- networking_flow/ford_fulkerson.py | 1 + 1 file changed, 1 insertion(+) diff --git a/networking_flow/ford_fulkerson.py b/networking_flow/ford_fulkerson.py index e1f6fb28f7e5..498e692e063d 100644 --- a/networking_flow/ford_fulkerson.py +++ b/networking_flow/ford_fulkerson.py @@ -9,6 +9,7 @@ from collections import deque + graph = [ [0, 16, 13, 0, 0, 0], [0, 0, 10, 12, 0, 0], From 1bb6d2668bff36dd0bd8c3cc19c32349e6d8127d Mon Sep 17 00:00:00 2001 From: lighting9999 Date: Sun, 12 Oct 2025 19:34:06 +0800 Subject: [PATCH 07/10] fix I001 Error for ford_fulkerson.py --- networking_flow/ford_fulkerson.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/networking_flow/ford_fulkerson.py b/networking_flow/ford_fulkerson.py index 498e692e063d..4e7ddee3f34e 100644 --- a/networking_flow/ford_fulkerson.py +++ b/networking_flow/ford_fulkerson.py @@ -7,8 +7,6 @@ (2) Choose the augmenting path from source to sink and add the path to flow """ -from collections import deque - graph = [ [0, 16, 13, 0, 0, 0], From fa3f95d9579702b640b872c238d4aa14bdc528b6 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 12 Oct 2025 11:34:26 +0000 Subject: [PATCH 08/10] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- networking_flow/ford_fulkerson.py | 1 - 1 file changed, 1 deletion(-) diff --git a/networking_flow/ford_fulkerson.py b/networking_flow/ford_fulkerson.py index 4e7ddee3f34e..b7171bee0517 100644 --- a/networking_flow/ford_fulkerson.py +++ b/networking_flow/ford_fulkerson.py @@ -7,7 +7,6 @@ (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], From 4f2650aa9fbbd2c78b9df542f246c972c1614bf9 Mon Sep 17 00:00:00 2001 From: lighting9999 Date: Sun, 12 Oct 2025 19:38:22 +0800 Subject: [PATCH 09/10] fix ford_fulkerson.py --- networking_flow/ford_fulkerson.py | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/networking_flow/ford_fulkerson.py b/networking_flow/ford_fulkerson.py index b7171bee0517..fda7cfedcbf7 100644 --- a/networking_flow/ford_fulkerson.py +++ b/networking_flow/ford_fulkerson.py @@ -28,24 +28,21 @@ def breadth_first_search(graph: list, source: int, sink: int, parents: list) -> parents: Parent list Returns: - True if there is a node that has not iterated. + True if there is a path from source to sink >>> breadth_first_search(graph, 0, 5, [-1, -1, -1, -1, -1, -1]) True - >>> breadth_first_search(graph, 0, 6, [-1, -1, -1, -1, -1, -1]) - Traceback (most recent call last): - ... - IndexError: list index out of range """ num_nodes = len(graph) visited = [False] * num_nodes - queue: deque[int] = deque() + queue = [] # Using list instead of deque queue.append(source) visited[source] = True while queue: - current_node = queue.popleft() + # Use pop(0) to simulate deque's popleft() + current_node = queue.pop(0) # If we reached the sink, we can stop early if current_node == sink: @@ -95,7 +92,7 @@ def ford_fulkerson(graph: list, source: int, sink: int) -> int: # Augment the flow while there is a path from source to sink while breadth_first_search(residual_graph, source, sink, parents): # Find the minimum residual capacity along the path - path_flow = 10**9 # Large integer instead of float + path_flow = float('inf') current_node = sink # Find the minimum capacity in the path @@ -122,4 +119,4 @@ def ford_fulkerson(graph: list, source: int, sink: int) -> int: from doctest import testmod testmod() - print(f"{ford_fulkerson(graph, source=0, sink=5) = }") + print(f"{ford_fulkerson(graph, source=0, sink=5) = }") \ No newline at end of file From c54023f4687dfb484ad441e0df37b495133342fd Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 12 Oct 2025 11:38:54 +0000 Subject: [PATCH 10/10] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- networking_flow/ford_fulkerson.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/networking_flow/ford_fulkerson.py b/networking_flow/ford_fulkerson.py index fda7cfedcbf7..52c77902c68b 100644 --- a/networking_flow/ford_fulkerson.py +++ b/networking_flow/ford_fulkerson.py @@ -92,7 +92,7 @@ def ford_fulkerson(graph: list, source: int, sink: int) -> int: # Augment the flow while there is a path from source to sink while breadth_first_search(residual_graph, source, sink, parents): # Find the minimum residual capacity along the path - path_flow = float('inf') + path_flow = float("inf") current_node = sink # Find the minimum capacity in the path @@ -119,4 +119,4 @@ def ford_fulkerson(graph: list, source: int, sink: int) -> int: from doctest import testmod testmod() - print(f"{ford_fulkerson(graph, source=0, sink=5) = }") \ No newline at end of file + print(f"{ford_fulkerson(graph, source=0, sink=5) = }")