VOOZH about

URL: https://www.geeksforgeeks.org/dsa/minimize-count-of-connections-required-to-be-rearranged-to-make-all-the-computers-connected/

⇱ Minimum Operations to Connect Hospitals - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Minimum Operations to Connect Hospitals

Last Updated : 21 Nov, 2025

In a city, there are many hospitals. Each hospital is connected to some other hospitals, represented using an adjacency list adj[][], where adj[i] contains all hospitals directly connected to the i-th hospital.
Our task is to ensure that all hospitals are connected, either directly or indirectly to each other. In one operation, we can remove an existing link and reconnect it between two previously disconnected hospitals. Determine the minimum number of operations required to make the entire network connected. If it is impossible, return -1.

Examples:

Input: adj[][] = [[1, 2], [0, 2], [0, 1], []]
Output: 1
Explanation: Remove the connection between hospitals 1 and 2 and connect the hospitals 1 and 3.

πŸ‘ image1

Input: adj[][]= [[1, 2], [0], [0, 3], [2, 4],[3]]
Output: 0
Explanation: All hospitals are already connected directly or indirectly. No rearrangement of connections is required.

πŸ‘ image2

Intuition:
To connect all hospitals into one complete network, we must ensure that every hospital is reachable from every other hospital and that the entire system forms a single connected component. To connect all disconnected groups of hospitals, we need extra edges. These extra edges come from redundant connections- links that connect hospitals already reachable through some other path.
If the number of extra edges is enough to connect all disconnected groups, i.e., if extra edges β‰₯ (disconnected components βˆ’ 1), then we can rebuild the network. In that case, the minimum number of operations needed will also be (disconnected components βˆ’ 1) because that’s exactly how many new links are required to make the whole system fully connected.

[Approach 1] Using Disjoint Set

We use a Disjoint Set Union (DSU), because it helps us efficiently identify if two hospitals are already connected (directly or indirectly) and also merge two disconnected hospitals into one connected component whenever an edge exists.

While traversing, if two hospitals already belong to the same component, it means the current connection is redundant or extra. Otherwise, we merge them into one set. By the time we finish processing all connections, we know exactly how many extra edges we have.
Next, to find how many disconnected components exist, we check each hospital and see whether it has a different parent. If a hospital’s parent is different from others, it belongs to a different component.

After that, we simply compare the number of extra edges with the number of disconnected components to determine whether the entire network can be connected.


Output
1

Time Complexity: O(V+E), V = number of hospitals (nodes), E = total number of links (edges)
Auxiliary Space: O (V + E)

[Approach 2] Using DFS

According to the MST property, a component with V vertices requires exactly (V βˆ’ 1) edges to stay fully connected. If more edges than required are present, those extra edges are redundant and can be reused to connect other disconnected hospitals.
We compute the number of extra edges using:
Extra Edges = Total Edges βˆ’ (Vertices βˆ’ Disconnected Components)


After this, we simply find the number of disconnected components using DFS and check whether the extra edges are enough to connect them. To connect all components, we need (Disconnected Components βˆ’ 1) edges. If the extra edges are at least this number, all hospitals can be connected; otherwise, it is impossible.


Output
1

Time Complexity: O(V+E)
Auxiliary Space: O(V+E)

Comment