public void solveMinCostFlow() {
        validateMinCostGraph();
        final Graph<V> residualGraph = residualGraph();
        addDummySourceNode(residualGraph);
        residualGraph.cancelNegativeCycles();
        removeDummySourceNode(residualGraph);

        for (final Entry<V, SortedMap<V, Edge>> nodeEdges : adjList.entrySet()) {
            final V node = nodeEdges.getKey();
            for (final Entry<V, Edge> nodeEdge : nodeEdges.getValue().entrySet()) {
                final V destination = nodeEdge.getKey();
                final Edge edge = nodeEdge.getValue();
                final Edge residualEdge = residualGraph.adjList.get(node).get(destination);
                edge.flow = residualEdge.flow;
                edge.residualFlow = residualEdge.residualFlow;
            }
        }
    }
Solve min cost flow with cycle canceling algorithm.
streams/src/main/java/org/apache/kafka/streams/processor/internals/assignment/Graph.java