-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaxFlowEK.java
More file actions
209 lines (190 loc) · 5.62 KB
/
MaxFlowEK.java
File metadata and controls
209 lines (190 loc) · 5.62 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
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;
public class MaxFlowEK {
static class AdjacencyList {
ArrayList<Vertex> vertexList;
AdjacencyList(int size) {
vertexList = new ArrayList<Vertex>(size);
for (int i = 0; i < size; i++) {
vertexList.add(new Vertex());
}
}
Vertex getVertex(int i) {
return vertexList.get(i);
}
void setEdge(int from, int to, long capacity) {
vertexList.get(from).outList.add(new EdgeLong(from, to, capacity));
vertexList.get(to).outList.add(new EdgeLong(to, from, 0));
}
}
static class Vertex {
ArrayList<EdgeLong> outList = new ArrayList<EdgeLong>();
}
static class Edge {
int from;
int to;
int capacity;
Edge(int f, int t, int c) {
from = f;
to = t;
capacity = c;
}
}
static class EdgeLong {
int from;
int to;
long capacity;
EdgeLong(int f, int t, long c) {
from = f;
to = t;
capacity = c;
}
}
/**
* This destroys the original graph
*
* @param residue
* @param source
* @param sink
* @param V
* @return
*/
public static long maxFlowEKLong(AdjacencyList residue, int source, int sink, final int V) {
long maxFlow = 0;
// build original
EdgeLong[] parent = new EdgeLong[V];
while (maxFlowEKBFSLong(residue, source, sink, parent, V)) {
long flow = Long.MAX_VALUE;
int current = sink;
for (; current != source; current = parent[current].from) {
EdgeLong e = parent[current];
flow = Math.min(flow, e.capacity);
}
for (current = sink; current != source; current = parent[current].from) {
EdgeLong e = parent[current];
e.capacity -= flow;
for (EdgeLong back : residue.getVertex(e.to).outList) {
if (back.to == e.from) {
back.capacity += flow;
break;
}
}
}
maxFlow += flow;
}
return maxFlow;
}
private static boolean maxFlowEKBFSLong(AdjacencyList graph, int source, int sink, EdgeLong[] parent, final int V) {
boolean[] seen = new boolean[V];
Queue<Integer> queue = new LinkedList<Integer>();
queue.offer(source);
seen[source] = true;
parent[source] = null;
while (!queue.isEmpty()) {
int current = queue.poll();
if (current == sink)
return true;
for (EdgeLong e : graph.getVertex(current).outList) {
if (e.capacity > 0 && !seen[e.to]) {
queue.offer(e.to);
parent[e.to] = e;
seen[e.to] = true;
}
}
}
return seen[sink];
}
public static long maxFlowEKLong(long[][] graph, int source, int sink, final int V) {
long maxFlow = 0;
long[][] residue = new long[V][V];
// build original
for (int u = 0; u < V; u++) {
for (int v = 0; v < V; v++) {
residue[u][v] = graph[u][v];
}
}
int[] parent = new int[V];
while (maxFlowEKBFSLong(residue, source, sink, parent, V)) {
long flow = Long.MAX_VALUE;
int current = sink;
for (; current != source; current = parent[current]) {
flow = Math.min(flow, residue[parent[current]][current]);
}
for (current = sink; current != source; current = parent[current]) {
residue[parent[current]][current] -= flow;
residue[current][parent[current]] += flow;
}
maxFlow += flow;
}
return maxFlow;
}
private static boolean maxFlowEKBFSLong(long[][] graph, int source, int sink, int[] parent, final int V) {
boolean[] seen = new boolean[V];
Queue<Integer> queue = new LinkedList<Integer>();
queue.offer(source);
seen[source] = true;
parent[source] = -1;
while (!queue.isEmpty()) {
int current = queue.poll();
// if (current == sink)
// return true;
for (int next = 0; next < V; next++) {
if (graph[current][next] > 0 && !seen[next]) {
queue.offer(next);
parent[next] = current;
seen[next] = true;
}
}
}
return seen[sink];
}
public static int maxFlowEK(int[][] graph, int source, int sink, final int V) {
int maxFlow = 0;
int[][] residue = new int[V][V];
// build original
for (int u = 0; u < V; u++) {
for (int v = 0; v < V; v++) {
residue[u][v] = graph[u][v];
}
}
int[] parent = new int[V];
while (maxFlowEKBFS(residue, source, sink, parent, V)) {
int flow = Integer.MAX_VALUE;
int current = sink;
for (; current != source; current = parent[current]) {
flow = Math.min(flow, residue[parent[current]][current]);
}
for (current = sink; current != source; current = parent[current]) {
residue[parent[current]][current] -= flow;
residue[current][parent[current]] += flow;
}
maxFlow += flow;
}
return maxFlow;
}
private static boolean maxFlowEKBFS(int[][] graph, int source, int sink, int[] parent, final int V) {
boolean[] seen = new boolean[V];
Queue<Integer> queue = new LinkedList<Integer>();
queue.offer(source);
seen[source] = true;
parent[source] = -1;
while (!queue.isEmpty()) {
int current = queue.poll();
// if (current == sink)
// return true;
for (int next = 0; next < V; next++) {
if (graph[current][next] > 0 && !seen[next]) {
queue.offer(next);
parent[next] = current;
seen[next] = true;
}
}
}
return seen[sink];
}
public static void main(String[] args) {
int[][] graph = { { 0, 20, 10, 0 }, { 0, 0, 30, 10 }, { 0, 0, 0, 20 }, { 0, 0, 0, 0 } };
System.out.println(maxFlowEK(graph, 0, 3, 4));
}
}