@@ -0,0 +1,2712 @@
+package me.alivecode.algs4;%0A%0Aimport edu.princeton.cs.algs4.In;%0Aimport edu.princeton.cs.algs4.StdOut;%0A%0Aimport java.util.Iterator;%0A%0A/**%0A * The %7B@code DirectedCycle%7D class checks%0A * if the specified directed graph has any cycle.%0A */%0Apublic class DirectedCycle %7B%0A    private Stack%3CInteger%3E cycle;%0A    private boolean%5B%5D marked;%0A    private int%5B%5D edgeTo;%0A    private boolean%5B%5D onStack; // onStack%5Bw%5D = is w on the path to v?%0A%0A    /**%0A     * Find cycle in the specified directed cycle.%0A     *%0A     * @param DG the directed graph%0A     */%0A    public DirectedCycle(Digraph DG) %7B%0A        marked = new boolean%5BDG.V()%5D;%0A        edgeTo = new int%5BDG.V()%5D;%0A        onStack = new boolean%5BDG.V()%5D;%0A%0A        for(int v = 0; v %3C DG.V(); v++) %7B%0A            if (!marked%5Bv%5D) %7B%0A                dfs(DG, v, -1);%0A            %7D%0A        %7D%0A    %7D%0A%0A    /*%0A        in the directed graph below has directed cycle 3-%3E1-%3E4-%3E3%0A        5%0A        5%0A        0 2%0A        1 4%0A        4 3%0A        3 1%0A        4 0%0A%0A        vertex 0 will be marked first and not in any cycle.%0A        'onStack' make sure marked vertices like vertex 0%0A        will not make not closed cycle.%0A     */%0A    private void dfs(Digraph DG, int v, int s) %7B%0A        marked%5Bv%5D = true;%0A        onStack%5Bv%5D = true;%0A%0A        for(int w: DG.adj(v)) %7B%0A            if (cycle != null) return;;%0A            if (!marked%5Bw%5D) %7B%0A                edgeTo%5Bw%5D = v;%0A                dfs(DG, w, v);%0A            %7D%0A            else if(onStack%5Bw%5D) %7B%0A                cycle = new Stack%3C%3E();%0A                for(int x = v; x != w; x = edgeTo%5Bx%5D) %7B%0A                    cycle.push(x);%0A                %7D%0A                cycle.push(w);%0A                cycle.push(v);%0A            %7D%0A%0A        %7D%0A        onStack%5Bv%5D = false;%0A    %7D%0A%0A    /**%0A     * Does the directed graph have cycle?%0A     *%0A     * @return %7B@code true%7D if the specified directed graph 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 first found directed cycle if the directed graph has any cycle,%0A     * %7B@code null%7D otherwise.%0A     *%0A     * @return the first found directed cycle if the directed graph has any cycle,%0A     * %7B@code null%7D otherwise.%0A     */%0A    public Iterable%3CInteger%3E cycle() %7B%0A        return cycle;%0A    %7D%0A%0A    // unit test%0A    public static void main(String%5B%5D args) %7B%0A        In in = new In(args%5B0%5D);%0A        Digraph DG = new Digraph(in);%0A        DirectedCycle dc = new DirectedCycle(DG);%0A        if (dc.hasCycle()) %7B%0A            for (int v: dc.cycle()) %7B%0A                StdOut.print(v + %22 %22);%0A            %7D%0A            StdOut.println();%0A        %7D%0A        else %7B%0A            StdOut.println(%22The digraph has no cycle.%22);%0A        %7D%0A%0A    %7D%0A%7D%0A

Add DirectedCycle class, a data type for determining cycle in directed graph.