Number of Operations to Make Network Connected - Problem

Imagine you're a network administrator managing n computers numbered from 0 to n-1. These computers are connected by ethernet cables represented by the array connections, where connections[i] = [ai, bi] means computers ai and bi are directly connected.

Currently, any computer can reach any other computer either directly or through a series of connections. However, your network might be fragmented into separate groups that cannot communicate with each other.

Your mission: You can unplug cables between directly connected computers and reconnect them between any pair of disconnected computers. Find the minimum number of cable relocations needed to ensure all computers are connected in one unified network.

If it's impossible to connect all computers (not enough cables), return -1.

Input & Output

example_1.py โ€” Basic Case
$ Input: n = 4, connections = [[0,1],[0,2],[1,2]]
โ€บ Output: 1
๐Ÿ’ก Note: We have 4 computers with 3 connections forming one component [0,1,2] and one isolated computer [3]. Since we have 3 edges but only need 3 edges minimum for 4 computers, we can remove one redundant edge (like [1,2] since [0,1] and [0,2] already connect them) and use it to connect computer 3. Result: 1 operation.
example_2.py โ€” Multiple Components
$ Input: n = 6, connections = [[0,1],[0,2],[0,3],[1,2]]
โ€บ Output: 2
๐Ÿ’ก Note: We have 6 computers with connections forming one component [0,1,2,3] and two isolated computers [4] and [5]. We have 4 edges but need minimum 5 edges for 6 computers. Since we only have 4 edges, we can't connect all computers. Wait - we do have enough! We have 3 components total, so need 3-1=2 moves to connect them all.
example_3.py โ€” Impossible Case
$ Input: n = 6, connections = [[0,1],[0,2],[0,3],[1,2],[1,3]]
โ€บ Output: 2
๐Ÿ’ก Note: We have 6 computers forming components [0,1,2,3], [4], and [5]. That's 3 components, so we need 3-1=2 operations to connect them. We have 5 connections which is โ‰ฅ 6-1=5 minimum needed, so it's possible.

Visualization

Tap to expand
Network Connection OptimizationBuilding A012Extra cable!Building B34Building C5IsolatedMove cable 1Move cable 2Result: 2 cable moves needed3 buildings โ†’ need 3-1 = 2 inter-building connections๐ŸŽฏ Key: Excess cables within buildings can be moved between buildings
Understanding the Visualization
1
Count Buildings
Each connected group of computers represents one building
2
Check Cable Supply
Need at least n-1 cables total to connect n computers
3
Calculate Moves
To connect k buildings, need exactly k-1 inter-building cables
4
Rearrange Cables
Move redundant intra-building cables to connect buildings
Key Takeaway
๐ŸŽฏ Key Insight: To connect k separate components, you need exactly k-1 connections. Any extra cables can be relocated!

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(E ร— ฮฑ(n))

E edges ร— inverse Ackermann function (nearly constant)

n
2n
โœ“ Linear Growth
Space Complexity
O(n)

Parent and rank arrays for Union-Find structure

n
2n
โšก Linearithmic Space

Constraints

  • 1 โ‰ค n โ‰ค 105
  • 1 โ‰ค connections.length โ‰ค min(n*(n-1)/2, 105)
  • connections[i].length == 2
  • 0 โ‰ค ai, bi < n
  • ai โ‰  bi
  • No repeated connections
Asked in
Google 45 Amazon 38 Microsoft 32 Meta 28
42.4K Views
High Frequency
~15 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen