Skip to content

Commit 520e464

Browse files
authored
Enhance docs, add more tests in FordFulkerson (#5953)
1 parent 4f7957f commit 520e464

File tree

2 files changed

+140
-0
lines changed

2 files changed

+140
-0
lines changed

src/main/java/com/thealgorithms/datastructures/graphs/FordFulkerson.java

+18
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,30 @@
33
import java.util.LinkedList;
44
import java.util.Queue;
55

6+
/**
7+
* This class implements the Ford-Fulkerson algorithm to compute the maximum flow
8+
* in a flow network.
9+
*
10+
* <p>The algorithm uses breadth-first search (BFS) to find augmenting paths from
11+
* the source vertex to the sink vertex, updating the flow in the network until
12+
* no more augmenting paths can be found.</p>
13+
*/
614
public final class FordFulkerson {
715
private static final int INF = Integer.MAX_VALUE;
816

917
private FordFulkerson() {
1018
}
1119

20+
/**
21+
* Computes the maximum flow in a flow network using the Ford-Fulkerson algorithm.
22+
*
23+
* @param vertexCount the number of vertices in the flow network
24+
* @param capacity a 2D array representing the capacity of edges in the network
25+
* @param flow a 2D array representing the current flow in the network
26+
* @param source the source vertex in the flow network
27+
* @param sink the sink vertex in the flow network
28+
* @return the total maximum flow from the source to the sink
29+
*/
1230
public static int networkFlow(int vertexCount, int[][] capacity, int[][] flow, int source, int sink) {
1331
int totalFlow = 0;
1432

src/test/java/com/thealgorithms/datastructures/graphs/FordFulkersonTest.java

+122
Original file line numberDiff line numberDiff line change
@@ -91,4 +91,126 @@ public void testComplexNetwork() {
9191
int maxFlow = FordFulkerson.networkFlow(vertexCount, capacity, flow, 0, 4);
9292
assertEquals(19, maxFlow);
9393
}
94+
95+
@Test
96+
public void testLargeNetwork() {
97+
int vertexCount = 8;
98+
int[][] capacity = new int[vertexCount][vertexCount];
99+
int[][] flow = new int[vertexCount][vertexCount];
100+
101+
// Setting up a large network
102+
capacity[0][1] = 10;
103+
capacity[0][2] = 5;
104+
capacity[1][3] = 15;
105+
capacity[2][3] = 10;
106+
capacity[1][4] = 10;
107+
capacity[3][5] = 10;
108+
capacity[4][5] = 5;
109+
capacity[4][6] = 10;
110+
capacity[5][7] = 10;
111+
capacity[6][7] = 15;
112+
113+
int maxFlow = FordFulkerson.networkFlow(vertexCount, capacity, flow, 0, 7);
114+
assertEquals(15, maxFlow); // Maximum flow should be 15
115+
}
116+
117+
@Test
118+
public void testMultipleSourcesAndSinks() {
119+
int vertexCount = 7;
120+
int[][] capacity = new int[vertexCount][vertexCount];
121+
int[][] flow = new int[vertexCount][vertexCount];
122+
123+
// Creating multiple sources and sinks scenario
124+
capacity[0][1] = 10; // Source 1
125+
capacity[0][2] = 5;
126+
capacity[1][3] = 15;
127+
capacity[2][3] = 10;
128+
capacity[3][4] = 10; // Sink 1
129+
capacity[3][5] = 5;
130+
capacity[3][6] = 10; // Sink 2
131+
capacity[5][6] = 10;
132+
133+
int maxFlow = FordFulkerson.networkFlow(vertexCount, capacity, flow, 0, 4);
134+
assertEquals(10, maxFlow); // Maximum flow should be 10
135+
}
136+
137+
@Test
138+
public void testDisconnectedGraph() {
139+
int vertexCount = 6;
140+
int[][] capacity = new int[vertexCount][vertexCount];
141+
int[][] flow = new int[vertexCount][vertexCount];
142+
143+
// No connection between source and sink
144+
capacity[0][1] = 10; // Only one edge not connected to the sink
145+
capacity[1][2] = 10;
146+
capacity[3][4] = 10;
147+
148+
int maxFlow = FordFulkerson.networkFlow(vertexCount, capacity, flow, 0, 5);
149+
assertEquals(0, maxFlow); // No flow should be possible
150+
}
151+
152+
@Test
153+
public void testZeroCapacityEdge() {
154+
int vertexCount = 4;
155+
int[][] capacity = new int[vertexCount][vertexCount];
156+
int[][] flow = new int[vertexCount][vertexCount];
157+
158+
// Including a zero capacity edge
159+
capacity[0][1] = 10;
160+
capacity[0][2] = 0; // Zero capacity
161+
capacity[1][3] = 5;
162+
capacity[2][3] = 10;
163+
164+
int maxFlow = FordFulkerson.networkFlow(vertexCount, capacity, flow, 0, 3);
165+
assertEquals(5, maxFlow); // Flow only possible through 0 -> 1 -> 3
166+
}
167+
168+
@Test
169+
public void testAllEdgesZeroCapacity() {
170+
int vertexCount = 5;
171+
int[][] capacity = new int[vertexCount][vertexCount];
172+
int[][] flow = new int[vertexCount][vertexCount];
173+
174+
// All edges with zero capacity
175+
capacity[0][1] = 0;
176+
capacity[1][2] = 0;
177+
capacity[2][3] = 0;
178+
capacity[3][4] = 0;
179+
180+
int maxFlow = FordFulkerson.networkFlow(vertexCount, capacity, flow, 0, 4);
181+
assertEquals(0, maxFlow); // No flow should be possible
182+
}
183+
184+
@Test
185+
public void testCycleGraph() {
186+
int vertexCount = 4;
187+
int[][] capacity = new int[vertexCount][vertexCount];
188+
int[][] flow = new int[vertexCount][vertexCount];
189+
190+
// Setting up a cycle
191+
capacity[0][1] = 10;
192+
capacity[1][2] = 5;
193+
capacity[2][0] = 5; // This creates a cycle
194+
capacity[1][3] = 15;
195+
capacity[2][3] = 10;
196+
197+
int maxFlow = FordFulkerson.networkFlow(vertexCount, capacity, flow, 0, 3);
198+
assertEquals(10, maxFlow); // Maximum flow should be 10
199+
}
200+
201+
@Test
202+
public void testFlowWithExcessCapacity() {
203+
int vertexCount = 5;
204+
int[][] capacity = new int[vertexCount][vertexCount];
205+
int[][] flow = new int[vertexCount][vertexCount];
206+
207+
// Extra capacity in the flow
208+
capacity[0][1] = 20;
209+
capacity[1][2] = 10;
210+
capacity[2][3] = 15;
211+
capacity[1][3] = 5;
212+
213+
int maxFlow = FordFulkerson.networkFlow(vertexCount, capacity, flow, 0, 3);
214+
assertEquals(15, maxFlow); // Maximum flow should be 15 (20 from 0->1 and 10->2, limited by 15->3)
215+
}
94216
}

0 commit comments

Comments
 (0)