boolean (int startIndex, int vertexIndex, Graph<Node> scg) { /* * Find cycles in a strongly connected graph per Johnson. */ boolean foundCycle = false; Node vertex = toV(vertexIndex); myStack.push(vertex); myBlocked.add(vertex); Set<Node> successors = new HashSet<>(); scg.getOut(vertex).forEachRemaining(successors::add); for (Node successor : successors) { int successorIndex = toI(successor); if (successorIndex == startIndex) { List<Node> cycle = new ArrayList<>(myStack.size()); myStack.descendingIterator().forEachRemaining(cycle::add); myCycleConsumer.accept(cycle); foundCycle = true; } else if (!myBlocked.contains(successor)) { boolean gotCycle = findCyclesInSCG(startIndex, successorIndex, scg); foundCycle = foundCycle || gotCycle; } } if (foundCycle) { unblock(vertex); } else { for (Node w : successors) { Set<Node> bSet = getBSet(w); bSet.add(vertex); } } myStack.pop(); return foundCycle; }