|
7 | 7 | import org.junit.jupiter.api.Test; |
8 | 8 |
|
9 | 9 | class DinicTest { |
10 | | - @Test |
11 | | - @DisplayName("Classic CLRS network yields max flow 23 (Dinic)") |
12 | | - void clrsExample() { |
13 | | - int[][] capacity = {{0, 16, 13, 0, 0, 0}, {0, 0, 10, 12, 0, 0}, {0, 4, 0, 0, 14, 0}, |
14 | | - {0, 0, 9, 0, 0, 20}, {0, 0, 0, 7, 0, 4}, {0, 0, 0, 0, 0, 0}}; |
15 | | - int maxFlow = Dinic.maxFlow(capacity, 0, 5); |
16 | | - assertEquals(23, maxFlow); |
17 | | - } |
| 10 | + @Test |
| 11 | + @DisplayName("Classic CLRS network yields max flow 23 (Dinic)") |
| 12 | + void clrsExample() { |
| 13 | + int[][] capacity = {{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}}; |
| 14 | + int maxFlow = Dinic.maxFlow(capacity, 0, 5); |
| 15 | + assertEquals(23, maxFlow); |
| 16 | + } |
18 | 17 |
|
19 | | - @Test |
20 | | - @DisplayName("Disconnected network has zero flow (Dinic)") |
21 | | - void disconnectedGraph() { |
22 | | - int[][] capacity = {{0, 0, 0}, {0, 0, 0}, {0, 0, 0}}; |
23 | | - int maxFlow = Dinic.maxFlow(capacity, 0, 2); |
24 | | - assertEquals(0, maxFlow); |
25 | | - } |
| 18 | + @Test |
| 19 | + @DisplayName("Disconnected network has zero flow (Dinic)") |
| 20 | + void disconnectedGraph() { |
| 21 | + int[][] capacity = {{0, 0, 0}, {0, 0, 0}, {0, 0, 0}}; |
| 22 | + int maxFlow = Dinic.maxFlow(capacity, 0, 2); |
| 23 | + assertEquals(0, maxFlow); |
| 24 | + } |
26 | 25 |
|
27 | | - @Test |
28 | | - @DisplayName("Source equals sink returns zero (Dinic)") |
29 | | - void sourceEqualsSink() { |
30 | | - int[][] capacity = {{0, 5}, {0, 0}}; |
31 | | - int maxFlow = Dinic.maxFlow(capacity, 0, 0); |
32 | | - assertEquals(0, maxFlow); |
33 | | - } |
| 26 | + @Test |
| 27 | + @DisplayName("Source equals sink returns zero (Dinic)") |
| 28 | + void sourceEqualsSink() { |
| 29 | + int[][] capacity = {{0, 5}, {0, 0}}; |
| 30 | + int maxFlow = Dinic.maxFlow(capacity, 0, 0); |
| 31 | + assertEquals(0, maxFlow); |
| 32 | + } |
34 | 33 |
|
35 | | - @Test |
36 | | - @DisplayName("Invalid matrix throws exception (Dinic)") |
37 | | - void invalidMatrix() { |
38 | | - int[][] capacity = {{0, 1}, {1}}; |
39 | | - assertThrows(IllegalArgumentException.class, () -> Dinic.maxFlow(capacity, 0, 1)); |
40 | | - } |
| 34 | + @Test |
| 35 | + @DisplayName("Invalid matrix throws exception (Dinic)") |
| 36 | + void invalidMatrix() { |
| 37 | + int[][] capacity = {{0, 1}, {1}}; |
| 38 | + assertThrows(IllegalArgumentException.class, () -> Dinic.maxFlow(capacity, 0, 1)); |
| 39 | + } |
41 | 40 |
|
42 | | - @Test |
43 | | - @DisplayName("Dinic matches Edmonds-Karp on random small graphs") |
44 | | - void parityWithEdmondsKarp() { |
45 | | - java.util.Random rnd = new java.util.Random(42); |
46 | | - for (int n = 3; n <= 7; n++) { |
47 | | - for (int it = 0; it < 25; it++) { |
48 | | - int[][] cap = new int[n][n]; |
49 | | - for (int i = 0; i < n; i++) { |
50 | | - for (int j = 0; j < n; j++) { |
51 | | - if (i != j && rnd.nextDouble() < 0.35) { |
52 | | - cap[i][j] = rnd.nextInt(10); // capacities 0..9 |
| 41 | + @Test |
| 42 | + @DisplayName("Dinic matches Edmonds-Karp on random small graphs") |
| 43 | + void parityWithEdmondsKarp() { |
| 44 | + java.util.Random rnd = new java.util.Random(42); |
| 45 | + for (int n = 3; n <= 7; n++) { |
| 46 | + for (int it = 0; it < 25; it++) { |
| 47 | + int[][] cap = new int[n][n]; |
| 48 | + for (int i = 0; i < n; i++) { |
| 49 | + for (int j = 0; j < n; j++) { |
| 50 | + if (i != j && rnd.nextDouble() < 0.35) { |
| 51 | + cap[i][j] = rnd.nextInt(10); // capacities 0..9 |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | + int s = 0; |
| 56 | + int t = n - 1; |
| 57 | + int f1 = Dinic.maxFlow(copyMatrix(cap), s, t); |
| 58 | + int f2 = EdmondsKarp.maxFlow(cap, s, t); |
| 59 | + assertEquals(f2, f1); |
53 | 60 | } |
54 | | - } |
55 | 61 | } |
56 | | - int s = 0; |
57 | | - int t = n - 1; |
58 | | - int f1 = Dinic.maxFlow(copyMatrix(cap), s, t); |
59 | | - int f2 = EdmondsKarp.maxFlow(cap, s, t); |
60 | | - assertEquals(f2, f1); |
61 | | - } |
62 | 62 | } |
63 | | - } |
64 | 63 |
|
65 | | - private static int[][] copyMatrix(int[][] a) { |
66 | | - int[][] b = new int[a.length][a.length]; |
67 | | - for (int i = 0; i < a.length; i++) { |
68 | | - b[i] = java.util.Arrays.copyOf(a[i], a[i].length); |
| 64 | + private static int[][] copyMatrix(int[][] a) { |
| 65 | + int[][] b = new int[a.length][a.length]; |
| 66 | + for (int i = 0; i < a.length; i++) { |
| 67 | + b[i] = java.util.Arrays.copyOf(a[i], a[i].length); |
| 68 | + } |
| 69 | + return b; |
69 | 70 | } |
70 | | - return b; |
71 | | - } |
72 | 71 | } |
0 commit comments