Exemplo n.º 1
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();
  }
  /**
   * Reads the precedence constraints from standard input and prints a feasible schedule to standard
   * output.
   */
  public static void main(String[] args) {

    // number of jobs
    int N = StdIn.readInt();

    // source and sink
    int source = 2 * N;
    int sink = 2 * N + 1;

    // build network
    EdgeWeightedDigraph G = new EdgeWeightedDigraph(2 * N + 2);
    for (int i = 0; i < N; i++) {
      double duration = StdIn.readDouble();
      G.addEdge(new DirectedEdge(source, i, 0.0));
      G.addEdge(new DirectedEdge(i + N, sink, 0.0));
      G.addEdge(new DirectedEdge(i, i + N, duration));

      // precedence constraints
      int M = StdIn.readInt();
      for (int j = 0; j < M; j++) {
        int precedent = StdIn.readInt();
        G.addEdge(new DirectedEdge(N + i, precedent, 0.0));
      }
    }

    // compute longest path
    AcyclicLP lp = new AcyclicLP(G, source);

    // print results
    StdOut.println(" job   start  finish");
    StdOut.println("--------------------");
    for (int i = 0; i < N; i++) {
      StdOut.printf("%4d %7.1f %7.1f\n", i, lp.distTo(i), lp.distTo(i + N));
    }
    StdOut.printf("Finish time: %7.1f\n", lp.distTo(sink));
  }