Skip to content

Commit ff31380

Browse files
committed
style: apply clang-format to Dinic and tests
1 parent 839e34b commit ff31380

File tree

2 files changed

+125
-126
lines changed

2 files changed

+125
-126
lines changed

src/main/java/com/thealgorithms/graph/Dinic.java

Lines changed: 73 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -21,86 +21,86 @@
2121
* @see <a href="https://en.wikipedia.org/wiki/Dinic%27s_algorithm">Wikipedia: Dinic's algorithm</a>
2222
*/
2323
public final class Dinic {
24-
private Dinic() { }
25-
26-
/**
27-
* Computes the maximum flow from source to sink using Dinic's algorithm.
28-
*
29-
* @param capacity square capacity matrix (n x n); entries must be >= 0
30-
* @param source source vertex index in [0, n)
31-
* @param sink sink vertex index in [0, n)
32-
* @return the maximum flow value
33-
* @throws IllegalArgumentException if the input matrix is null/non-square/has negatives or
34-
* indices invalid
35-
*/
36-
public static int maxFlow(int[][] capacity, int source, int sink) {
37-
if (capacity == null || capacity.length == 0) {
38-
throw new IllegalArgumentException("Capacity matrix must not be null or empty");
24+
private Dinic() {
3925
}
40-
final int n = capacity.length;
41-
for (int i = 0; i < n; i++) {
42-
if (capacity[i] == null || capacity[i].length != n) {
43-
throw new IllegalArgumentException("Capacity matrix must be square");
44-
}
45-
for (int j = 0; j < n; j++) {
46-
if (capacity[i][j] < 0) {
47-
throw new IllegalArgumentException("Capacities must be non-negative");
26+
27+
/**
28+
* Computes the maximum flow from source to sink using Dinic's algorithm.
29+
*
30+
* @param capacity square capacity matrix (n x n); entries must be >= 0
31+
* @param source source vertex index in [0, n)
32+
* @param sink sink vertex index in [0, n)
33+
* @return the maximum flow value
34+
* @throws IllegalArgumentException if the input matrix is null/non-square/has negatives or
35+
* indices invalid
36+
*/
37+
public static int maxFlow(int[][] capacity, int source, int sink) {
38+
if (capacity == null || capacity.length == 0) {
39+
throw new IllegalArgumentException("Capacity matrix must not be null or empty");
40+
}
41+
final int n = capacity.length;
42+
for (int i = 0; i < n; i++) {
43+
if (capacity[i] == null || capacity[i].length != n) {
44+
throw new IllegalArgumentException("Capacity matrix must be square");
45+
}
46+
for (int j = 0; j < n; j++) {
47+
if (capacity[i][j] < 0) {
48+
throw new IllegalArgumentException("Capacities must be non-negative");
49+
}
50+
}
51+
}
52+
if (source < 0 || sink < 0 || source >= n || sink >= n) {
53+
throw new IllegalArgumentException("Source and sink must be valid vertex indices");
54+
}
55+
if (source == sink) {
56+
return 0;
4857
}
49-
}
50-
}
51-
if (source < 0 || sink < 0 || source >= n || sink >= n) {
52-
throw new IllegalArgumentException("Source and sink must be valid vertex indices");
53-
}
54-
if (source == sink) {
55-
return 0;
56-
}
5758

58-
// residual capacities
59-
int[][] residual = new int[n][n];
60-
for (int i = 0; i < n; i++) {
61-
residual[i] = Arrays.copyOf(capacity[i], n);
62-
}
59+
// residual capacities
60+
int[][] residual = new int[n][n];
61+
for (int i = 0; i < n; i++) {
62+
residual[i] = Arrays.copyOf(capacity[i], n);
63+
}
6364

64-
int[] level = new int[n];
65-
int flow = 0;
66-
while (bfsBuildLevelGraph(residual, source, sink, level)) {
67-
int[] next = new int[n]; // current-edge optimization
68-
int pushed;
69-
do {
70-
pushed = dfsBlocking(residual, level, next, source, sink, Integer.MAX_VALUE);
71-
flow += pushed;
72-
} while (pushed > 0);
65+
int[] level = new int[n];
66+
int flow = 0;
67+
while (bfsBuildLevelGraph(residual, source, sink, level)) {
68+
int[] next = new int[n]; // current-edge optimization
69+
int pushed;
70+
do {
71+
pushed = dfsBlocking(residual, level, next, source, sink, Integer.MAX_VALUE);
72+
flow += pushed;
73+
} while (pushed > 0);
74+
}
75+
return flow;
7376
}
74-
return flow;
75-
}
7677

77-
private static boolean bfsBuildLevelGraph(int[][] residual, int source, int sink, int[] level) {
78-
Arrays.fill(level, -1);
79-
level[source] = 0;
80-
Queue<Integer> q = new ArrayDeque<>();
81-
q.add(source);
82-
while (!q.isEmpty()) {
83-
int u = q.poll();
84-
for (int v = 0; v < residual.length; v++) {
85-
if (residual[u][v] > 0 && level[v] == -1) {
86-
level[v] = level[u] + 1;
87-
if (v == sink) {
88-
return true;
89-
}
90-
q.add(v);
78+
private static boolean bfsBuildLevelGraph(int[][] residual, int source, int sink, int[] level) {
79+
Arrays.fill(level, -1);
80+
level[source] = 0;
81+
Queue<Integer> q = new ArrayDeque<>();
82+
q.add(source);
83+
while (!q.isEmpty()) {
84+
int u = q.poll();
85+
for (int v = 0; v < residual.length; v++) {
86+
if (residual[u][v] > 0 && level[v] == -1) {
87+
level[v] = level[u] + 1;
88+
if (v == sink) {
89+
return true;
90+
}
91+
q.add(v);
92+
}
93+
}
9194
}
92-
}
95+
return level[sink] != -1;
9396
}
94-
return level[sink] != -1;
95-
}
9697

97-
private static int dfsBlocking(
98-
int[][] residual, int[] level, int[] next, int u, int sink, int f) {
99-
if (u == sink) {
100-
return f;
101-
}
102-
final int n = residual.length;
103-
for (int v = next[u]; v < n; v++, next[u] = v) {
98+
private static int dfsBlocking(int[][] residual, int[] level, int[] next, int u, int sink, int f) {
99+
if (u == sink) {
100+
return f;
101+
}
102+
final int n = residual.length;
103+
for (int v = next[u]; v < n; v++, next[u] = v) {
104104
if (residual[u][v] <= 0) {
105105
continue;
106106
}
@@ -114,6 +114,6 @@ private static int dfsBlocking(
114114
return pushed;
115115
}
116116
}
117-
return 0;
118-
}
117+
return 0;
118+
}
119119
}

src/test/java/com/thealgorithms/graph/DinicTest.java

Lines changed: 52 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -7,66 +7,65 @@
77
import org.junit.jupiter.api.Test;
88

99
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+
}
1817

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+
}
2625

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+
}
3433

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+
}
4140

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);
5360
}
54-
}
5561
}
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-
}
6262
}
63-
}
6463

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;
6970
}
70-
return b;
71-
}
7271
}

0 commit comments

Comments
 (0)