Given an integer N, denoting the number of computers connected by cables forming a network and a 2D array connections[][], with each row (i, j) representing a connection between ith and jth computer, the task is to connect all the computers either directly or indirectly by removing any of the given connections and connecting two disconnected computers.
If it’s not possible to connect all the computers, print -1.
Otherwise, print the minimum number of such operations required.
Examples:
Input: N = 4, connections[][] = {{0, 1}, {0, 2}, {1, 2}}
Output: 1
Explanation: Remove the connection between computers 1 and 2 and connect the computers 1 and 3.Input: N = 5, connections[][] = {{0, 1}, {0, 2}, {3, 4}, {2, 3}}
Output: 0
Approach: The idea is to use a concept similar to that of Minimum Spanning Tree, as in a Graph with N nodes, only N – 1 edges are required to make all the nodes connected.
Redundant edges = Total edges – [(Number of Nodes – 1) – (Number of components – 1)]
If Redundant edges > (Number of components – 1) : It is clear that there are enough wires that can be used to connect disconnected computers. Otherwise, print -1.
Follow the steps below to solve the problem:
- Initialize an unordered map, say adj to store the adjacency list from the given information about edges.
- Initialize a vector of boolean datatype, say visited, to store whether a node is visited or not.
- Generate the adjacency list and also calculate the number of edges.
- Initialize a variable, say components, to store the count of connected components.
- Traverse the nodes of the graph using DFS to count the number of connected components and store it in the variable components.
- Initialize a variable, say redundant, and store the number of redundant edges using the above formula.
- If redundant edges > components – 1, then the minimum number of required operations is equal to components – 1. Otherwise, print -1.
Below is the implementation of the above approach:
C++
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function to visit the nodes of a graph void DFS(unordered_map<int, vector<int> >& adj, int node, vector<bool>& visited) { // If current node is already visited if (visited[node]) return; // If current node is not visited visited[node] = true; // Recurse for neighbouring nodes for (auto x : adj[node]) { // If the node is not vistied if (visited[x] == false) DFS(adj, x, visited); } } // Utility function to check if it is // possible to connect all computers or not int makeConnectedUtil(int N, int connections[][2], int M) { // Stores whether a // node is visited or not vector<bool> visited(N, false); // Build the adjacency list unordered_map<int, vector<int> > adj; // Stores count of edges int edges = 0; // Building adjaceny list // from the given edges for (int i = 0; i < M; ++i) { // Add edges adj[connections[i][0]].push_back( connections[i][1]); adj[connections[i][1]].push_back( connections[i][0]); // Increment count of edges edges += 1; } // Stores count of components int components = 0; for (int i = 0; i < N; ++i) { // If node is not visited if (visited[i] == false) { // Increment components components += 1; // Perform DFS DFS(adj, i, visited); } } // At least N - 1 edges are required if (edges < N - 1) return -1; // Count redundant edges int redundant = edges - ((N - 1) - (components - 1)); // Check if components can be // rearranged using redundant edges if (redundant >= (components - 1)) return components - 1; return -1; } // Function to check if it is possible // to connect all the computers or not void makeConnected(int N, int connections[][2], int M) { // Stores counmt of minimum // operations required int minOps = makeConnectedUtil( N, connections, M); // Print the minimum number // of operations required cout << minOps; } // Driver Code int main() { // Given number of computers int N = 4; // Given set of connections int connections[][2] = { { 0, 1 }, { 0, 2 }, { 1, 2 } }; int M = sizeof(connections) / sizeof(connections[0]); // Function call to check if it is // possible to connect all computers or not makeConnected(N, connections, M); return 0; } |
1
Time Complexity: O(N + M)
Auxiliary Space: O(N)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

