Ejemplo n.º 1
0
  public static void main(String[] args) {
    In in = new In("jobsPC1.txt");
    int N = in.readInt();
    in.readLine();
    EdgeWeightedDigraph G;
    G = new EdgeWeightedDigraph(2 * N + 2);
    int s = 2 * N, t = 2 * N + 1;
    for (int i = 0; i < N; ++i) {
      String[] a = in.readLine().split("\\s+");
      double duration = Double.parseDouble(a[0]);
      G.addEdge(new DirectedEdge(i, i + N, duration));
      G.addEdge(new DirectedEdge(s, i, 0.0));
      G.addEdge(new DirectedEdge(i + N, t, 0.0));
      for (int j = 1; j < a.length; ++j) {
        G.addEdge(new DirectedEdge(i + N, Integer.parseInt(a[j]), 0));
      }
    }
    StdOut.println(G);
    MyAcyclicLP lp = new MyAcyclicLP(G, s);

    StdOut.println("Start times: ");
    for (int i = 0; i < N; ++i) StdOut.printf("%4d: %5.1f\n", i, lp.distTo(i));
    StdOut.printf("%5.1f\n", lp.distTo(t));

    for (DirectedEdge e : lp.pathTo(t)) {
      StdOut.print(e + " ");
    }
    StdOut.println();
  }
Ejemplo n.º 2
0
  public static void main(String[] args) {

    // create random DAG with V vertices and E edges; then add F random edges
    int V = Integer.parseInt(args[0]);
    int E = Integer.parseInt(args[1]);
    int F = Integer.parseInt(args[2]);
    EdgeWeightedDigraph G = new EdgeWeightedDigraph(V);
    int[] vertices = new int[V];
    for (int i = 0; i < V; i++) vertices[i] = i;
    StdRandom.shuffle(vertices);
    for (int i = 0; i < E; i++) {
      int v, w;
      do {
        v = StdRandom.uniform(V);
        w = StdRandom.uniform(V);
      } while (v >= w);
      double weight = Math.random();
      G.addEdge(new DirectedEdge(v, w, weight));
    }

    // add F extra edges
    for (int i = 0; i < F; i++) {
      int v = (int) (Math.random() * V);
      int w = (int) (Math.random() * V);
      double weight = Math.random();
      G.addEdge(new DirectedEdge(v, w, weight));
    }

    StdOut.println(G);

    // find a directed cycle
    EdgeWeightedDirectedCycle finder = new EdgeWeightedDirectedCycle(G);
    if (finder.hasCycle()) {
      StdOut.print("Cycle: ");
      for (DirectedEdge e : finder.cycle()) {
        StdOut.print(e + " ");
      }
      StdOut.println();
    }

    // or give topologial sort
    else {
      StdOut.println("No directed cycle");
    }
  }
Ejemplo n.º 3
0
	private boolean findNegativeCycle(){
		int V =  edgeTo.length;
		EdgeWeightedDigraph spt;
		spt = new EdgeWeightedDigraph(V);
		for (int v = 0 ; v < V ; v++ ) {
			if(edgeTo[v] !=null) 
				spt.addEdge(edgeTo[v]);
		}
		EdgeWeightedCycleFinder cf ; 
		cf = new EdgeWeightedCycleFinder(spt);
		cycle = cf.cycle();
	}
Ejemplo n.º 4
0
	private void findNegativeCycle(){
		int V = edgeTo.length;
		EdgeWeightedDigraph spt ; 
		spt = new EdgeWeightedDigraph;
		for (int v = 0 ; v < V ; v++ ) {
			if(edgeTo[v] !=null ){
				spt.addEdge(edgeTo[v]);
			}
		}
		EdgeWeightedCycleFinder cf;
		cf = new EdgeWeightedCycleFinder(spt);
		cycle = cf.cycle();//cycle()方法来自4.2界
	}
Ejemplo n.º 5
0
	public static void main(String[] args){
		int N = StdIn.readInt();StdIn.readLine();
		EdgeWeightedDigraph G ;
		G = new EdgeWeightedDigraph(2*N+2);
		int s = 2*N ,t = 2*N+1;
		for(int i = 0 ; i < N ; i++){
			String[] a = StdIn.readLine().split("\\s+");
			double duration = Double.parseDouble(a[0]);
			G.addEdge(new DirectedEdge(i,i+N,duration));
			G.addEdge(new DirectedEdge(s,i,0.0));
			G.addEdge(new DirectedEdge(i+N,t,0.0));
			for(int j  = 1 ;  j  < a.length ; j++){
				int sucessor = Integer.parseInt(a[j]);
				G.addEdge(new DirectedEdge(i+N,sucessor,0.0));
			}
		}
		AcyclicSP lp = new AcyclicSP(G,s);
		StdOut.println("START TIME:");
		for(int i  = 0 ; i  < N ; i++){
			StdOut.printf("%4d:%5.1f\n",i,lp.distTo(i));
			StdOut.printf("finish time :%5.1f\n",lp.distTo(t));
		}
	}
Ejemplo n.º 6
0
  /**
   * 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));
  }