@@ -0,0 +1,2792 @@
+package me.alivecode.algs4;%0A%0Aimport edu.princeton.cs.algs4.In;%0Aimport edu.princeton.cs.algs4.StdOut;%0A%0A/**%0A * The %7B@code EdgeWeightedDirectedCycle%7D class determines%0A * cycle in a %7B@link EdgeWeightedDigraph%7D.%0A */%0Apublic class EdgeWeightedDirectedCycle %7B%0A    private final boolean%5B%5D marked; // marked%5Bv%5D = has vertex v been marked ?%0A    private final DirectedEdge%5B%5D edgeTo; // edgeTo%5Bv%5D = previous edge on path to v%0A    private final boolean%5B%5D onStack; // onStack%5Bv%5D = is v on the path?%0A    private Stack%3CDirectedEdge%3E cycle; // the directed cycle or null if no cycle%0A%0A    /**%0A     * Determines the directed cycle in the specified edge-weighted digraph %7B@code G%7D.%0A     *%0A     * @param G the edge-weighted graph%0A     */%0A    public EdgeWeightedDirectedCycle(EdgeWeightedDigraph G) %7B%0A        marked = new boolean%5BG.V()%5D;%0A        onStack = new boolean%5BG.V()%5D;%0A        edgeTo = new DirectedEdge%5BG.V()%5D;%0A%0A        for(int v = 0; v %3C G.V(); v++) %7B%0A            if (!marked%5Bv%5D) %7B%0A                dfs(G, v);%0A            %7D%0A        %7D%0A%0A%0A    %7D%0A%0A    // see DirectedCycle class for ref%0A    private void dfs(EdgeWeightedDigraph G, int v) %7B%0A        marked%5Bv%5D = true;%0A        onStack%5Bv%5D = true;%0A%0A        for(DirectedEdge e : G.adj(v)) %7B%0A            int w = e.to();%0A%0A            if (cycle != null) return;%0A%0A            if (!marked%5Bw%5D) %7B%0A                edgeTo%5Bw%5D = e;%0A                dfs(G, w);%0A            %7D%0A            else if (onStack%5Bw%5D) %7B%0A                cycle = new Stack%3C%3E();%0A                DirectedEdge x = e;%0A                while(x.from() != w) %7B%0A                    cycle.push(x);%0A                    x = edgeTo%5Bx.from()%5D;%0A                %7D%0A                cycle.push(x);%0A            %7D%0A%0A        %7D%0A        onStack%5Bv%5D = false;%0A    %7D%0A%0A    /**%0A     * Is the specified edge-weighted digraph has any cycle?%0A     *%0A     * @return %7B@code true%7D if the specified edge-weighted digraph has cycle,%0A     * %7B@code false%7D otherwise%0A     */%0A    public boolean hasCycle() %7B%0A        return cycle != null;%0A    %7D%0A%0A    /**%0A     * Returns the directed cycle if the specified edge-weighted digraph%0A     * has cycle, %7B@code null%7D otherwise.%0A     *%0A     * @return the directed cycle if the specified edge-weighted%0A     * has cycle, %7B@code null%7D otherwise%0A     */%0A    public Iterable%3CDirectedEdge%3E cycle() %7B%0A        return cycle;%0A    %7D%0A%0A    // unit test code%0A    public static void main(String%5B%5D args) %7B%0A        In in = new In(args%5B0%5D);%0A        EdgeWeightedDigraph G = new EdgeWeightedDigraph(in);%0A        EdgeWeightedDirectedCycle finder = new EdgeWeightedDirectedCycle(G);%0A%0A        if (finder.hasCycle()) %7B%0A            for (DirectedEdge e : finder.cycle()) %7B%0A                StdOut.print(e + %22   %22);%0A            %7D%0A            StdOut.println();%0A        %7D%0A        else %7B%0A            StdOut.println(%22no cycle%22);%0A        %7D%0A    %7D%0A%0A%7D%0A

Add EdgeWeightedDirectedCycle class for determining cycles in edge-weighed digraphs.