Critical Connections in a Network (LeetCode 1192): Solution and Explanation
A connected network of n servers is linked by undirected connections. A connection is critical if removing it disconnects some servers from the rest. Return all critical connections in any order.
Watch it run step by step
Try your own input and write the code →Intuition
The direct approach removes each edge one at a time and runs a BFS to see if the graph is still connected. With E edges that is O(E × (V + E)), which is far too slow for the 100,000-edge inputs this problem allows.
A critical connection is what graph theory calls a bridge, and the key fact is that an edge is a bridge exactly when it does not lie on any cycle. If there is a cycle through it, traffic can go the other way around the cycle when it is removed. So the job is to detect, in a single DFS, which tree edges have a back route around them.
Tarjan's algorithm does this with two numbers per node. disc[u] is when the DFS first visited u. low[u] is the earliest discovery time that u's subtree can reach using tree edges down and at most one back edge up. After exploring child v, if low[v] > disc[u], nothing in v's subtree can reach u or anything above it except through the edge (u, v), so that edge is a bridge. If low[v] is at most disc[u], some back edge climbs over the edge and it is safe.
Approach: Tarjan's bridge-finding DFS
- Build an adjacency list from the connections.
- Initialize
discandlowto -1 for every node and keep a shared timer. - In
dfs(node, parent), set bothdisc[node]andlow[node]to the current time and advance the timer. - For each neighbor other than the parent: if it is unvisited, recurse, then lower
low[node]tolow[neighbor]and record[node, neighbor]as a bridge whenlow[neighbor] > disc[node]. - If the neighbor was already visited, it is a back edge, so lower
low[node]todisc[neighbor]. - Start the DFS at node 0 and return the collected bridges.
Step-by-step example
n = 4, connections = [[0,1],[1,2],[2,0],[1,3]]
- DFS visits 0, then 1, then 2, giving discovery times 0, 1 and 2.
- At node 2, neighbor 0 is already visited and is not the parent, so
low[2] = min(2, disc[0]) = 0. - Back at node 1,
low[1] = min(1, low[2]) = 0. Sincelow[2] = 0is not greater thandisc[1] = 1, edge (1, 2) is on a cycle. - Node 1 then visits 3, with
disc[3] = 3. Node 3 has only its parent as a neighbor, solow[3] = 3, which is greater thandisc[1] = 1. Edge[1, 3]is a bridge. - Back at node 0,
low[1] = 0is not greater thandisc[0], so (0, 1) is safe. The answer is[[1, 3]].
Code: Python, Java, C++
1class Solution:
2 def criticalConnections(self, n: int, connections: List[List[int]]) -> List[List[int]]:
3 graph = defaultdict(list)
4 for a, b in connections:
5 graph[a].append(b)
6 graph[b].append(a)
7
8 disc = [-1] * n
9 low = [-1] * n
10 bridges = []
11 time = [0]
12
13 def dfs(node, parent):
14 disc[node] = low[node] = time[0]
15 time[0] += 1
16
17 for neighbor in graph[node]:
18 if neighbor == parent:
19 continue
20 if disc[neighbor] == -1:
21 dfs(neighbor, node)
22 low[node] = min(low[node], low[neighbor])
23 if low[neighbor] > disc[node]:
24 bridges.append([node, neighbor])
25 else:
26 low[node] = min(low[node], disc[neighbor])
27
28 dfs(0, -1)
29 return bridges1class Solution {
2 private List<Integer>[] graph;
3 private int[] disc, low;
4 private List<List<Integer>> bridges;
5 private int time;
6
7 public List<List<Integer>> criticalConnections(int n, int[][] connections) {
8 graph = new ArrayList[n];
9 for (int i = 0; i < n; i++) graph[i] = new ArrayList<>();
10 for (int[] conn : connections) {
11 graph[conn[0]].add(conn[1]);
12 graph[conn[1]].add(conn[0]);
13 }
14
15 disc = new int[n];
16 low = new int[n];
17 Arrays.fill(disc, -1);
18 Arrays.fill(low, -1);
19 bridges = new ArrayList<>();
20 time = 0;
21
22 dfs(0, -1);
23 return bridges;
24 }
25
26 private void dfs(int node, int parent) {
27 disc[node] = low[node] = time++;
28
29 for (int neighbor : graph[node]) {
30 if (neighbor == parent) continue;
31 if (disc[neighbor] == -1) {
32 dfs(neighbor, node);
33 low[node] = Math.min(low[node], low[neighbor]);
34 if (low[neighbor] > disc[node]) {
35 bridges.add(Arrays.asList(node, neighbor));
36 }
37 } else {
38 low[node] = Math.min(low[node], disc[neighbor]);
39 }
40 }
41 }
42}1class Solution {
2public:
3 vector<vector<int>> criticalConnections(int n, vector<vector<int>>& connections) {
4 vector<vector<int>> graph(n);
5 for (auto& conn : connections) {
6 graph[conn[0]].push_back(conn[1]);
7 graph[conn[1]].push_back(conn[0]);
8 }
9
10 vector<int> disc(n, -1), low(n, -1);
11 vector<vector<int>> bridges;
12 int time = 0;
13
14 function<void(int, int)> dfs = [&](int node, int parent) {
15 disc[node] = low[node] = time++;
16
17 for (int neighbor : graph[node]) {
18 if (neighbor == parent) continue;
19 if (disc[neighbor] == -1) {
20 dfs(neighbor, node);
21 low[node] = min(low[node], low[neighbor]);
22 if (low[neighbor] > disc[node]) {
23 bridges.push_back({node, neighbor});
24 }
25 } else {
26 low[node] = min(low[node], disc[neighbor]);
27 }
28 }
29 };
30
31 dfs(0, -1);
32 return bridges;
33 }
34};Time and space complexity
O(V + E)
The DFS visits each node once and examines each adjacency entry once, doing constant work per edge.
O(V + E)
The adjacency list stores every edge twice, and the disc and low arrays plus the recursion stack are O(V).
Other approaches
Remove each edge and test connectivity (time O(E × (V + E)), space O(V + E))
For every edge, delete it and run BFS or DFS to see if all nodes remain reachable. Easy to explain but quadratic in practice.
Iterative Tarjan (time O(V + E), space O(V + E))
The same algorithm with an explicit stack to avoid recursion limits. It is the safer choice in Python on deep graphs, at the cost of trickier bookkeeping for when a child finishes.
Common mistakes
- Using
low[neighbor]instead ofdisc[neighbor]for back edges. For bridges both happen to work, butdiscis the standard and avoids confusion with articulation point variants. - Comparing
low[neighbor] >= disc[node]. That is the articulation point test; for bridges the condition must be strictly greater. - Skipping the parent by node value when parallel edges exist. If two connections join the same pair, skipping by edge index is needed, although this problem guarantees no duplicates.
- Hitting Python's recursion limit on a long chain of 100,000 servers. Raise the limit or convert the DFS to an iterative one.
Frequently asked questions
What is a critical connection in a network?
It is a bridge: an edge whose removal splits the graph into more than one connected component. Equivalently, it is an edge that belongs to no cycle.
How does Tarjan's algorithm find bridges?
It records each node's discovery time and the lowest discovery time reachable from its subtree via one back edge. A tree edge from u to child v is a bridge when low[v] > disc[u].
What is the time complexity of critical connections?
O(V + E), since a single DFS touches each node and edge a constant number of times.
What is the difference between bridges and articulation points?
Bridges are edges and use low[v] > disc[u]. Articulation points are nodes and use low[v] >= disc[u], with a special rule for the DFS root.