Repository navigation
Expand file tree
/
Copy pathDinics.java
More file actions
161 lines (135 loc) · 4.21 KB
/
Copy pathDinics.java
File metadata and controls
161 lines (135 loc) · 4.21 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
/**
* Implementation of Dinic's network flow algorithm. The algorithm works by first constructing a
* level graph using a BFS and then finding augmenting paths on the level graph using multiple DFSs.
*
* <p>Run script:
*
* <p>$ bazel run //src/main/java/com/williamfiset/algorithms/graphtheory/networkflow:Dinics
*
* <p>Time Complexity: O(EV²)
*
* @author William Fiset, william.alexandre.fiset@gmail.com
*/
package com.williamfiset.algorithms.graphtheory.networkflow;
import static java.lang.Math.min;
import java.util.*;
public class Dinics extends NetworkFlowSolverBase {
private int[] level;
/**
* Creates an instance of a flow network solver. Use the {@link #addEdge} method to add edges to
* the graph.
*
* @param n - The number of nodes in the graph including source and sink nodes.
* @param s - The index of the source node, 0 <= s < n
* @param t - The index of the sink node, 0 <= t < n, t != s
*/
public Dinics(int n, int s, int t) {
super(n, s, t);
level = new int[n];
}
@Override
public void solve() {
// next[i] indicates the next unused edge index in the adjacency list for node i. This is part
// of the Shimon Even and Alon Itai optimization of pruning deads ends as part of the DFS phase.
int[] next = new int[n];
while (bfs()) {
Arrays.fill(next, 0);
// Find max flow by adding all augmenting path flows.
for (long f = dfs(s, next, INF); f != 0; f = dfs(s, next, INF)) {
maxFlow += f;
}
}
for (int i = 0; i < n; i++) if (level[i] != -1) minCut[i] = true;
}
// Do a BFS from source to sink and compute the depth/level of each node
// which is the minimum number of edges from that node to the source.
private boolean bfs() {
Arrays.fill(level, -1);
level[s] = 0;
Deque<Integer> q = new ArrayDeque<>(n);
q.offer(s);
while (!q.isEmpty()) {
int node = q.poll();
for (Edge edge : graph[node]) {
long cap = edge.remainingCapacity();
if (cap > 0 && level[edge.to] == -1) {
level[edge.to] = level[node] + 1;
q.offer(edge.to);
}
}
}
return level[t] != -1;
}
private long dfs(int at, int[] next, long flow) {
if (at == t) return flow;
final int numEdges = graph[at].size();
for (; next[at] < numEdges; next[at]++) {
Edge edge = graph[at].get(next[at]);
long cap = edge.remainingCapacity();
if (cap > 0 && level[edge.to] == level[at] + 1) {
long bottleNeck = dfs(edge.to, next, min(flow, cap));
if (bottleNeck > 0) {
edge.augment(bottleNeck);
return bottleNeck;
}
}
}
return 0;
}
/* Examples */
public static void main(String[] args) {
testSmallFlowGraph();
// testGraphFromSlides();
}
// Testing graph from:
// http://crypto.cs.mcgill.ca/~crepeau/COMP251/KeyNoteSlides/07demo-maxflowCS-C.pdf
private static void testSmallFlowGraph() {
int n = 6;
int s = n - 1;
int t = n - 2;
Dinics solver;
solver = new Dinics(n, s, t);
// Source edges
solver.addEdge(s, 0, 10);
solver.addEdge(s, 1, 10);
// Sink edges
solver.addEdge(2, t, 10);
solver.addEdge(3, t, 10);
// Middle edges
solver.addEdge(0, 1, 2);
solver.addEdge(0, 2, 4);
solver.addEdge(0, 3, 8);
solver.addEdge(1, 3, 9);
solver.addEdge(3, 2, 6);
System.out.println(solver.getMaxFlow()); // 19
}
private static void testGraphFromSlides() {
int n = 11;
int s = n - 1;
int t = n - 2;
NetworkFlowSolverBase solver;
solver = new Dinics(n, s, t);
// Source edges
solver.addEdge(s, 0, 5);
solver.addEdge(s, 1, 10);
solver.addEdge(s, 2, 15);
// Middle edges
solver.addEdge(0, 3, 10);
solver.addEdge(1, 0, 15);
solver.addEdge(1, 4, 20);
solver.addEdge(2, 5, 25);
solver.addEdge(3, 4, 25);
solver.addEdge(3, 6, 10);
solver.addEdge(3, 7, 20);
solver.addEdge(4, 2, 5);
solver.addEdge(4, 7, 30);
solver.addEdge(5, 7, 20);
solver.addEdge(5, 8, 10);
solver.addEdge(7, 8, 15);
// Sink edges
solver.addEdge(6, t, 5);
solver.addEdge(7, t, 15);
solver.addEdge(8, t, 10);
System.out.printf("Maximum flow %d\n", solver.getMaxFlow()); // 30
}
}