Exemplo n.º 1
0
 // run DFS in digraph G from vertex v and compute preorder/postorder
 private void dfs(Digraph G, int v) {
   marked[v] = true;
   pre[v] = preCounter++;
   preorder.enqueue(v);
   for (int w : G.adj(v)) {
     if (!marked[w]) {
       dfs(G, w);
     }
   }
   postorder.enqueue(v);
   post[v] = postCounter++;
 }
Exemplo n.º 2
0
  private void dfs(Digraph G, int v) {
    marked[v] = true;
    preorder[v] = pre++;
    stack1.push(v);
    stack2.push(v);
    for (int w : G.adj(v)) {
      if (!marked[w]) dfs(G, w);
      else if (id[w] == -1) {
        while (preorder[stack2.peek()] > preorder[w]) stack2.pop();
      }
    }

    // found strong component containing v
    if (stack2.peek() == v) {
      stack2.pop();
      int w;
      do {
        w = stack1.pop();
        id[w] = count;
      } while (w != v);
      count++;
    }
  }