@@ -0,0 +1,2600 @@
+package me.alivecode.algs4;%0A%0Aimport edu.princeton.cs.algs4.In;%0Aimport edu.princeton.cs.algs4.StdOut;%0A%0A/**%0A * The %7B@code CC%7D class represents a data type for %0A * determining connected components in an undirected graph.%0A */%0Apublic class CC %7B%0A    private boolean%5B%5D marked; // marked%5Bv%5D = did v has been visisted?%0A    private int%5B%5D id; // id%5Bv%5D = the id of the component that vertex v belongs to%0A    private int%5B%5D size; // size%5Bid%5Bv%5D%5D = size of the component id%5Bv%5D%0A    private int count; // number of components in the graph%0A%0A    /**%0A     * Computes connected components in the undirected graph %7B@code G%7D%0A     */%0A    public CC(Graph G) %7B%0A        marked = new boolean%5BG.V()%5D;%0A        id = new int%5BG.V()%5D;%0A        size = new int%5BG.V()%5D;%0A        count = 0;%0A        for(int v = 0; v %3C G.V(); v++) %7B%0A            if (!marked%5Bv%5D) %7B%0A                dfs(G, v);%0A                count++;%0A            %7D%0A        %7D%0A    %7D %0A%0A    // depth first search%0A    private void dfs(Graph G, int v) %7B%0A        marked%5Bv%5D = true;%0A        id%5Bv%5D = count;%0A        size%5Bcount%5D++;%0A        for(int w: G.adj(v)) %7B%0A            if (!marked%5Bw%5D) %7B%0A                dfs(G, w);%0A            %7D%0A        %7D%0A    %7D%0A%0A    /**%0A     * Returns the number of connected components in the graph.%0A     *%0A     */%0A    public int count() %7B%0A        return count;%0A    %7D%0A%0A    /**%0A     * Returns the size of the connected component that vertex %7B@code v%7D belongs to.%0A     */%0A    public int size(int v) %7B%0A        validateVertex(v);%0A        return size%5Bid%5Bv%5D%5D;%0A    %7D%0A%0A    /**%0A     * Returns the id of the connected component that vertex %7B@code v%7D belongs to.%0A     */%0A    public int id(int v) %7B%0A        validateVertex(v);%0A        return id%5Bv%5D;%0A    %7D%0A%0A    /**%0A     * Is there a v-w path?%0A     */%0A    public boolean connected(int v, int w) %7B%0A        validateVertex(v);%0A        validateVertex(w);%0A        return id%5Bv%5D == id%5Bw%5D;%0A    %7D%0A%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    // unit test code%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        CC cc = new CC(G);%0A        StdOut.println(cc.count() + %22 component(s) in the graph%22);%0A        for(int id = 0; id %3C cc.count(); id++) %7B%0A            StdOut.print(%22component %22 + id + %22:%22);%0A            for(int v = 0; v %3C G.V(); v++) %7B%0A                if (cc.id(v) == id) %7B%0A                    StdOut.print(%22 %22 + v);%0A                %7D%0A            %7D%0A            StdOut.println();%0A        %7D%0A    %7D%0A%7D%0A%0A%0A%0A

Add CC class, a data type for determining connected components in an undirected graph.