/****************************************************************************** * Compilation: javac FordFulkerson.java * Execution: java FordFulkerson V E * Dependencies: FlowNetwork.java FlowEdge.java Queue.java * Data files: https://algs4.cs.princeton.edu/65maxflow/tinyFN.txt * * Ford-Fulkerson algorithm for computing a max flow and * a min cut using shortest augmenting path rule. * ******************************************************************************/ /** * The {@code FordFulkerson} class represents a data type for computing a * maximum st-flow and minimum st-cut in a flow * network. *
* This implementation uses the Ford-Fulkerson algorithm with * the shortest augmenting path heuristic. * The constructor takes time proportional to E V (E + V) * in the worst case and extra space (not including the network) * proportional to V, where V is the number of vertices * and E is the number of edges. In practice, the algorithm will * run much faster. * Afterwards, the {@code inCut()} and {@code value()} methods take * constant time. *
* If the capacities and initial flow values are all integers, then this * implementation guarantees to compute an integer-valued maximum flow. * If the capacities and floating-point numbers, then floating-point * roundoff error can accumulate. *
* For additional documentation,
* see Section 6.4 of
* Algorithms, 4th Edition by Robert Sedgewick and Kevin Wayne.
*
* @author Robert Sedgewick
* @author Kevin Wayne
*/
public class FordFulkerson {
private static final double FLOATING_POINT_EPSILON = 1E-11;
private final int V; // number of vertices
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
/**
* Compute a maximum flow and minimum cut in the network {@code G}
* from vertex {@code s} to vertex {@code t}.
*
* @param G the flow network
* @param s the source vertex
* @param t the sink vertex
* @throws IllegalArgumentException unless {@code 0 <= s < V}
* @throws IllegalArgumentException unless {@code 0 <= t < V}
* @throws IllegalArgumentException if {@code s == t}
* @throws IllegalArgumentException if initial flow is infeasible
*/
public FordFulkerson(FlowNetwork G, int s, int t) {
V = G.V();
validate(s);
validate(t);
if (s == t) throw new IllegalArgumentException("Source equals sink");
if (!isFeasible(G, s, t)) throw new IllegalArgumentException("Initial flow is infeasible");
// while there exists an augmenting path, use it
value = excess(G, t);
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);
}
/**
* Returns the value of the maximum flow.
*
* @return the value of the maximum flow
*/
public double value() {
return value;
}
/**
* Returns true if the specified vertex is on the {@code s} side of the mincut.
*
* @param v vertex
* @return {@code true} if vertex {@code v} is on the {@code s} side of the micut;
* {@code false} otherwise
* @throws IllegalArgumentException unless {@code 0 <= v < V}
*/
public boolean inCut(int v) {
validate(v);
return marked[v];
}
// throw an IllegalArgumentException if v is outside prescibed range
private void validate(int v) {
if (v < 0 || v >= V)
throw new IllegalArgumentException("vertex " + v + " is not between 0 and " + (V-1));
}
// is there an augmenting path?
// if so, upon termination edgeTo[] will contain a parent-link representation of such a path
// this implementation finds a shortest augmenting path (fewest number of edges),
// which performs well both in theory and in practice
private boolean hasAugmentingPath(FlowNetwork G, int s, int t) {
edgeTo = new FlowEdge[G.V()];
marked = new boolean[G.V()];
// breadth-first search
Queue