JAVA easy to understand (DFS, count connected component)
\nclass Solution {\n    public int makeConnected(int n, int[][] connections) {\n      boolean visited []=new boolean[n];\n       ArrayList<Integer>[] adj=new Ar
```\nclass Solution {\n    public int makeConnected(int n, int[][] connections) {\n      boolean visited []=new boolean[n];\n       ArrayList<Integer>[] adj=new ArrayList[n];\n        for(int i=0;i<adj.length;i++){\n            adj[i]=new ArrayList<Integer>();\n        }\n      \n            for(int arr[]:connections){\n               adj[arr[0]].add(arr[1]);\n                adj[arr[1]].add(arr[0]);\n                  \n              }\n          \n          int no_of_component=0;\n        for(int i=0;i<n;i++){\n            if(visited[i]!=true){\n                no_of_component++;\n                dfs(i,visited,adj);\n            }\n            \n        }\n        return connections.length>=n-1?no_of_component-1:-1;\n    }\n        public void dfs(int n,boolean visited[],ArrayList<Integer>[]adj){\n            if(visited[n]!=true){\n                visited[n]=true;\n                for(int a:adj[n]){\n                    if(visited[a]!=true){\n                        dfs(a,visited,adj);\n                    }\n                }\n            }\n        }\n}\n```
['Breadth-First Search', 'Java']