Esempio n. 1
0
  /** Unit tests the <tt>GabowSCC</tt> data type. */
  public static void main(String[] args) {
    In in = new In(args[0]);
    Digraph G = new Digraph(in);
    GabowSCC scc = new GabowSCC(G);

    // number of connected components
    int M = scc.count();
    StdOut.println(M + " components");

    // compute list of vertices in each strong component
    Queue<Integer>[] components = (Queue<Integer>[]) new Queue[M];
    for (int i = 0; i < M; i++) {
      components[i] = new Queue<Integer>();
    }
    for (int v = 0; v < G.V(); v++) {
      components[scc.id(v)].enqueue(v);
    }

    // print results
    for (int i = 0; i < M; i++) {
      for (int v : components[i]) {
        StdOut.print(v + " ");
      }
      StdOut.println();
    }
  }
Esempio n. 2
0
  /** Unit tests the <tt>DepthFirstOrder</tt> data type. */
  public static void main(String[] args) {
    In in = new In(args[0]);
    Digraph G = new Digraph(in);

    DepthFirstOrder dfs = new DepthFirstOrder(G);
    StdOut.println("   v  pre post");
    StdOut.println("--------------");
    for (int v = 0; v < G.V(); v++) {
      StdOut.printf("%4d %4d %4d\n", v, dfs.pre(v), dfs.post(v));
    }

    StdOut.print("Preorder:  ");
    for (int v : dfs.pre()) {
      StdOut.print(v + " ");
    }
    StdOut.println();

    StdOut.print("Postorder: ");
    for (int v : dfs.post()) {
      StdOut.print(v + " ");
    }
    StdOut.println();

    StdOut.print("Reverse postorder: ");
    for (int v : dfs.reversePost()) {
      StdOut.print(v + " ");
    }
    StdOut.println();
  }
Esempio n. 3
0
 /**
  * Determines a depth-first order for the digraph <tt>G</tt>.
  *
  * @param G the digraph
  */
 public DepthFirstOrder(Digraph G) {
   pre = new int[G.V()];
   post = new int[G.V()];
   postorder = new Queue<Integer>();
   preorder = new Queue<Integer>();
   marked = new boolean[G.V()];
   for (int v = 0; v < G.V(); v++) if (!marked[v]) dfs(G, v);
 }
Esempio n. 4
0
 // does the id[] array contain the strongly connected components?
 private boolean check(Digraph G) {
   TransitiveClosure tc = new TransitiveClosure(G);
   for (int v = 0; v < G.V(); v++) {
     for (int w = 0; w < G.V(); w++) {
       if (stronglyConnected(v, w) != (tc.reachable(v, w) && tc.reachable(w, v))) return false;
     }
   }
   return true;
 }
Esempio n. 5
0
  /**
   * Computes the strong components of the digraph <tt>G</tt>.
   *
   * @param G the digraph
   */
  public GabowSCC(Digraph G) {
    marked = new boolean[G.V()];
    stack1 = new Stack<Integer>();
    stack2 = new Stack<Integer>();
    id = new int[G.V()];
    preorder = new int[G.V()];
    for (int v = 0; v < G.V(); v++) id[v] = -1;

    for (int v = 0; v < G.V(); v++) {
      if (!marked[v]) dfs(G, v);
    }

    // check that id[] gives strong components
    assert check(G);
  }
Esempio n. 6
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++;
 }
Esempio n. 7
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++;
    }
  }