public static <NodeType extends IGraphNode<NodeType>> Set<NodeType> getLoopNodes(
      final NodeType sourceNode, final NodeType destinationNode) {
    if (sourceNode == destinationNode) {
      final ArrayList<NodeType> nodeList = new ArrayList<NodeType>();
      nodeList.add(sourceNode);
      return new HashSet<NodeType>(nodeList);
    }

    final ArrayList<NodeType> upwardsNodes = new ArrayList<NodeType>();
    upwardsNodes.add(destinationNode);

    final Set<NodeType> resolveUpwards = new HashSet<NodeType>(upwardsNodes);
    final Stack<NodeType> upwardsWorkingList = new Stack<NodeType>();
    upwardsWorkingList.push(sourceNode);

    while (!upwardsWorkingList.empty()) {
      final NodeType currentNode = upwardsWorkingList.pop();
      resolveUpwards.add(currentNode);
      for (final NodeType currentParentNode : currentNode.getParents()) {
        if (!resolveUpwards.contains(currentParentNode)) {
          resolveUpwards.add(currentParentNode);
          upwardsWorkingList.push(currentParentNode);
        }
      }
    }

    final ArrayList<NodeType> downwardsNodes = new ArrayList<NodeType>();

    final Set<NodeType> resolveDownwards = new HashSet<NodeType>(downwardsNodes);
    final Stack<NodeType> downwardsWorkingList = new Stack<NodeType>();
    downwardsWorkingList.push(destinationNode);

    while (!downwardsWorkingList.empty()) {
      final NodeType currentNode = downwardsWorkingList.pop();
      resolveDownwards.add(currentNode);
      for (final NodeType currentChildNode : currentNode.getChildren()) {
        if (!resolveDownwards.contains(currentChildNode)) {
          resolveDownwards.add(currentChildNode);
          downwardsWorkingList.push(currentChildNode);
        }
      }
    }

    resolveUpwards.retainAll(resolveDownwards);

    return resolveUpwards;
  }
Gets the nodes of a loop

@param sourceNode The source node of the loop where the back edge originates from.
@param destinationNode The destination node of the loop where the back edge points to.
@return The Set of nodes which belong to the loop.
java/zylib/src/main/java/com/google/security/zynamics/zylib/types/graphs/GraphAlgorithms.java