Esempio n. 1
0
 public KosarajuSCC(Digraph G) {
   marked = new boolean[G.V()];
   id = new int[G.V()];
   DepthFirstOrder order = new DepthFirstOrder(G.reverse());
   for (int s : order.reverse())
     if (!marked[s]) {
       dfs(G, s);
       count++;
     }
 }
  private KosarajuSharirSCC(Digraph<V> digraph) {
    ids = new HashMap<>(digraph.vertexCount());
    visited = new HashSet<>(digraph.vertexCount());
    size = new int[digraph.vertexCount()];

    DepthFirstOrder<V> dfo = DepthFirstOrder.from(digraph.reverse());

    for (V vertex : dfo.reversePost()) {
      if (!visited.contains(vertex)) {
        dfs(digraph, vertex);
        count++;
      }
    }
  }
  public KosarajuSCC(Digraph G) {

    // compute reverse postorder of reverse graph
    DepthFirstOrder dfs = new DepthFirstOrder(G.reverse());

    // run DFS on G, using reverse postorder to guide calculation
    marked = new boolean[G.V()];
    id = new int[G.V()];
    for (int v : dfs.reversePost()) {
      if (!marked[v]) {
        dfs(G, v);
        count++;
      }
    }

    // check that id[] gives strong components
    assert check(G);
  }
Esempio n. 4
0
  public static void main(String[] args) {

    In in1 = new In(Inputs.DIGRAPH_SMALL);
    In in2 = new In(Inputs.DIGRAPH_SMALL);
    edu.princeton.cs.algs4.Digraph sedgewicks = new edu.princeton.cs.algs4.Digraph(in1);
    Digraph digraph = new Digraph(in2);
    in1.close();
    in2.close();

    StdOut.printf("Sedgewicks - v: %d, e: %d\n", sedgewicks.V(), sedgewicks.E());
    for (int i = 0; i < sedgewicks.V(); i++) {
      for (int w : sedgewicks.adj(i)) {
        StdOut.printf("%d -> %d\n", i, w);
      }
    }
    StdOut.println();

    StdOut.printf("My - v: %d, e: %d\n", digraph.V(), digraph.E());
    for (int i = 0; i < digraph.V(); i++) {
      for (int w : digraph.adj(i)) {
        StdOut.printf("%d -> %d\n", i, w);
      }
    }
    StdOut.println();

    edu.princeton.cs.algs4.Digraph sedwickDi = sedgewicks.reverse();
    StdOut.printf("Sedgewicks reverse - v: %d, e: %d\n", sedwickDi.V(), sedwickDi.E());
    for (int i = 0; i < sedwickDi.V(); i++) {
      for (int w : sedwickDi.adj(i)) {
        StdOut.printf("%d -> %d\n", i, w);
      }
    }
    StdOut.println();

    Digraph myReverse = digraph.reverse();
    StdOut.printf("My reverse - v: %d, e: %d\n", myReverse.V(), myReverse.E());
    for (int i = 0; i < myReverse.V(); i++) {
      for (int w : myReverse.adj(i)) {
        StdOut.printf("%d -> %d\n", i, w);
      }
    }
  }