Critical Connections in a Network - Problem

Imagine you're a network administrator managing a critical server infrastructure where n servers (numbered 0 to n-1) are connected by bidirectional links. Your network is represented by connections[i] = [a_i, b_i], meaning servers a_i and b_i can communicate directly.

Currently, every server can reach every other server either directly or through intermediate servers. However, some connections are more critical than others!

A critical connection (bridge) is a link that, if removed, would split the network into disconnected parts, making some servers unreachable from others.

Your mission: Find all critical connections that keep the network fully connected. Without these bridges, your network would fragment!

Example: If servers 0-1-2 form a line, then both connections [0,1] and [1,2] are critical because removing either would disconnect the network.

Input & Output

example_1.py โ€” Python
$ Input: n = 4, connections = [[0,1],[1,2],[2,3],[1,3]]
โ€บ Output: [[0,1]]
๐Ÿ’ก Note: The network forms a cycle between nodes 1,2,3 with node 0 connected only through node 1. Removing edge [0,1] would isolate node 0 from the rest of the network, making it the only critical connection.
example_2.py โ€” Python
$ Input: n = 2, connections = [[0,1]]
โ€บ Output: [[0,1]]
๐Ÿ’ก Note: With only two nodes and one connection, removing the single edge [0,1] would completely disconnect the network. Therefore, it's a critical connection.
example_3.py โ€” Python
$ Input: n = 3, connections = [[0,1],[1,2]]
โ€บ Output: [[0,1],[1,2]]
๐Ÿ’ก Note: This forms a linear chain: 0-1-2. Both edges are critical because removing either [0,1] or [1,2] would split the network into disconnected components.

Visualization

Tap to expand
City ADiscovery: 0Low: 0City BDiscovery: 1Low: 1City CDiscovery: 2Low: 1City DDiscovery: 3Low: 1CRITICAL BRIDGEOnly route to City A!Alternative route๐ŸŽฏ Key Insight: Bridge Detectionโ€ข Discovery time: When we first visit a city during our journeyโ€ข Low value: Earliest city reachable from current city (including back routes)โ€ข Critical bridge: low[neighbor] > discovery[current] means no alternative path exists!
Understanding the Visualization
1
Start Journey
Begin DFS traversal from any starting point, recording when we first discover each location
2
Track Reachability
For each location, track the earliest discoverable location we can reach through any path
3
Identify Bottlenecks
A bridge is critical if it's the only way to reach newer territory - no alternative routes exist
4
Mark Critical Routes
Edges where low[neighbor] > discovery[current] are bottlenecks that would isolate parts of the network
Key Takeaway
๐ŸŽฏ Key Insight: Tarjan's algorithm elegantly identifies bridges by detecting when there's no "back door" route around an edge - if low[neighbor] > discovery[current], that edge is the sole connection between network components.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(E ร— (V + E))

For each of E edges, we perform a DFS/BFS taking O(V + E) time

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

Space for adjacency list representation and recursion stack

n
2n
โœ“ Linear Space

Constraints

  • 2 โ‰ค n โ‰ค 105
  • n - 1 โ‰ค connections.length โ‰ค 105
  • 0 โ‰ค ai, bi โ‰ค n - 1
  • ai โ‰  bi
  • There are no repeated connections
  • The given graph is connected
Asked in
Amazon 45 Google 38 Microsoft 32 Meta 28
89.5K Views
High Frequency
~35 min Avg. Time
2.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