001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
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
|
package algs64; // section 6.4
import stdlib.*;
import algs13.Queue;
/* ***********************************************************************
* Compilation: javac FordFulkerson.java
* Execution: java FordFulkerson V E
* Dependencies: FlowNetwork.java FlowEdge.java Queue.java
*
* Ford-Fulkerson algorithm for computing a max flow and
* a min cut using shortest augmenthing path rule.
*
*********************************************************************/
public class FordFulkerson {
private boolean[] marked; // marked[v] = true iff s->v path in residual graph
private FlowEdge[] edgeTo; // edgeTo[v] = last edge on shortest residual s->v path
private double value; // current value of max flow
// max flow in flow network G from s to t
public FordFulkerson(FlowNetwork G, int s, int t) {
if (s < 0 || s >= G.V()) {
throw new IndexOutOfBoundsException("Source s is invalid: " + s);
}
if (t < 0 || t >= G.V()) {
throw new IndexOutOfBoundsException("Sink t is invalid: " + t);
}
if (s == t) {
throw new IllegalArgumentException("Source equals sink");
}
value = excess(G, t);
if (!isFeasible(G, s, t)) {
throw new Error("Initial flow is infeasible");
}
// while there exists an augmenting path, use it
while (hasAugmentingPath(G, s, t)) {
// compute bottleneck capacity
double bottle = Double.POSITIVE_INFINITY;
for (int v = t; v != s; v = edgeTo[v].other(v)) {
bottle = Math.min(bottle, edgeTo[v].residualCapacityTo(v));
}
// augment flow
for (int v = t; v != s; v = edgeTo[v].other(v)) {
edgeTo[v].addResidualFlowTo(v, bottle);
}
value += bottle;
}
// check optimality conditions
assert check(G, s, t);
}
// return value of max flow
public double value() {
return value;
}
// is v in the s side of the min s-t cut?
public boolean inCut(int v) {
return marked[v];
}
// return an augmenting path if one exists, otherwise return null
private boolean hasAugmentingPath(FlowNetwork G, int s, int t) {
edgeTo = new FlowEdge[G.V()];
marked = new boolean[G.V()];
// breadth-first search
Queue<Integer> q = new Queue<>();
q.enqueue(s);
marked[s] = true;
while (!q.isEmpty()) {
int v = q.dequeue();
for (FlowEdge e : G.adj(v)) {
int w = e.other(v);
// if residual capacity from v to w
if (e.residualCapacityTo(w) > 0) {
if (!marked[w]) {
edgeTo[w] = e;
marked[w] = true;
q.enqueue(w);
}
}
}
}
// is there an augmenting path?
return marked[t];
}
// return excess flow at vertex v
private double excess(FlowNetwork G, int v) {
double excess = 0.0;
for (FlowEdge e : G.adj(v)) {
if (v == e.from()) excess -= e.flow();
else excess += e.flow();
}
return excess;
}
// return excess flow at vertex v
private boolean isFeasible(FlowNetwork G, int s, int t) {
double EPSILON = 1E-11;
// check that capacity constraints are satisfied
for (int v = 0; v < G.V(); v++) {
for (FlowEdge e : G.adj(v)) {
if (e.flow() < -EPSILON || e.flow() > e.capacity() + EPSILON) {
System.err.println("Edge does not satisfy capacity constraints: " + e);
return false;
}
}
}
// check that net flow into a vertex equals zero, except at source and sink
if (Math.abs(value + excess(G, s)) > EPSILON) {
System.err.println("Excess at source = " + excess(G, s));
System.err.println("Max flow = " + value);
return false;
}
if (Math.abs(value - excess(G, t)) > EPSILON) {
System.err.println("Excess at sink = " + excess(G, t));
System.err.println("Max flow = " + value);
return false;
}
for (int v = 0; v < G.V(); v++) {
if (v == s || v == t) continue;
else if (Math.abs(excess(G, v)) > EPSILON) {
System.err.println("Net flow out of " + v + " doesn't equal zero");
return false;
}
}
return true;
}
// check optimality conditions
private boolean check(FlowNetwork G, int s, int t) {
// check that flow is feasible
if (!isFeasible(G, s, t)) {
System.err.println("Flow is infeasible");
return false;
}
// check that s is on the source side of min cut and that t is not on source side
if (!inCut(s)) {
System.err.println("source " + s + " is not on source side of min cut");
return false;
}
if (inCut(t)) {
System.err.println("sink " + t + " is on source side of min cut");
return false;
}
// check that value of min cut = value of max flow
double mincutValue = 0.0;
for (int v = 0; v < G.V(); v++) {
for (FlowEdge e : G.adj(v)) {
if ((v == e.from()) && inCut(e.from()) && !inCut(e.to()))
mincutValue += e.capacity();
}
}
double EPSILON = 1E-11;
if (Math.abs(mincutValue - value) > EPSILON) {
System.err.println("Max flow value = " + value + ", min cut value = " + mincutValue);
return false;
}
return true;
}
// test client that creates random network, solves max flow, and prints results
public static void main(String[] args) {
// create flow network with V vertices and E edges
int V = Integer.parseInt(args[0]);
int E = Integer.parseInt(args[1]);
int s = 0, t = V-1;
FlowNetwork G = new FlowNetwork(V, E);
StdOut.println(G);
// compute maximum flow and minimum cut
FordFulkerson maxflow = new FordFulkerson(G, s, t);
StdOut.println("Max flow from " + s + " to " + t);
for (int v = 0; v < G.V(); v++) {
for (FlowEdge e : G.adj(v)) {
if ((v == e.from()) && e.flow() > 0)
StdOut.println(" " + e);
}
}
// print min-cut
StdOut.print("Min cut: ");
for (int v = 0; v < G.V(); v++) {
if (maxflow.inCut(v)) StdOut.print(v + " ");
}
StdOut.println();
StdOut.println("Max flow value = " + maxflow.value());
}
}
|