public static <T extends IGraphNode<T>> ArrayList<Set<T>> getGraphLoops(
      final IDirectedGraph<T, ?> graph) throws MalformedGraphException {
    T rootNode = null;
    final ArrayList<Set<T>> resultList = new ArrayList<Set<T>>();

    for (final T currentNode : graph.getNodes()) {
      if (currentNode.getParents().size() == 0) {
        rootNode = currentNode;
        break;
      }
    }

    if (rootNode == null) {
      return null;
    }

    final HashMap<T, ArrayList<T>> nodeToBackEdges = getBackEdges(graph, rootNode);

    for (final T graphNode : graph.getNodes()) {
      final ArrayList<T> nodesBackEdges = nodeToBackEdges.get(graphNode);
      for (final T backEdgeNode : nodesBackEdges) {
        resultList.add(getLoopNodes(graphNode, backEdgeNode));
      }
    }
    return resultList;
  }
Calculates the loops contained in a graph.

@param graph the input graph
@return A List of Sets where each set contains the nodes of one loop.
@throws MalformedGraphException Thrown if the graph has more than one entry node.
java/zylib/src/main/java/com/google/security/zynamics/zylib/types/graphs/GraphAlgorithms.java