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(); }
public DepthFirstOrder(EdgeWeightedDigraph 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); }
public EdgeWeightedDirectedCycle(EdgeWeightedDigraph G) { marked = new boolean[G.V()]; onStack = new boolean[G.V()]; edgeTo = new DirectedEdge[G.V()]; for (int v = 0; v < G.V(); v++) if (!marked[v]) dfs(G, v); // check that digraph has a cycle assert check(G); }
/** * Método recursivo usado para la obtención de todos los caminos posibles dentro de un árbol. Si * un nodo tiene un outdegree igual a 0, es una hoja, y el camino ha terminado. De lo contrario, * debe continuar buscando caminos entre los nodos adyacentes al primer nodo mencionado. * * @param G Grafo dirigido y con pesos sobre el cual se están buscando caminos. * @param node Nodo que se está visitando actualmente. * @param weight Peso actual del camino. * @param path Representaoión como String del camino que se ha seguido hasta el nodo actual. */ private void getPaths(EdgeWeightedDigraph G, Integer node, double weight, String path) { if (G.outdegree(node) == 0) this.paths.put(weight, path); else for (DirectedEdge edge : G.adj(node)) this.getPaths( G, edge.to(), weight + edge.weight(), path + "| " + edge.from() + " -> " + edge.to() + " | "); }
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(); }
public DijkstraSP(EdgeWeightedDigraph G , int s){ edgeTo =new DirectedEdge[G.V()]; distTo =new double[G.V()]; pq = new IndexMinPQ<Double>(G.V()); for(int v = 0 ; v < G.V() ; v++) distTo[v] = Double.POSITIVE_INFINITY; distTo[s]=0; pq.insert(s,0.0); while(!pq.isEmpty()){ relax(G,pq.delMin()); } }
public AcyclicLP(EdgeWeightedDigraph G, int s) { distTo = new double[G.V()]; edgeTo = new DirectedEdge[G.V()]; for (int v = 0; v < G.V(); v++) distTo[v] = Double.NEGATIVE_INFINITY; distTo[s] = 0.0; // relax vertices in toplogical order Topological topological = new Topological(G); for (int v : topological.order()) { for (DirectedEdge e : G.adj(v)) relax(e); } }
public SingleSourceShortestPaths(EdgeWeightedDigraph G, int source) { this.source = source; this.distTo = new double[G.V()]; this.edgeTo = new DirectedEdge[G.V()]; for (int v = 0; v < G.V(); ++v) { this.distTo[v] = Double.POSITIVE_INFINITY; } this.distTo[this.source] = 0.0; }
public AcyclicSP(EdgeWeightedDigraph G ,int s){ edgeTo = new DirectedEdge[G.V()]; distTo = new double[G.V()]; for(int v = 0 ; v < G.V() ; v++){ distTo[v] = Double.POSITIVE_INFINITY; } distTo[s] = 0.0; Topological top = new Topological(G); for(int v:top.order()){ relax(G,v); } }
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界 }
private void relax(EdgeWeightedDigraph G ,int v){ for(DirectedEdge e:G.adj(v)){ int w = e.to(); if(distTo[w]>distTo[v]+e.weight()){ distTo[w] = distTo[v]+e.weight(); edgeTo[w] = e; if(!onQ[w]){ q.enqueue(w); onQ[w] = true; } } if(cost++ %G.V() == 0 ) findNegativeCycle(); } }
/** * Initializes a new edge-weighted digraph that is a deep copy of <tt>G</tt>. * * @param G the edge-weighted digraph to copy */ public EdgeWeightedDigraph(EdgeWeightedDigraph G) { this(G.V()); this.E = G.E(); for (int v = 0; v < G.V(); v++) { // reverse so that adjacency list is in same order as original Stack<DirectedEdge> reverse = new Stack<DirectedEdge>(); for (DirectedEdge e : G.adj[v]) { reverse.push(e); } for (DirectedEdge e : reverse) { adj[v].add(e); } } }
/** @param args */ public static void main(String[] args) { // TODO Auto-generated method stub In in = new In("E:\\jwork\\algs4-data\\tinyEWDAG.txt"); EdgeWeightedDigraph G = new EdgeWeightedDigraph(in); int s = 5; AcyclicLP sp = new AcyclicLP(G, s); for (int i = 0; i < G.V(); i++) { System.out.printf(s + "-" + i + ":%.2f\n", sp.distTo(i)); for (DirectedEdge edge : sp.pathTo(i)) { System.out.print(edge + " "); } System.out.println(); } }
// check that algorithm computes either the topological order or finds a directed cycle private void dfs(EdgeWeightedDigraph G, int v) { onStack[v] = true; marked[v] = true; for (DirectedEdge e : G.adj(v)) { int w = e.to(); // short circuit if directed cycle found if (cycle != null) return; // found new vertex, so recur else if (!marked[w]) { edgeTo[w] = e; dfs(G, w); } // trace back directed cycle else if (onStack[w]) { cycle = new Stack<DirectedEdge>(); while (e.from() != w) { cycle.push(e); e = edgeTo[e.from()]; } cycle.push(e); } } onStack[v] = false; }
public BellmanFordSP(EdgeWeightedDigraph G ,int s){ distTo = new double[G.V()]; edgeTo = new DirectedEdge[G.V()]; onQ = new boolean[G.V()]; queue = new Queue<Integer>(); for(int v = 0 ; v < G.V() ; v++) distTo[v] = Double.POSITIVE_INFINITY; distTo[s] = 0.0; onQ[s]=true; while(!queue.isEmpty() && !hasNegativeCycle()){ int v = queue.dequeue(); onQ[v] = false; relax(G,v); } }
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"); } }
public void relax(EdgeWeightedDigraph G , int v){//vertex relaxation for(DirectedEdge e: G.adj(v)){//没有遍历树,只是遍历了从v指向的所有边(edges) int w = e.to(); if(distTo[w]>distTo[v]+e.weight){ distTo[w]=distTo[v]+e.weight; edgeTo[w]=e; } } }
private void relax(EdgeWeightedDigraph G, int v) { for (DirectedEdge edge : G.adj(v)) { int w = edge.to(); if (distTo[w] < distTo[v] + edge.weight()) { distTo[w] = distTo[v] + edge.weight(); edgeTo[w] = edge; } } }
public static void main(String[] args) { In in = new In(args[0]); int s = Integer.parseInt(args[1]); EdgeWeightedDigraph G = new EdgeWeightedDigraph(in); AcyclicLP lp = new AcyclicLP(G, s); for (int v = 0; v < G.V(); v++) { if (lp.hasPathTo(v)) { StdOut.printf("%d to %d (%.2f) ", s, v, lp.distTo(v)); for (DirectedEdge e : lp.pathTo(v)) { StdOut.print(e + " "); } StdOut.println(); } else { StdOut.printf("%d to %d no path\n", s, v); } } }
private void relax(EdgeWeightedDigraph G ,int v){ for(DirectedEdge e:G.adj(V)){ int w = e.to(); if(distTo[w]>distTo[v]+e.weight()){ distTo[w] = distTo[v]+e.weight(); edgeTo[w] = e; if(pq.contains(w)) pq.change(w,distTo[w]); else pq.insert(w,distTo[w]); } } }
// run DFS in edge-weighted digraph G from vertex v and compute preorder/postorder private void dfs(EdgeWeightedDigraph G, int v) { marked[v] = true; pre[v] = preCounter++; preorder.enqueue(v); for (DirectedEdge e : G.adj(v)) { int w = e.to(); if (!marked[w]) { dfs(G, w); } } postorder.enqueue(v); post[v] = postCounter++; }
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)); } }
public AcyclicLP(EdgeWeightedDigraph G, int s) { this.s = s; distTo = new double[G.V()]; for (int i = 0; i < distTo.length; i++) { distTo[i] = INFINITY; } edgeTo = new DirectedEdge[G.V()]; EdgeWeightedTopologicalOrder order = new EdgeWeightedTopologicalOrder(G); int count = 0; int kb = Integer.MAX_VALUE; edgeTo[s] = null; distTo[s] = 0; /* initialize*/ for (int i : order.reversePost()) { if (i == s) { kb = count; } if (count < kb) { count++; continue; } relax(G, i); } }
/** * 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)); }
DisjkstraAllPairsSP(EdgeWeightedDigraph G){ all = new DijkstraSP[G.V()]; for(int v = 0 ; v < G.V() ; v++){ all[v] = new DijkstraSP(G,v); } }