Number of Operations to Make Network Connected
Find the fewest cable moves that connect every computer: one fewer than the number of separate groups.
The problem
There are n computers numbered 0 to n - 1, and each pair in connections is a cable between two of them. In one move you can unplug any cable and use it to join two computers that were not connected. Return the fewest moves needed to connect all the computers, directly or through others, or -1 if it can't be done.
Input n = 5, connections = [[0, 1], [1, 2], [0, 2], [3, 4]]
Output 1
The cable between 0 and 2 is spare, because 0 and 2 are already linked through 1. Moving it to join the two groups takes one move.
Input n = 5, connections = [[0, 1], [2, 3], [3, 4]]
Output -1
Connecting 5 computers needs at least 4 cables, and there are only 3.
Key constraint: Connecting n computers needs at least n - 1 cables, so with fewer cables the answer is -1 however you move them.
How to spot the pattern
- What matters is how many separate groups the network falls into.
- Cables come as pairs, and each one either joins two groups or is spare.
The idea
If the network falls into k separate groups, joining them takes exactly k - 1 new links, and each move provides one. The spare cables come from inside the groups: a group of s computers needs only s - 1 of its cables, so whenever there are at least n - 1 cables in total, there are enough spares. So check the cable count, count the groups with union-find, and return k - 1.
Brute force: Simulate the moves: repeatedly find a spare cable and two separate groups with a fresh DFS, which costs O(n * (n + E)) time.
Step by step
- 1If there are fewer than
n - 1cables, return -1. - 2Make a union-find of the
ncomputers and set the group count ton. - 3For each cable, union its two computers and lower the count whenever two groups merge.
- 4Return the group count minus 1.
See the pattern move
The union-find pattern on a small example. Step through it, or press play.
Code
class Solution:
def makeConnected(self, n: int, connections: List[List[int]]) -> int:
if len(connections) < n - 1:
return -1 # not enough cables to connect n computers
parent = list(range(n))
group_size = [1] * n
def find(x: int) -> int:
while parent[x] != x:
parent[x] = parent[parent[x]] # path halving: point to the grandparent
x = parent[x]
return x
def union(x: int, y: int) -> bool:
a, b = find(x), find(y)
if a == b:
return False # already one group: this cable is spare
if group_size[a] < group_size[b]:
a, b = b, a
parent[b] = a # hang the smaller group under the larger one
group_size[a] += group_size[b]
return True
groups = n
for a, b in connections:
if union(a, b):
groups -= 1
return groups - 1 # one move joins two groupsComplexity
Time
O(n + E)
Space
O(n)
E is the number of cables: each cable costs a nearly O(1) find and union, and the parent and size arrays hold n entries.
Watch out for
- Trying to track exactly which cable to move; only the number of groups matters.
- Forgetting the -1 case, when there are fewer than
n - 1cables. - Treating computers with no cables as connected; each one is a group of its own.
Follow-up questions
How would you list the actual moves?
During the union pass, keep every cable whose ends were already joined as a spare. Then link one computer from each extra group to computer 0's group, using one spare cable per link.
Can you count the groups without union-find?
Yes, with DFS or BFS from every unvisited computer over an adjacency list; it is also O(n + E).
In Hinglish
Pehle check karo ki cables n - 1 se kam toh nahi, warna -1. Phir union-find se alag groups gino; jitne groups hain, usse ek kam moves lagenge.