L6: iterates as long as it can find an augmenting path
L7: finds the edge with the minimum capacity in the augmenting path.
L8: updates the edges in the path with the flow.
Let us define the getMin() method:
privatedoublegetMin(MaxFlow mf,List<Edge> path) {double min =mf.getResidual(path.get(0));for (int i =1; i <path.size(); i++) min =Math.min(min,mf.getResidual(path.get(i)));return min;}
Finally, let us define the getAugmentingPath() method:
L2: once the source reaches the target, it found an augmenting path.
L6: adding the source vertex would cause a cycle.
L7: cannot push the flow when there is no residual left.
L10: recursively finds the augmenting path by switching the target.
Backward Pushing
Let us consider the following graph:
As shown, our implementation of Ford-Fulkerson Algorithm does not always guarantee to find the maximum flow correctly. To fix this issue, we need to implement backward pushing:
The backward pushing can be performed after the applying the flow to all edges as in the implementation above (see the code in the "With Backward Pushing" tab).
Finally, the updateBackward() method can be implemented as follows: