Minimum Operations Required to Make the Network ConnectedIn this problem, we are given a network of n components. The components are connected to each other, and the information about the connections is given in the form of a 2D array. Our task in this problem is to find the number of minimum extra connections required such that all the components are connected to one of the components of the network. If there is no way that all the components can be connected, then we will return -1. Examples: Input: N = 5, c = [[0, 1], [1, 2], [0, 2], [1, 3], [0, 3]] Output: 1 Input: N = 6, c = [[0, 1], [1, 2], [0, 2], [1, 3], [0, 3]] Output: 2 Approach - 1In this problem, we will use the disjoint set to find the number of extra connections in the given network. A disjoint set is a type of data structure. This data structure stores the sets of elements that do not have any element in common. Hence, the name disjoint set. The number of extra connections will tell us the number of new connections that we can make to connect the whole network. If the number of extra connections is less than the number of components - 1, then there is a way to connect the network, and this network will contain n - 1 edges where n is the total number of nodes in the graph. If the condition is not satisfied, then we will return - 1. We will follow these steps:
Below is the implementation of the above approach. Code Output: 1 Time Complexity: The DFS takes linear time to traverse through all the nodes of the graph. The operations on the disjoint set work in constant time. Since the total number of nodes is n, the time complexity of this approach is O(n). Space Complexity: We have used extra space to store the set; therefore, the space complexity of this approach is O (e + n), where e is the number of edges and n is the number of nodes. Approach - 2In this approach, we will use the minimum spanning tree method to find the minimum number of connections required to connect the whole network. Minimum Spanning TreeSuppose we have an undirected graph. The edges of the graph are weighted. The graph has N number of vertices and E number of edges, and let S be the set containing the edges. A minimum spanning tree is the subset of the set S that contains the edges that are enough to span the complete tree or graph and whose total is the minimum weight of all the possible sets of edges. Thus, a minimum spanning tree contains N - 1 number of edges whose total weight is minimum. We can use this concept in our problem as we also need N - 1 number of connections to connect the whole network. Let us see the algorithm of this approach:
Below is the implementation of this approach in Python. Code Output: 1 Time Complexity: In this approach, we have used the DFS to find the minimum connections required; hence, the time complexity of this approach is O(n), where n is the number of nodes in the graph. Space Complexity: We have used extra space to store the array and keep track of the visited nodes. Hence, the space complexity of this approach is O(n). |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India