Repository: TheAlgorithms/Java
Pull request #5384 (2024-08-24 20:24:39)

refactor: `FordFulkerson`
Class cleanup.
Readability improvements.
Added tests.

<!--
Thank you for your contribution!
In order to reduce the number of notifications sent to the maintainers, please:
- create your PR as draft, cf. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/about-pull-requests#draft-pull-requests,
- make sure that all of the CI checks pass,
- mark your PR as ready for review, cf. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/changing-the-stage-of-a-pull-request#marking-a-pull-request-as-ready-for-review
-->

<!-- For completed items, change [ ] to [x] -->

- [x] I have read [CONTRIBUTING.md](https://github.com/TheAlgorithms/Java/blob/master/CONTRIBUTING.md).
- [x] This pull request is all my own work -- I have not plagiarized it.
- [x] All filenames are in PascalCase.
- [x] All functions and variable names follow Java naming conventions.
- [x] All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.
- [x] All new code is formatted with `clang-format -i --style=file path/to/your/file.java`

codecov-commenter: ## [Codecov](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5384?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) Report
Attention: Patch coverage is `96.00000%` with `1 line` in your changes missing coverage. Please review.
> Project coverage is 47.14%. Comparing base [(`b231a72`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/b231a72d449420c6dc7004bc5969285aea7ec956?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) to head [(`61e922e`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/61e922e5df886bd3ba36ee9ad5e456e175ef3b75?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).

| [Files](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5384?dropdown=coverage&src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) | Patch % | Lines |
|---|---|---|
| [...healgorithms/dynamicprogramming/FordFulkerson.java](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5384?src=pr&el=tree&filepath=src%2Fmain%2Fjava%2Fcom%2Fthealgorithms%2Fdynamicprogramming%2FFordFulkerson.java&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms#diff-c3JjL21haW4vamF2YS9jb20vdGhlYWxnb3JpdGhtcy9keW5hbWljcHJvZ3JhbW1pbmcvRm9yZEZ1bGtlcnNvbi5qYXZh) | 96.00% | [1 Missing :warning: ](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5384?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) |

<details><summary>Additional details and impacted files</summary>


```diff
@@             Coverage Diff              @@
##             master    #5384      +/-   ##
============================================
+ Coverage     46.90%   47.14%   +0.24%     
- Complexity     2903     2913      +10     
============================================
  Files           525      525              
  Lines         15363    15346      -17     
  Branches       2908     2907       -1     
============================================
+ Hits           7206     7235      +29     
+ Misses         7855     7810      -45     
+ Partials        302      301       -1     
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5384?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).


Patch:
diff --git a/src/main/java/com/thealgorithms/dynamicprogramming/FordFulkerson.java b/src/main/java/com/thealgorithms/dynamicprogramming/FordFulkerson.java
index 6168ec6e..76995f1c 100644
--- a/src/main/java/com/thealgorithms/dynamicprogramming/FordFulkerson.java
+++ b/src/main/java/com/thealgorithms/dynamicprogramming/FordFulkerson.java
@@ -2,76 +2,54 @@ package com.thealgorithms.dynamicprogramming;
 
 import java.util.LinkedList;
 import java.util.Queue;
-import java.util.Vector;
 
 public final class FordFulkerson {
-    private FordFulkerson() {
-    }
-
-    static final int INF = 987654321;
-    // edges
-    static int vertexCount;
-    static int[][] capacity;
-    static int[][] flow;
+    private static final int INF = Integer.MAX_VALUE;
 
-    public static void main(String[] args) {
-        System.out.println("Vertex Count : 6");
-        vertexCount = 6;
-        capacity = new int[vertexCount][vertexCount];
-
-        capacity[0][1] = 12;
-        capacity[0][3] = 13;
-        capacity[1][2] = 10;
-        capacity[2][3] = 13;
-        capacity[2][4] = 3;
-        capacity[2][5] = 15;
-        capacity[3][2] = 7;
-        capacity[3][4] = 15;
-        capacity[4][5] = 17;
-
-        System.out.println("Max capacity in networkFlow : " + networkFlow(0, 5));
+    private FordFulkerson() {
     }
 
-    private static int networkFlow(int source, int sink) {
-        flow = new int[vertexCount][vertexCount];
+    public static int networkFlow(int vertexCount, int[][] capacity, int[][] flow, int source, int sink) {
         int totalFlow = 0;
+
         while (true) {
-            Vector<Integer> parent = new Vector<>(vertexCount);
-            for (int i = 0; i < vertexCount; i++) {
-                parent.add(-1);
-            }
-            Queue<Integer> q = new LinkedList<>();
-            parent.set(source, source);
-            q.add(source);
-            while (!q.isEmpty() && parent.get(sink) == -1) {
-                int here = q.peek();
-                q.poll();
-                for (int there = 0; there < vertexCount; ++there) {
-                    if (capacity[here][there] - flow[here][there] > 0 && parent.get(there) == -1) {
-                        q.add(there);
-                        parent.set(there, here);
+            int[] parent = new int[vertexCount];
+            boolean[] visited = new boolean[vertexCount];
+            Queue<Integer> queue = new LinkedList<>();
+
+            queue.add(source);
+            visited[source] = true;
+            parent[source] = -1;
+
+            while (!queue.isEmpty() && !visited[sink]) {
+                int current = queue.poll();
+
+                for (int next = 0; next < vertexCount; next++) {
+                    if (!visited[next] && capacity[current][next] - flow[current][next] > 0) {
+                        queue.add(next);
+                        visited[next] = true;
+                        parent[next] = current;
                     }
                 }
             }
-            if (parent.get(sink) == -1) {
-                break;
+
+            if (!visited[sink]) {
+                break; // No more augmenting paths
             }
 
-            int amount = INF;
-            String printer = "path : ";
-            StringBuilder sb = new StringBuilder();
-            for (int p = sink; p != source; p = parent.get(p)) {
-                amount = Math.min(capacity[parent.get(p)][p] - flow[parent.get(p)][p], amount);
-                sb.append(p + "-");
+            int pathFlow = INF;
+            for (int v = sink; v != source; v = parent[v]) {
+                int u = parent[v];
+                pathFlow = Math.min(pathFlow, capacity[u][v] - flow[u][v]);
             }
-            sb.append(source);
-            for (int p = sink; p != source; p = parent.get(p)) {
-                flow[parent.get(p)][p] += amount;
-                flow[p][parent.get(p)] -= amount;
+
+            for (int v = sink; v != source; v = parent[v]) {
+                int u = parent[v];
+                flow[u][v] += pathFlow;
+                flow[v][u] -= pathFlow;
             }
-            totalFlow += amount;
-            printer += sb.reverse() + " / max flow : " + totalFlow;
-            System.out.println(printer);
+
+            totalFlow += pathFlow;
         }
 
         return totalFlow;


Test patch:
diff --git a/src/test/java/com/thealgorithms/dynamicprogramming/FordFulkersonTest.java b/src/test/java/com/thealgorithms/dynamicprogramming/FordFulkersonTest.java
new file mode 100644
index 00000000..d4d38ed5
--- /dev/null
+++ b/src/test/java/com/thealgorithms/dynamicprogramming/FordFulkersonTest.java
@@ -0,0 +1,94 @@
+package com.thealgorithms.dynamicprogramming;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+import org.junit.jupiter.api.Test;
+
+public class FordFulkersonTest {
+    @Test
+    public void testMaxFlow() {
+        int vertexCount = 6;
+        int[][] capacity = new int[vertexCount][vertexCount];
+        int[][] flow = new int[vertexCount][vertexCount];
+
+        // Setting up the capacity graph
+        capacity[0][1] = 12;
+        capacity[0][3] = 13;
+        capacity[1][2] = 10;
+        capacity[2][3] = 13;
+        capacity[2][4] = 3;
+        capacity[2][5] = 15;
+        capacity[3][2] = 7;
+        capacity[3][4] = 15;
+        capacity[4][5] = 17;
+
+        int maxFlow = FordFulkerson.networkFlow(vertexCount, capacity, flow, 0, 5);
+        assertEquals(23, maxFlow);
+    }
+
+    @Test
+    public void testNoFlow() {
+        int vertexCount = 6;
+        int[][] capacity = new int[vertexCount][vertexCount];
+        int[][] flow = new int[vertexCount][vertexCount];
+
+        // No connections between source and sink
+        capacity[0][1] = 10;
+        capacity[2][3] = 10;
+
+        int maxFlow = FordFulkerson.networkFlow(vertexCount, capacity, flow, 1, 4);
+        assertEquals(0, maxFlow);
+    }
+
+    @Test
+    public void testSinglePath() {
+        int vertexCount = 6;
+        int[][] capacity = new int[vertexCount][vertexCount];
+        int[][] flow = new int[vertexCount][vertexCount];
+
+        // Setting up a single path from source to sink
+        capacity[0][1] = 5;
+        capacity[1][2] = 5;
+        capacity[2][3] = 5;
+        capacity[3][4] = 5;
+        capacity[4][5] = 5;
+
+        int maxFlow = FordFulkerson.networkFlow(vertexCount, capacity, flow, 0, 5);
+        assertEquals(5, maxFlow);
+    }
+
+    @Test
+    public void testParallelPaths() {
+        int vertexCount = 4;
+        int[][] capacity = new int[vertexCount][vertexCount];
+        int[][] flow = new int[vertexCount][vertexCount];
+
+        // Setting up parallel paths from source to sink
+        capacity[0][1] = 10;
+        capacity[0][2] = 10;
+        capacity[1][3] = 10;
+        capacity[2][3] = 10;
+
+        int maxFlow = FordFulkerson.networkFlow(vertexCount, capacity, flow, 0, 3);
+        assertEquals(20, maxFlow);
+    }
+
+    @Test
+    public void testComplexNetwork() {
+        int vertexCount = 5;
+        int[][] capacity = new int[vertexCount][vertexCount];
+        int[][] flow = new int[vertexCount][vertexCount];
+
+        // Complex network
+        capacity[0][1] = 10;
+        capacity[0][2] = 10;
+        capacity[1][3] = 4;
+        capacity[1][4] = 8;
+        capacity[2][4] = 9;
+        capacity[3][2] = 6;
+        capacity[3][4] = 10;
+
+        int maxFlow = FordFulkerson.networkFlow(vertexCount, capacity, flow, 0, 4);
+        assertEquals(19, maxFlow);
+    }
+}