C++ || BFS & DFS approach || Easy and clean code
Here is my c++ code for this problem.\nBFS approach:-\n\'\'\'\n\n\tclass Solution {\n\tpublic:\n\t\tint findCircleNum(vector>& isConnected) {\n\t\t\tint V=isCon
Here is my c++ code for this problem.\nBFS approach:-\n\'\'\'\n\n\tclass Solution {\n\tpublic:\n\t\tint findCircleNum(vector<vector<int>>& isConnected) {\n\t\t\tint V=isConnected.size();\n\t\t\tvector<int>adjls[V];\n\t\t\tfor(int i=0; i<V; i++){\n\t\t\t\tfor(int j=0; j<V; j++){\n\t\t\t\t\tif(isConnected[i][j]==1 && i!=j){\n\t\t\t\t\t\tadjls[i].push_back(j);\n\t\t\t\t\t\tadjls[j].push_back(i);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\tvector<int>v(V, 0);\n\t\t\tint cnt=0;\n\t\t\tfor(int i=0; i<V; i++){\n\t\t\t\tif(v[i]==0){\n\t\t\t\t\tcnt++;\n\t\t\t\t\tqueue<int>q;\n\t\t\t\t\tq.push(i);\n\t\t\t\t\twhile(!q.empty()){\n\t\t\t\t\t\tint ft=q.front();\n\t\t\t\t\t\tq.pop();\n\t\t\t\t\t\tfor(auto it: adjls[ft]){\n\t\t\t\t\t\t\tif(!v[it]){\n\t\t\t\t\t\t\t\tv[it]=1;\n\t\t\t\t\t\t\t\tq.push(it);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn cnt;\n\t\t}\n\t};\n\'\'\'\nDFS approach:-\n\'\'\'\n\n\tclass Solution {\n\tpublic:\n\t\tvoid dfs(int st, vector<int>adjls[], vector<int>& v){\n\t\t  v[st]=1;\n\t\t  for(auto it: adjls[st]){\n\t\t\t  if(!v[it]){\n\t\t\t\t  v[it]=1;\n\t\t\t\t  dfs(it, adjls, v);\n\t\t\t  }\n\t\t  }\n\t  }\n\t\tint findCircleNum(vector<vector<int>>& isConnected) {\n\t\t\tint V=isConnected.size();\n\t\t\tvector<int>adjls[V];\n\t\t\tfor(int i=0; i<V; i++){\n\t\t\t\tfor(int j=0; j<V; j++){\n\t\t\t\t\tif(isConnected[i][j]==1 && i!=j){\n\t\t\t\t\t\tadjls[i].push_back(j);\n\t\t\t\t\t\tadjls[j].push_back(i);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\tvector<int>v(V, 0);\n\t\t\tint cnt=0;\n\t\t\tfor(int i=0; i<V; i++){\n\t\t\t\tif(v[i]==0){\n\t\t\t\t\tcnt++;\n\t\t\t\t\tint st=i;\n\t\t\t\t\tdfs(st, adjls, v);\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn cnt;\n\t\t}\n\t};\n\'\'\'
['Depth-First Search', 'Breadth-First Search', 'C']