22. Minimum Operations to Connect Hospitals
β GFG solution to the Minimum Operations to Connect Hospitals problem: determine minimum reconnections needed to connect all hospitals using Union-Find with path compression. π
The problem can be found at the following link: π Question Link
π§© Problem Description
You are given an undirected network of V hospitals numbered from 0 to V - 1, represented as a 2D array edges[][], where each element edges[i] = [u, v] denotes a direct connection between hospital u and hospital v.
In one operation, you are allowed to remove any existing link and reconnect it between two hospitals that are currently not directly or indirectly connected.
Your task is to determine the minimum number of operations required to make sure that all hospitals become connected, either directly or indirectly, using the given links.
Note: If it is impossible to connect all hospitals into a single network, return -1.
π Examples
Example 1
Input: V = 4, E = 3, edges[][] = [[0, 1], [0, 2], [1, 2]]
Output: 1
Explanation: Remove the connection between hospitals 1 and 2 and connect hospitals 1 and 3.
The redundant edge [1, 2] can be used to connect the isolated hospital 3.Example 2
Input: V = 5, E = 4, edges[][] = [[0, 1], [0, 2], [2, 3], [3, 4]]
Output: 0
Explanation: All hospitals are already connected directly or indirectly.
No rearrangement of connections is required.π Constraints
$1 \le V \le 10^3$
$0 \le E \le V \times (V-1)/2$
$0 \le \text{edges}[i][0], \text{edges}[i][1] < V$
β
My Approach
The optimal approach uses the Disjoint Set Union (DSU) data structure with path compression to efficiently track connected components:
Union-Find with Path Compression
Initial Check:
If the number of edges is less than
V - 1, it's impossible to connect all hospitals, return -1.A tree connecting V nodes requires at least V-1 edges.
Initialize DSU:
Create a parent array where each hospital is initially its own parent.
Track the number of components, initially equal to V.
Process Edges:
For each edge, find the root representatives of both hospitals using path compression.
If they belong to different components, union them by updating parent pointers.
Decrement the component count when a successful union occurs.
Calculate Result:
After processing all edges, the number of operations needed equals
components - 1.This represents the number of edges needed to connect all remaining disconnected components.
Path Compression Optimization:
During the find operation, compress paths by making nodes point directly to the root.
This ensures nearly constant-time operations for subsequent queries.
π Time and Auxiliary Space Complexity
Expected Time Complexity: O(E Γ Ξ±(V)), where E is the number of edges and Ξ± is the inverse Ackermann function. Due to path compression, Ξ±(V) is effectively constant (β€ 5) for all practical values, making this approach nearly linear in practice.
Expected Auxiliary Space Complexity: O(V), as we only use a parent array of size V to track the disjoint set structure. No additional data structures like adjacency lists are required.
π§βπ» Code (C++)
β Code (Java)
π Code (Python)
π§ Contribution and Support
For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: π¬ Any Questions?. Let's make this learning journey more collaborative!
β If you find this helpful, please give this repository a star! β
πVisitor Count
Last updated