@@ -0,0 +1,2601 @@
+package me.alivecode.algs4;%0A%0Aimport edu.princeton.cs.algs4.StdOut;%0Aimport edu.princeton.cs.algs4.In;%0A%0A/**%0A * The %7B@code DepthFirstSearch%7D class represents a data type for%0A * determining the vertices connected the given source vertex.%0A */%0Apublic class DepthFirstSearch %7B%0A    private int count; // number of vertices connnected to s%0A    private boolean%5B%5D marked; // marked%5Bv%5D = Is there an v-s path?%0A%0A    /**%0A     * Computes the vertices in graph %7B@code G%7D that%0A     * are conected to source vertex %7B@code s%7D.%0A     *%0A     * @param G the graph%0A     * @param s the source vertex%0A     * @throws IllegalArgumentException unless %7B@code 0 %3C= s %3C V%7D%0A     */%0A    public DepthFirstSearch(Graph G, int s) %7B%0A        count = 0;%0A        marked = new boolean%5BG.V()%5D;%0A        validateVertex(s);%0A        dfs(G, s);%0A    %7D%0A    %0A    // throws IllegalArgumentException unless 0 %3C= v %3C V %0A    private void validateVertex(int v) %7B%0A        int V = marked.length;%0A        if (v %3C 0 %7C%7C v %3E= V)%0A            throw new IllegalArgumentException(%22vertex %22 + v + %22 is not between 0 and %22 + (V-1)); %0A    %7D%0A%0A    // computes the vertices in graph G that%0A    // are connected to vertex s%0A    private void dfs(Graph G, int s) %7B%0A        validateVertex(s);%0A        marked%5Bs%5D = true;%0A        count++;%0A        for(int w : G.adj(s)) %7B%0A            if (!marked%5Bw%5D) %7B%0A                dfs(G, w);%0A            %7D%0A        %7D%0A    %7D%0A%0A    /**%0A     * Is there a path between source vertex %7B@code s%7D and vertex %7B@code v%7D?%0A     *%0A     * @param v the target vertex%0A     * @return %7B@code true%7D if there is a path, %7B@code false%7D otherwise%0A     * @throws IllegalArgumentException unless %7B@code 0 %3C= v %3C V%7D%0A     */ %0A    public boolean marked(int v) %7B%0A        validateVertex(v);%0A        return marked%5Bv%5D;%0A    %7D%0A%0A    /**%0A     * Returns number of vertices that are connected to source vertex %7B@code s%7D.%0A     *%0A     * @returns number of vertices that are connected to source vertex %7B@code s%7D %0A     */%0A    public int count() %7B%0A        return count;%0A    %7D%0A%0A    // unit text code%0A    // DepthFirstSearch input.txt v%0A    public static void main(String%5B%5D args) %7B%0A        In in = new In(args%5B0%5D);%0A        Graph G = new Graph(in);%0A        int s = Integer.parseInt(args%5B1%5D);%0A        DepthFirstSearch search = new DepthFirstSearch(G, s);%0A%0A        for(int v = 0; v %3C G.V(); v++) %7B%0A            if (search.marked(v)) %7B%0A                StdOut.print(v + %22 %22);%0A            %7D%0A        %7D%0A%0A        StdOut.println();%0A        if (search.count() != G.V()) %7B%0A            StdOut.println(%22NOT connected.%22);%0A        %7D %0A        else %7B%0A            StdOut.println(%22connected.%22);%0A        %7D%0A    %7D%0A%7D%0A

Add DepthFirstSearch that computs the vertices in graph G that are connected to soruce vertex s.