From e9eb5f8437f234f817aff8d9fdb0be074efcd66c Mon Sep 17 00:00:00 2001 From: Vishnunair585 Date: Fri, 24 Apr 2026 18:54:24 +0530 Subject: [PATCH 1/2] Optimize BFS and fix min-cut detection --- networking_flow/minimum_cut.py | 56 ++++++++++++++++++++++------------ 1 file changed, 37 insertions(+), 19 deletions(-) diff --git a/networking_flow/minimum_cut.py b/networking_flow/minimum_cut.py index 164b45f1012a..b5a8f6807d31 100644 --- a/networking_flow/minimum_cut.py +++ b/networking_flow/minimum_cut.py @@ -1,5 +1,7 @@ # Minimum cut on Ford_Fulkerson algorithm. +from collections import deque + test_graph = [ [0, 16, 13, 0, 0, 0], [0, 0, 10, 12, 0, 0], @@ -11,56 +13,72 @@ def bfs(graph, s, t, parent): - # Return True if there is node that has not iterated. visited = [False] * len(graph) - queue = [s] + queue = deque([s]) visited[s] = True while queue: - u = queue.pop(0) + u = queue.popleft() + for ind in range(len(graph[u])): - if visited[ind] is False and graph[u][ind] > 0: + if not visited[ind] and graph[u][ind] > 0: queue.append(ind) visited[ind] = True parent[ind] = u + # Early exit if sink is found + if ind == t: + return True + return visited[t] +def dfs(graph, s, visited): + visited[s] = True + for i in range(len(graph)): + if graph[s][i] > 0 and not visited[i]: + dfs(graph, i, visited) + + def mincut(graph, source, sink): - """This array is filled by BFS and to store path + """ + Returns max_flow and minimum cut edges. + >>> mincut(test_graph, source=0, sink=5) - [(1, 3), (4, 3), (4, 5)] + (23, [(1, 3), (4, 3), (4, 5)]) """ - parent = [-1] * (len(graph)) + residual = [i[:] for i in graph] + parent = [-1] * len(graph) max_flow = 0 - res = [] - temp = [i[:] for i in graph] # Record original cut, copy. - while bfs(graph, source, sink, parent): + + while bfs(residual, source, sink, parent): path_flow = float("Inf") s = sink while s != source: - # Find the minimum value in select path - path_flow = min(path_flow, graph[parent[s]][s]) + path_flow = min(path_flow, residual[parent[s]][s]) s = parent[s] max_flow += path_flow - v = sink + v = sink while v != source: u = parent[v] - graph[u][v] -= path_flow - graph[v][u] += path_flow + residual[u][v] -= path_flow + residual[v][u] += path_flow v = parent[v] + visited = [False] * len(graph) + dfs(residual, source, visited) + + res = [] for i in range(len(graph)): - for j in range(len(graph[0])): - if graph[i][j] == 0 and temp[i][j] > 0: + for j in range(len(graph)): + if visited[i] and not visited[j] and graph[i][j] > 0: res.append((i, j)) - return res + return max_flow, res if __name__ == "__main__": - print(mincut(test_graph, source=0, sink=5)) + print(mincut(test_graph, source=0, sink=5)) \ No newline at end of file From cf993135940a74628813528aaec2eedc296e0ae9 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 24 Apr 2026 13:25:21 +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/minimum_cut.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/networking_flow/minimum_cut.py b/networking_flow/minimum_cut.py index b5a8f6807d31..1124ad2be21c 100644 --- a/networking_flow/minimum_cut.py +++ b/networking_flow/minimum_cut.py @@ -81,4 +81,4 @@ def mincut(graph, source, sink): if __name__ == "__main__": - print(mincut(test_graph, source=0, sink=5)) \ No newline at end of file + print(mincut(test_graph, source=0, sink=5))