private void cancelNegativeCycle(final V nodeInCycle, final Map<V, V> parentNodes, final Map<V, Edge> parentEdges) {
        // Start from parentNode since nodeInCyle is used as exit condition in below loops
        final V parentNode = parentNodes.get(nodeInCycle);
        Edge parentEdge = parentEdges.get(nodeInCycle);

        // Find max possible negative flow
        int possibleFlow = parentEdge.residualFlow;
        for (V curNode = parentNode; curNode != nodeInCycle; curNode = parentNodes.get(curNode)) {
            parentEdge = parentEdges.get(curNode);
            possibleFlow = Math.min(possibleFlow, parentEdge.residualFlow);
        }

        // Update graph by removing negative flow
        parentEdge = parentEdges.get(nodeInCycle);
        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;
        }
        for (V curNode = parentNode; curNode != nodeInCycle; curNode = parentNodes.get(curNode)) {
            parentEdge = parentEdges.get(curNode);
            counterEdge = parentEdge.counterEdge;
            parentEdge.residualFlow -= possibleFlow;
            if (parentEdge.forwardEdge) {
                parentEdge.flow += possibleFlow;
            }
            counterEdge.residualFlow += possibleFlow;
            if (counterEdge.forwardEdge && counterEdge.flow >= possibleFlow) {
                counterEdge.flow -= possibleFlow;
            }
        }
    }
Solve min cost flow with cycle canceling algorithm.
streams/src/main/java/org/apache/kafka/streams/processor/internals/assignment/Graph.java