Minimum Operations Required to Make the Network Connected5 Jan 2025 | 8 min read In 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). |
exec() in Python
Introduction: In this tutorial, we are learning about . The exec() function is used for dynamically execute a Python program, which can be a string or an object of the code. If it is a string, the string is split into a bunch of Python statements...
6 min read
Response.headers - Python Requests
Python is a versatile programming language with many libraries for diverse applications, such as web scraping, data retrieval, and web interactions. 'requests' is a popular Python library for making HTTP requests. It offers a straightforward and beautiful API for sending HTTP queries and receiving answers. When...
4 min read
Lua vs Python
When we talk about scripting languages, we're referring to special types of computer languages used for specific purposes. Think of them as tools designed for particular tasks, like fixing a leaky faucet with a specific wrench rather than a general toolkit. Some of these scripting languages,...
25 min read
Telco Customer ChurnRate Analysis
In this Tutorial, we will go over how we developed simple yet practical models to account for the churn rate using the Kaggle Telco Customer dataset. Background and Problem; Data Summary and Exploratory Analysis; Data Analyses; Strategy Recommendations, The drawbacks, and Future Research are all included in the particular procedure. Background Given...
14 min read
Create a Hash Map in Python
Introduction to Hash Map There are different data types that might be used to access data. One of them is the hash map. Hash maps are a fundamental information shape in programming that permits the garage and retrieval of records based totally on key-cost pairs. In...
6 min read
Why Substring Slicing Index Out of Range Works in Python
? Introduction Python, known for its simplicity and readability, offers a rich arrangement of highlights that make it a preferred choice for many developers. One such component is slicing, a strategy that permits you to extricate portions of sequences such as strings, lists, and tuples. Slicing isn't...
6 min read
Background Subtraction Using OpenCV in Python
An Introduction to Background Subtraction Background subtraction is an essential computer vision and image processing technique that segregates moving objects from a static background in video sequences. One of the most used techniques, background subtraction has various applications, such as: Surveillance: Detecting intrusion or...
7 min read
What does the Double Star operator mean in Python
? Python's double star operator, **, is a powerful feature that allows you to work with keyword arguments in a flexible and dynamic way. This operator is used in function definitions and function calls to handle a variable number of keyword arguments. In this article, we'll...
3 min read
Python Membership Operators
In Python, membership operators are additional operators that help us check if a specified value or element exists in a certain sequence or collection like strings, lists, tuples, sets or dictionaries. In other words, these operators are used to test the membership of an item...
8 min read
How to Remove All Leading Whitespace in String in Python
? A string is a group of letters that can stand in for an entire phrase or just one word. Because they may be defined with or without a specifier and don't require explicit declaration, strings in Python are simple to use. Python has built-in procedures and...
4 min read
We request you to subscribe our newsletter for upcoming updates.

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