Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.19 KB | None | 0 0
  1. public class FordFulkerson
  2. {
  3. private boolean[] marked; // Is s->v path in residual graph?
  4. private FlowEdge[] edgeTo; // last edge on shortest s->v path
  5. private double value; // current value of maxflow
  6. public FordFulkerson(FlowNetwork G, int s, int t)
  7. { // Find maxflow in flow network G from s to t.
  8. while (hasAugmentingPath(G, s, t))
  9. { // While there exists an augmenting path, use it.
  10. // Compute bottleneck capacity.
  11. double bottle = Double.POSITIVE_INFINITY;
  12. for (int v = t; v != s; v = edgeTo[v].other(v))
  13. bottle = Math.min(bottle, edgeTo[v].residualCapacityTo(v));
  14. // Augment flow.
  15. for (int v = t; v != s; v = edgeTo[v].other(v))
  16. edgeTo[v].addResidualFlowTo(v, bottle);
  17. value += bottle;
  18. }
  19. }
  20. public double value() { return value; }
  21. public boolean inCut(int v) { return marked[v]; }
  22. public static void main(String[] args)
  23. {
  24. FlowNetwork G = new FlowNetwork(new In(args[0]));
  25. int s = 0, t = G.V() - 1;
  26. FordFulkerson maxflow = new FordFulkerson(G, s, t);
  27. StdOut.println("Max flow from " + s + " to " + t);
  28. for (int v = 0; v < G.V(); v++)
  29. for (FlowEdge e : G.adj(v))
  30. if ((v == e.from()) && e.flow() > 0)
  31. StdOut.println(" " + e);
  32. StdOut.println("Max flow value = " + maxflow.value());
  33. }
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement