V detectNegativeCycles(final V source, final Map<V, V> parentNodes, final Map<V, Edge> parentEdges) {
        // Use long to account for any overflow
        final Map<V, Long> distance = new HashMap<>();
        distance.put(source, 0L);
        final int nodeCount = nodes.size();

        // Iterate nodeCount iterations since Bellaman-Ford will find the shortest path in nodeCount - 1
        // iterations. If the distance can still be relaxed in nodeCount iteration, there's a negative
        // cycle
        for (int i = 0; i < nodeCount; i++) {
            // Iterate through all edges
            for (final Entry<V, SortedMap<V, Edge>> nodeEdges : adjList.entrySet()) {
                final V start = nodeEdges.getKey();
                for (final Entry<V, Edge> nodeEdge : nodeEdges.getValue().entrySet()) {
                    final Edge edge = nodeEdge.getValue();
                    if (edge.residualFlow == 0) {
                        continue;
                    }
                    final V end = edge.destination;
                    final Long distanceStart = distance.get(start);
                    final Long distanceEnd = distance.get(end);
                    // There's a path to u and either we haven't computed V or distance to V is shorter
                    if (distanceStart != null && (distanceEnd == null || distanceEnd > distanceStart + edge.cost)) {
                        distance.put(end, distanceStart + edge.cost);
                        parentNodes.put(end, start);
                        parentEdges.put(end, edge);
                        if (i == nodeCount - 1) {
                            return end;
                        }
                    }
                }
            }
        }
        return null;
    }
Detect negative cycle using Bellman-ford's shortest path algorithm.
@param source Source node
@param parentNodes Parent nodes to store negative cycle nodes
@param parentEdges Parent edges to store negative cycle edges

@return One node which can lead to negative cycle if exists or null if there's no negative cycle
streams/src/main/java/org/apache/kafka/streams/processor/internals/assignment/Graph.java