コード例 #1
0
  private void strongConnect(SCCNode v, List<List<SCCNode>> sccs) {
    // Set the depth index for v to the smallest unused index
    v.index = index;
    v.lowlink = index++;
    stack.push(v);
    stackSet.add(v);

    // Consider successors of v
    for (Node<SCCNode> n : v.getLinks()) {
      SCCNode w = (SCCNode) n;
      if (w.index == -1) {
        // Successor w has not yet been visited; recurse on it
        strongConnect(w, sccs);
        v.lowlink = Math.min(v.lowlink, w.lowlink);
      } else if (stackSet.contains(w)) {
        // Successor w is in stack S and hence in the current SCC
        v.lowlink = Math.min(v.lowlink, w.index);
      }
    }

    // If v is a root node, pop the stack and generate an SCC
    if (v.lowlink == v.index) {
      List<SCCNode> SCC = new LinkedList<>();
      SCCNode node = null;
      do {
        node = stack.pop();
        stackSet.remove(node);
        SCC.add(node);
      } while (node != v);

      if (SCC.size() > 1) sccs.add(SCC);
    }
  }