Skip to content

Commit 6d1b7c3

Browse files
committed
Added ford-fulkerson algorith by Bisha18
1 parent 6fab70a commit 6d1b7c3

File tree

2 files changed

+167
-0
lines changed

2 files changed

+167
-0
lines changed
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
package com.thealgorithms.others;
2+
import java.util.*;
3+
4+
public class FordFulkerson {
5+
6+
// Method to perform Breadth-First Search to find an augmenting path
7+
boolean bfs(int rGraph[][], int source, int sink, int parent[]) {
8+
// Create a visited array and mark all vertices as not visited
9+
boolean visited[] = new boolean[rGraph.length];
10+
Arrays.fill(visited, false);
11+
12+
// Create a queue, enqueue source vertex and mark it as visited
13+
LinkedList<Integer> queue = new LinkedList<Integer>();
14+
queue.add(source);
15+
visited[source] = true;
16+
parent[source] = -1;
17+
18+
// Standard BFS loop
19+
while (!queue.isEmpty()) {
20+
int u = queue.poll();
21+
22+
for (int v = 0; v < rGraph.length; v++) {
23+
if (!visited[v] && rGraph[u][v] > 0) { // if not yet visited and there is an edge
24+
if (v == sink) { // if we found the sink
25+
parent[v] = u;
26+
return true;
27+
}
28+
queue.add(v);
29+
parent[v] = u;
30+
visited[v] = true;
31+
}
32+
}
33+
}
34+
35+
return false; // No augmenting path found
36+
}
37+
38+
// Returns the maximum flow from source to sink in the given graph
39+
int fordFulkerson(int graph[][], int source, int sink) {
40+
int u, v;
41+
42+
// Create a residual graph and fill the residual graph with given capacities
43+
int rGraph[][] = new int[graph.length][graph.length];
44+
45+
for (u = 0; u < graph.length; u++) {
46+
for (v = 0; v < graph.length; v++) {
47+
rGraph[u][v] = graph[u][v];
48+
}
49+
}
50+
51+
// This array is filled by BFS and to store the path
52+
int parent[] = new int[graph.length];
53+
54+
int maxFlow = 0; // Initially, no flow
55+
56+
// Augment the flow while there is a path from source to sink
57+
while (bfs(rGraph, source, sink, parent)) {
58+
// Find the maximum flow through the path found by BFS
59+
int pathFlow = Integer.MAX_VALUE;
60+
for (v = sink; v != source; v = parent[v]) {
61+
u = parent[v];
62+
pathFlow = Math.min(pathFlow, rGraph[u][v]);
63+
}
64+
65+
// Update residual capacities of the edges and reverse edges along the path
66+
for (v = sink; v != source; v = parent[v]) {
67+
u = parent[v];
68+
rGraph[u][v] -= pathFlow;
69+
rGraph[v][u] += pathFlow;
70+
}
71+
72+
// Add the path flow to the overall flow
73+
maxFlow += pathFlow;
74+
}
75+
76+
return maxFlow;
77+
}
78+
79+
public static void main(String[] args) {
80+
Scanner scanner = new Scanner(System.in);
81+
82+
System.out.println("Enter the number of vertices:");
83+
int V = scanner.nextInt();
84+
85+
int graph[][] = new int[V][V];
86+
87+
System.out.println("Enter the number of edges:");
88+
int E = scanner.nextInt();
89+
90+
System.out.println("Enter the edges with capacities (u v capacity):");
91+
for (int i = 0; i < E; i++) {
92+
int u = scanner.nextInt();
93+
int v = scanner.nextInt();
94+
int capacity = scanner.nextInt();
95+
graph[u][v] = capacity;
96+
}
97+
98+
System.out.println("Enter the source vertex:");
99+
int source = scanner.nextInt();
100+
101+
System.out.println("Enter the sink vertex:");
102+
int sink = scanner.nextInt();
103+
104+
FordFulkerson maxFlow = new FordFulkerson();
105+
System.out.println("The maximum possible flow is: " + maxFlow.fordFulkerson(graph, source, sink));
106+
107+
scanner.close();
108+
}
109+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.thealgorithms.others;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import org.junit.jupiter.api.Test;
5+
6+
public class FordFulkersonTest {
7+
8+
@Test
9+
void testMaxFlowSmallGraph() {
10+
// Small graph test case
11+
int[][] graph = {
12+
{0, 16, 13, 0, 0, 0},
13+
{0, 0, 10, 12, 0, 0},
14+
{0, 4, 0, 0, 14, 0},
15+
{0, 0, 9, 0, 0, 20},
16+
{0, 0, 0, 7, 0, 4},
17+
{0, 0, 0, 0, 0, 0}
18+
};
19+
int source = 0;
20+
int sink = 5;
21+
FordFulkerson maxFlow = new FordFulkerson();
22+
assertEquals(23, maxFlow.fordFulkerson(graph, source, sink));
23+
}
24+
25+
@Test
26+
void testMaxFlowDisconnectedGraph() {
27+
// Disconnected graph test case (no path from source to sink)
28+
int[][] graph = {
29+
{0, 16, 0, 0, 0, 0},
30+
{0, 0, 10, 0, 0, 0},
31+
{0, 4, 0, 0, 14, 0},
32+
{0, 0, 0, 0, 0, 0},
33+
{0, 0, 0, 7, 0, 0},
34+
{0, 0, 0, 0, 0, 0}
35+
};
36+
int source = 0;
37+
int sink = 5;
38+
FordFulkerson maxFlow = new FordFulkerson();
39+
assertEquals(0, maxFlow.fordFulkerson(graph, source, sink));
40+
}
41+
42+
@Test
43+
void testMaxFlowMediumGraph() {
44+
// Medium graph test case
45+
int[][] graph = {
46+
{0, 10, 10, 0, 0, 0},
47+
{0, 0, 0, 4, 8, 0},
48+
{0, 0, 0, 5, 9, 0},
49+
{0, 0, 0, 0, 0, 10},
50+
{0, 0, 0, 6, 0, 10},
51+
{0, 0, 0, 0, 0, 0}
52+
};
53+
int source = 0;
54+
int sink = 5;
55+
FordFulkerson maxFlow = new FordFulkerson();
56+
assertEquals(19, maxFlow.fordFulkerson(graph, source, sink));
57+
}
58+
}

0 commit comments

Comments
 (0)