private boolean breadthFirstSearch(final V source, final V target, final Map<V, V> parents) {
        final Set<V> visited = new HashSet<>();
        final Queue<V> queue = new LinkedList<>();
        queue.add(source);
        visited.add(source);
        while (!queue.isEmpty()) {
            final V node = queue.poll();
            final SortedMap<V, Edge> nodeEdges = adjList.get(node);
            for (final Entry<V, Edge> nodeEdge : nodeEdges.entrySet()) {
                final V nextNode = nodeEdge.getKey();
                if (visited.contains(nextNode) || nodeEdge.getValue().residualFlow <= 0) {
                    continue;
                }
                if (nodeEdge.getKey().equals(target)) {
                    parents.put(target, node);
                    return true;
                }
                queue.add(nodeEdge.getKey());
                parents.put(nodeEdge.getKey(), node);
                visited.add(nodeEdge.getKey());
            }
        }

        return false;
    }
Solve min cost flow with cycle canceling algorithm.
streams/src/main/java/org/apache/kafka/streams/processor/internals/assignment/Graph.java