Connected Components DFS C++


  • 0
    W
    void dfs(vector<vector<int>>& M, vector<int>& V, int i) {
            for (int j = 0; j < M.size(); j++) {
                if (i == j || M[i][j] == 0 || V[j] == 1) continue;
                V[j] = 1;
                dfs(M, V, j);
            }
        }
    
        int findCircleNum(vector<vector<int>>& M) {
            
            vector<int> V(M.size(), 0);
            int count = 0;
            for (int i = 0; i < M.size(); i++) {
                if (V[i]) continue;
                count++;
                dfs(M, V, i);
            }
            return (count);
        }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.