@@ -0,0 +1,930 @@
+// Solution is connected components problem, we are given adjacency matrix and are asked to find%0A// isolated components in the graph.%0A// For each vertex we perform DFS (or BFS) and mark connected vertices. Every new vertex in outer%0A// loop marks new component.%0A//%0A// Solution beats 69.80%25 of submissions.%0A// Algorithm runs in O(n%5E2) time and O(n) space.%0Aclass Solution %7B%0A  public int findCircleNum(int%5B%5D%5B%5D M) %7B%0A    int n = M.length; // number of vertices%0A    boolean%5B%5D visited = new boolean%5Bn%5D;%0A    int count = 0;%0A    for (int i = 0; i %3C n; i++) %7B%0A      if (!visited%5Bi%5D) %7B%0A        // depth-first search%0A        dfs(M, i, visited);%0A        count++;%0A      %7D%0A    %7D%0A    return count;%0A  %7D%0A%0A  private void dfs(int%5B%5D%5B%5D M, int v, boolean%5B%5D visited) %7B%0A    visited%5Bv%5D = true;%0A    int%5B%5D adjacent = M%5Bv%5D;%0A    for (int j = 0; j %3C adjacent.length; j++) %7B%0A      if (M%5Bv%5D%5Bj%5D == 1 && !visited%5Bj%5D) %7B%0A        dfs(M, j, visited);%0A      %7D%0A    %7D%0A  %7D%0A%7D%0A

add solution