From 46e513dcbb7799b601c9c5e307b7aaad196646d5 Mon Sep 17 00:00:00 2001 From: Sunny Singh Date: Thu, 1 Oct 2020 18:03:11 +0530 Subject: [PATCH] Create MaxFlow Ford Fulkerson --- src/algo/graph/MaxFlow Ford Fulkerson | 33 +++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 src/algo/graph/MaxFlow Ford Fulkerson diff --git a/src/algo/graph/MaxFlow Ford Fulkerson b/src/algo/graph/MaxFlow Ford Fulkerson new file mode 100644 index 0000000..23d9fbf --- /dev/null +++ b/src/algo/graph/MaxFlow Ford Fulkerson @@ -0,0 +1,33 @@ +// https://en.wikipedia.org/wiki/Ford–Fulkerson_algorithm in O(V^2 * flow) +public class MaxFlowFordFulkerson { + public static int maxFlow(int[][] cap, int s, int t) { + for (int flow = 0;;) { + int df = findPath(cap, new boolean[cap.length], s, t, Integer.MAX_VALUE); + if (df == 0) + return flow; + flow += df; + } + } + + static int findPath(int[][] cap, boolean[] vis, int u, int t, int f) { + if (u == t) + return f; + vis[u] = true; + for (int v = 0; v < vis.length; v++) + if (!vis[v] && cap[u][v] > 0) { + int df = findPath(cap, vis, v, t, Math.min(f, cap[u][v])); + if (df > 0) { + cap[u][v] -= df; + cap[v][u] += df; + return df; + } + } + return 0; + } + + // Usage example + public static void main(String[] args) { + int[][] capacity = {{0, 3, 2}, {0, 0, 2}, {0, 0, 0}}; + System.out.println(4 == maxFlow(capacity, 0, 2)); + } +}