001// Exercise 4.4.28 (Solution published at http://algs4.cs.princeton.edu/) 002package algs44; 003import stdlib.*; 004import algs13.Stack; 005import algs42.Topological; 006/* *********************************************************************** 007 * Compilation: javac AcyclicLP.java 008 * Execution: java AcyclicP V E 009 * Dependencies: EdgeWeightedDigraph.java DirectedEdge.java Topological.java 010 * Data files: http://algs4.cs.princeton.edu/44sp/tinyEWDAG.txt 011 * 012 * Computes longest paths in an edge-weighted acyclic digraph. 013 * 014 * Remark: should probably check that graph is a DAG before running 015 * 016 * % java AcyclicLP tinyEWDAG.txt 5 017 * 5 to 0 (2.44) 5->1 0.32 1->3 0.29 3->6 0.52 6->4 0.93 4->0 0.38 018 * 5 to 1 (0.32) 5->1 0.32 019 * 5 to 2 (2.77) 5->1 0.32 1->3 0.29 3->6 0.52 6->4 0.93 4->7 0.37 7->2 0.34 020 * 5 to 3 (0.61) 5->1 0.32 1->3 0.29 021 * 5 to 4 (2.06) 5->1 0.32 1->3 0.29 3->6 0.52 6->4 0.93 022 * 5 to 5 (0.00) 023 * 5 to 6 (1.13) 5->1 0.32 1->3 0.29 3->6 0.52 024 * 5 to 7 (2.43) 5->1 0.32 1->3 0.29 3->6 0.52 6->4 0.93 4->7 0.37 025 * 026 *************************************************************************/ 027 028public class AcyclicLP { 029 private final double[] distTo; // distTo[v] = distance of longest s->v path 030 private final DirectedEdge[] edgeTo; // edgeTo[v] = last edge on longest s->v path 031 032 public AcyclicLP(EdgeWeightedDigraph G, int s) { 033 distTo = new double[G.V()]; 034 edgeTo = new DirectedEdge[G.V()]; 035 for (int v = 0; v < G.V(); v++) distTo[v] = Double.NEGATIVE_INFINITY; 036 distTo[s] = 0.0; 037 038 // relax vertices in toplogical order 039 Topological topological = new Topological(G); 040 for (int v : topological.order()) { 041 for (DirectedEdge e : G.adj(v)) 042 relax(e); 043 } 044 } 045 046 // relax edge e, but update if you find a *longer* path 047 private void relax(DirectedEdge e) { 048 int v = e.from(), w = e.to(); 049 if (distTo[w] < distTo[v] + e.weight()) { 050 distTo[w] = distTo[v] + e.weight(); 051 edgeTo[w] = e; 052 } 053 } 054 055 // return length of the longest path from s to v, -infinity if no such path 056 public double distTo(int v) { 057 return distTo[v]; 058 } 059 060 // is there a path from s to v? 061 public boolean hasPathTo(int v) { 062 return distTo[v] > Double.NEGATIVE_INFINITY; 063 } 064 065 // return view of longest path from s to v, null if no such path 066 public Iterable<DirectedEdge> pathTo(int v) { 067 if (!hasPathTo(v)) return null; 068 Stack<DirectedEdge> path = new Stack<>(); 069 for (DirectedEdge e = edgeTo[v]; e != null; e = edgeTo[e.from()]) { 070 path.push(e); 071 } 072 return path; 073 } 074 075 076 077 public static void main(String[] args) { 078 In in = new In(args[0]); 079 int s = Integer.parseInt(args[1]); 080 EdgeWeightedDigraph G = new EdgeWeightedDigraph(in); 081 082 AcyclicLP lp = new AcyclicLP(G, s); 083 084 for (int v = 0; v < G.V(); v++) { 085 if (lp.hasPathTo(v)) { 086 StdOut.format("%d to %d (%.2f) ", s, v, lp.distTo(v)); 087 for (DirectedEdge e : lp.pathTo(v)) { 088 StdOut.print(e + " "); 089 } 090 StdOut.println(); 091 } 092 else { 093 StdOut.format("%d to %d no path\n", s, v); 094 } 095 } 096 } 097}