DFS and BFS
\nJust Started Graphs   \uD83D\uDD25\n\nAdded More intuitive approaches!\n\n\n\n\n# Code\njava []\n// BFS\n\nclass Solution {\n    public int findCircleNum(int[
\nJust Started Graphs   \uD83D\uDD25\n\nAdded More intuitive approaches!\n\n<br/>\n\n\n# Code\n```java []\n// BFS\n\nclass Solution {\n    public int findCircleNum(int[][] isConnected) {\n        int n = isConnected.length;\n        boolean[] visited = new boolean[n];\n        int provinces = 0;\n\n        for (int i = 0; i < n; i++) {\n            if (!visited[i]) {\n                provinces++;\n                bfs(isConnected, visited, i);\n            }\n        }\n\n        return provinces;\n    }\n\n    private void bfs(int[][] isConnected, boolean[] visited, int city) {\n        Queue<Integer> queue = new LinkedList<>();\n        queue.add(city);\n        visited[city] = true;\n\n        while (!queue.isEmpty()) {\n            int current = queue.poll();\n            for (int neighbor = 0; neighbor < isConnected.length; neighbor++) {\n                if (isConnected[current][neighbor] == 1 && !visited[neighbor]) {\n                    queue.add(neighbor);\n                    visited[neighbor] = true;\n                }\n            }\n        }\n    }\n}\n\n```\n```java []\n//DFS approach\n\nclass Solution {\n    public int findCircleNum(int[][] isConnected) {\n        int n = isConnected.length;\n        boolean[] visited = new boolean[n];\n        int provinces = 0;\n\n        for (int i = 0; i < n; i++) {\n            if (!visited[i]) {\n                provinces++;\n                dfs(isConnected, visited, i);\n            }\n        }\n\n        return provinces;\n    }\n\n    private void dfs(int[][] isConnected, boolean[] visited, int city) {\n        visited[city] = true;\n        for (int neighbor = 0; neighbor < isConnected.length; neighbor++) {\n            if (isConnected[city][neighbor] == 1 && !visited[neighbor]) {\n                dfs(isConnected, visited, neighbor);\n            }\n        }\n    }\n}\n\n```\n```java []\n//BFS approach\n\nclass Solution {\n    public int findCircleNum(int[][] isConnected) {\n        \n        ArrayList<ArrayList<Integer>> adjLs = new ArrayList<ArrayList<Integer>>(); \n        int V = isConnected.length;\n        for(int i = 0;i<V;i++) {\n            adjLs.add(new ArrayList<Integer>()); \n        }\n        \n        // to change adjacency matrix to list \n        for(int i = 0;i<V;i++) {\n            for(int j = 0;j<V;j++) {\n                // self nodes are not considered \n                if(isConnected[i][j] == 1 && i != j) {\n                    adjLs.get(i).add(j); \n                    adjLs.get(j).add(i); \n                }\n            }\n        }\n        boolean vis[] = new boolean[V]; \n        int cnt = 0; \n        for(int i = 0;i<V;i++) {\n            if(vis[i] ==false) {\n               cnt++;\n               bfs(i, adjLs, vis); \n            }\n        }\n        return cnt; \n    }\n\n    private void bfs(int node, ArrayList<ArrayList<Integer>> adjLs, boolean [] vis) {\n        \n        Queue<Integer> q = new LinkedList<>();\n        vis[node] = true;\n        q.offer(node);\n        while(!q.isEmpty()){\n            int currentNode = q.poll();\n\n            for(Integer it: adjLs.get(currentNode)){\n                if(vis[it]==false){\n                    vis[it]=true;\n                    q.offer(it);\n                }\n            }\n        }\n    }\n}\n```\n```java []\n//DFS approach\n\nclass Solution {\n    public int findCircleNum(int[][] isConnected) {\n        \n        ArrayList<ArrayList<Integer>> adjLs = new ArrayList<ArrayList<Integer>>(); \n        int V = isConnected.length;\n        for(int i = 0;i<V;i++) {\n            adjLs.add(new ArrayList<Integer>()); \n        }\n        \n        // to change adjacency matrix to list \n        for(int i = 0;i<V;i++) {\n            for(int j = 0;j<V;j++) {\n                // self nodes are not considered \n                if(isConnected[i][j] == 1 && i != j) {\n                    adjLs.get(i).add(j); \n                    adjLs.get(j).add(i); \n                }\n            }\n        }\n        boolean vis[] = new boolean[V]; \n        int cnt = 0; \n        for(int i = 0;i<V;i++) {\n            if(vis[i] ==false) {\n               cnt++;\n               dfs(i, adjLs, vis); \n            }\n        }\n        return cnt; \n    }\n\n    private void dfs(int node, ArrayList<ArrayList<Integer>> adjLs, boolean [] vis) {\n        vis[node] = true;\n        \n        for(Integer it: adjLs.get(node)){\n            if(vis[it]==false){\n                vis[it]=true;\n                dfs(it,adjLs,vis);\n            }\n        }\n    }\n}\n```
['Java']