✅ Java | Easy | BFS | Similar to Number of Islands
Similar to number of islands with a slight change. The change is if the number of edges is less than n-1 (because we know minimum number of edges required for a
**Similar to number of islands with a slight change. The change is if the number of edges is less than n-1 (because we know minimum number of edges required for a graph of n vertices to remain connected is n-1) then we will return -1 or else If the number of edges is >= n-1 then we have sufficient wires to connect all the computers so we return (components-1) as counting the number of components signifies how many group of computers are not connected. So we required (components-1) number of wires so that all computers are directly or indirectly connected with each other.**\n```\nclass Solution {\n    public int makeConnected(int n, int[][] connections) {\n        if(connections.length<n-1)\n            return -1;\n        List<List<Integer>> adj=new ArrayList<>();\n        for(int i=0;i<n;i++){\n            adj.add(new ArrayList<>());\n        }\n        for(int i=0;i<connections.length;i++){\n            adj.get(connections[i][0]).add(connections[i][1]);\n            adj.get(connections[i][1]).add(connections[i][0]);\n        }\n        boolean vis[]=new boolean[n];\n        Queue<Integer> qu=new LinkedList<>();\n        qu.add(0);\n        int comp=0;\n        for(int i=0;i<n;i++){\n            if(!vis[i]){\n                bfs(i,vis,adj);\n                comp++;\n            }\n        }\n        return comp-1;\n    }\n    public void bfs(int node,boolean[] vis,List<List<Integer>> adj){\n        Queue<Integer> qu=new LinkedList<>();\n        qu.add(node);\n        while(!qu.isEmpty()){\n            int curr=qu.poll();\n            for(int adjnode:adj.get(curr)){\n                if(!vis[adjnode]){\n                    qu.add(adjnode);\n                    vis[adjnode]=true;\n                }\n            }\n        }\n    }\n}\n```\n![image](https://assets.leetcode.com/users/images/5c243964-c160-4231-97b7-b7f2dc373604_1679569819.3938613.png)\n\n![image](https://assets.leetcode.com/users/images/6cebf425-8b1f-482e-bb28-4c8a9585114b_1679569882.6787083.jpeg)\n
['Breadth-First Search', 'Union Find', 'Graph', 'Java']