-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathMaxFlowExampleTest.java
More file actions
executable file
·385 lines (348 loc) · 11.9 KB
/
MaxFlowExampleTest.java
File metadata and controls
executable file
·385 lines (348 loc) · 11.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
/**
* JUnit 5 test suite for {@link MaxFlowExample}.
*
* <p>Covers the three package-private utility methods:
* <ul>
* <li>{@link MaxFlowExample#bfs(int[][], int, int, int[])}</li>
* <li>{@link MaxFlowExample#edmondsKarp(int[][], int, int)}</li>
* <li>{@link MaxFlowExample#getFlowDetails(int[][], int[][])}</li>
* </ul>
*
* @author Generated Test Suite (claude.ai)
* @author Dr. Jody Paul
* @version CS4050 - Spring 2026
*/
public class MaxFlowExampleTest {
/**
* The 6-node benchmark graph used in {@code main()}.
* Known maximum flow from node 0 to node 5 is 23.
*/
private static int[][] benchmarkGraph() {
return new int[][] {
{0, 16, 13, 0, 0, 0},
{0, 0, 10, 12, 0, 0},
{0, 4, 0, 0, 14, 0},
{0, 0, 9, 0, 0, 20},
{0, 0, 0, 7, 0, 4},
{0, 0, 0, 0, 0, 0}
};
}
/**
* Default constructor for test class MaxFlowExampleTest
*/
public MaxFlowExampleTest() { }
/**
* Set up the test fixture.
* Called before every test case method.
*/
@BeforeEach
public void setUp() { }
/**
* Tear down the test fixture.
* Called after every test case method.
*/
@AfterEach
public void tearDown() { }
// ==============
// bfs() tests
// ==============
@Test
public void returnsTrueForDirectEdge() {
int[][] graph = {
{0, 10},
{0, 0}
};
int[] parent = new int[2];
assertTrue(MaxFlowExample.bfs(graph, 0, 1, parent));
}
@Test
public void recordsParentForDirectEdge() {
int[][] graph = {
{0, 5},
{0, 0}
};
int[] parent = new int[2];
MaxFlowExample.bfs(graph, 0, 1, parent);
assertEquals(0, parent[1], "Parent of sink should be source for a direct edge");
}
@Test
public void returnsFalseWhenNoPath() {
int[][] graph = {
{0, 0},
{0, 0}
};
int[] parent = new int[2];
assertFalse(MaxFlowExample.bfs(graph, 0, 1, parent));
}
@Test
public void returnsFalseWhenCapacityExhausted() {
int[][] graph = {
{0, 0},
{0, 0}
};
int[] parent = new int[2];
assertFalse(MaxFlowExample.bfs(graph, 0, 1, parent),
"Zero-capacity edge should not be traversable");
}
@Test
public void findsMultiHopPath() {
// 0 -> 1 -> 2
int[][] graph = {
{0, 8, 0},
{0, 0, 5},
{0, 0, 0}
};
int[] parent = new int[3];
assertTrue(MaxFlowExample.bfs(graph, 0, 2, parent));
assertEquals(1, parent[2], "Parent of node 2 should be node 1");
assertEquals(0, parent[1], "Parent of node 1 should be node 0");
}
@Test
public void sourceParentIsSentinel() {
int[][] graph = {
{0, 3},
{0, 0}
};
int[] parent = new int[2];
MaxFlowExample.bfs(graph, 0, 1, parent);
assertEquals(-1, parent[0], "Source node parent sentinel should be -1");
}
@Test
public void singleNodeGraph() {
int[][] graph = {{0}};
int[] parent = new int[1];
// source == sink; BFS adds source to queue but the sink check (v == t)
// is only reached for neighbors, so no path is reported.
assertFalse(MaxFlowExample.bfs(graph, 0, 0, parent));
}
@Test
public void skipsZeroCapacityEdges() {
// Only path 0->2 has capacity; 0->1->2 has a zero edge at 0->1
int[][] graph = {
{0, 0, 6},
{0, 0, 4},
{0, 0, 0}
};
int[] parent = new int[3];
assertTrue(MaxFlowExample.bfs(graph, 0, 2, parent));
// Direct path should be used; parent[2] must be 0
assertEquals(0, parent[2]);
}
@Test
public void benchmarkGraphHasPath() {
int[] parent = new int[6];
assertTrue(MaxFlowExample.bfs(benchmarkGraph(), 0, 5, parent));
}
// ===================
// edmondsKarp() tests
// ===================
@Test
void benchmarkGraphMaxFlow() {
MaxFlowExample.MaxFlowResult result =
MaxFlowExample.edmondsKarp(benchmarkGraph(), 0, 5);
assertEquals(23, result.maxFlow());
}
@Test
void noPathReturnsZeroFlow() {
int[][] graph = {
{0, 0},
{0, 0}
};
MaxFlowExample.MaxFlowResult result =
MaxFlowExample.edmondsKarp(graph, 0, 1);
assertEquals(0, result.maxFlow());
}
@Test
public void singleEdgeGraph() {
int[][] graph = {
{0, 7},
{0, 0}
};
MaxFlowExample.MaxFlowResult result =
MaxFlowExample.edmondsKarp(graph, 0, 1);
assertEquals(7, result.maxFlow());
}
@Test
public void bottleneckLimitsFlow() {
// Path: 0 --(10)--> 1 --(3)--> 2; bottleneck is 3
int[][] graph = {
{0, 10, 0},
{0, 0, 3},
{0, 0, 0}
};
MaxFlowExample.MaxFlowResult result =
MaxFlowExample.edmondsKarp(graph, 0, 2);
assertEquals(3, result.maxFlow());
}
@Test
public void parallelPathsFlow() {
// 0->1->3 (cap 5) and 0->2->3 (cap 4); total max = 9
int[][] graph = {
{0, 5, 4, 0},
{0, 0, 0, 5},
{0, 0, 0, 4},
{0, 0, 0, 0}
};
MaxFlowExample.MaxFlowResult result =
MaxFlowExample.edmondsKarp(graph, 0, 3);
assertEquals(9, result.maxFlow());
}
@Test
public void residualGraphIsNonNull() {
MaxFlowExample.MaxFlowResult result =
MaxFlowExample.edmondsKarp(benchmarkGraph(), 0, 5);
assertNotNull(result.residualGraph());
}
@Test
public void residualGraphDimensions() {
int n = 6;
MaxFlowExample.MaxFlowResult result =
MaxFlowExample.edmondsKarp(benchmarkGraph(), 0, 5);
assertEquals(n, result.residualGraph().length);
for (int[] row : result.residualGraph()) {
assertEquals(n, row.length);
}
}
@Test
public void flowConservationAtSink() {
int[][] original = benchmarkGraph();
int sink = 5;
MaxFlowExample.MaxFlowResult result =
MaxFlowExample.edmondsKarp(original, 0, sink);
// Flow into sink = sum over all u of (original[u][sink] - residual[u][sink])
int flowIntoSink = 0;
for (int u = 0; u < original.length; u++) {
flowIntoSink += original[u][sink] - result.residualGraph()[u][sink];
}
assertEquals(result.maxFlow(), flowIntoSink);
}
@Test
public void doesNotMutateOriginalGraph() {
int[][] original = benchmarkGraph();
int[][] copy = benchmarkGraph();
MaxFlowExample.edmondsKarp(original, 0, 5);
assertArrayEquals(copy, original, "edmondsKarp must not modify the input graph");
}
@Test
public void zeroCapacityEdge() {
int[][] graph = {
{0, 0},
{0, 0}
};
MaxFlowExample.MaxFlowResult result =
MaxFlowExample.edmondsKarp(graph, 0, 1);
assertEquals(0, result.maxFlow());
}
// ======================
// getFlowDetails() tests
// ======================
@Test
public void containsHeader() {
int[][] graph = benchmarkGraph();
MaxFlowExample.MaxFlowResult result = MaxFlowExample.edmondsKarp(graph, 0, 5);
String details = MaxFlowExample.getFlowDetails(graph, result.residualGraph());
assertTrue(details.contains("Edge_# -> Flow / Capacity"));
}
@Test
public void containsSeparator() {
int[][] graph = benchmarkGraph();
MaxFlowExample.MaxFlowResult result = MaxFlowExample.edmondsKarp(graph, 0, 5);
String details = MaxFlowExample.getFlowDetails(graph, result.residualGraph());
assertTrue(details.contains("-----------------------"));
}
@Test
public void outputsOneLinePerEdge() {
int[][] graph = benchmarkGraph();
MaxFlowExample.MaxFlowResult result = MaxFlowExample.edmondsKarp(graph, 0, 5);
String details = MaxFlowExample.getFlowDetails(graph, result.residualGraph());
// Count edges in original graph
int edgeCount = 0;
for (int[] row : graph) {
for (int cap : row) {
if (cap > 0) edgeCount++;
}
}
// Count "Edge X -> Y:" occurrences
long lineCount = details.lines()
.filter(l -> l.startsWith("Edge "))
.count();
assertEquals(edgeCount, lineCount);
}
@Test
public void flowNeverExceedsCapacity() {
int[][] graph = benchmarkGraph();
MaxFlowExample.MaxFlowResult result = MaxFlowExample.edmondsKarp(graph, 0, 5);
String details = MaxFlowExample.getFlowDetails(graph, result.residualGraph());
details.lines()
.filter(l -> l.startsWith("Edge "))
.forEach(line -> {
// Format: "Edge u -> v: flow / capacity"
String[] parts = line.split(":")[1].trim().split("/");
int flow = Integer.parseInt(parts[0].trim());
int capacity = Integer.parseInt(parts[1].trim());
assertTrue(flow <= capacity,
"Flow must not exceed capacity on line: " + line);
});
}
@Test
public void flowValuesAreNonNegative() {
int[][] graph = benchmarkGraph();
MaxFlowExample.MaxFlowResult result = MaxFlowExample.edmondsKarp(graph, 0, 5);
String details = MaxFlowExample.getFlowDetails(graph, result.residualGraph());
details.lines()
.filter(l -> l.startsWith("Edge "))
.forEach(line -> {
String[] parts = line.split(":")[1].trim().split("/");
int flow = Integer.parseInt(parts[0].trim());
assertTrue(flow >= 0, "Flow must be non-negative on line: " + line);
});
}
@Test
public void zeroFlowGraph() {
int[][] graph = {
{0, 5},
{0, 0}
};
// Residual same as original but fully used up (simulate 0 flow manually)
int[][] residual = {
{0, 5},
{0, 0}
};
String details = MaxFlowExample.getFlowDetails(graph, residual);
// flow = original - residual = 5 - 5 = 0
assertTrue(details.contains("Edge 0 -> 1: 0 / 5"));
}
@Test
public void omitsZeroCapacityEntries() {
int[][] graph = {
{0, 0},
{5, 0}
};
int[][] residual = {
{0, 0},
{5, 0}
};
String details = MaxFlowExample.getFlowDetails(graph, residual);
// Only edge 1->0 is a real edge; edge 0->1 should not appear
assertFalse(details.contains("Edge 0 -> 1"),
"Zero-capacity edge 0->1 should not appear in output");
assertTrue(details.contains("Edge 1 -> 0"),
"Real edge 1->0 should appear in output");
}
@Test
public void saturatedEdgeReflectsFullCapacity() {
int[][] original = {
{0, 8},
{0, 0}
};
MaxFlowExample.MaxFlowResult result =
MaxFlowExample.edmondsKarp(original, 0, 1);
String details = MaxFlowExample.getFlowDetails(original, result.residualGraph());
assertTrue(details.contains("Edge 0 -> 1: 8 / 8"),
"Saturated edge should show flow equal to capacity");
}
}