private void fordFulkson() {
        if (!isResidualGraph) {
            throw new IllegalStateException("Should be residual graph to cancel negative cycles");
        }

        Map<V, V> parents = new HashMap<>();
        while (breadthFirstSearch(sourceNode, sinkNode, parents)) {
            int possibleFlow = Integer.MAX_VALUE;
            for (V node = sinkNode; node != sourceNode; node = parents.get(node)) {
                final V parent = parents.get(node);
                possibleFlow = Math.min(possibleFlow, adjList.get(parent).get(node).residualFlow);
            }

            for (V node = sinkNode; node != sourceNode; node = parents.get(node)) {
                final V parent = parents.get(node);
                final Edge parentEdge = adjList.get(parent).get(node);
                final Edge counterEdge = parentEdge.counterEdge;
                parentEdge.residualFlow -= possibleFlow;
                if (parentEdge.forwardEdge) {
                    parentEdge.flow += possibleFlow;
                }
                counterEdge.residualFlow += possibleFlow;
                if (counterEdge.forwardEdge && counterEdge.flow >= possibleFlow) {
                    counterEdge.flow -= possibleFlow;
                }
            }

            parents = new HashMap<>();
        }
    }
Solve min cost flow with cycle canceling algorithm.
streams/src/main/java/org/apache/kafka/streams/processor/internals/assignment/Graph.java