Critical Connections in a Network
Pattern: Bridges and articulation points
A bridge is an edge on no cycle. One DFS records each vertex's visit time tin and low, the earliest visit time its subtree can reach with one back edge. A tree edge u → v is a bridge when low[v] > tin[u]: nothing under v can get back to u or above.
Solve on LeetCodeSolve on GeeksforGeeks
The problem
Return every connection whose removal disconnects some servers, as [smaller, larger], sorted. Vertices are numbered from 0 and each edge is given as [u, v].
Think first
- Which edges can never be bridges?
- What does low[v] tell you?
- Why skip the edge back to the parent?
Hints
Brute force, and why it fails
Remove each edge and check connectivity: O(E × (V + E)).
The key insight
One DFS with tin and low finds every bridge, because a bridge is exactly where a subtree cannot climb back.
Dry run
| tin | 0 1 2 |
|---|
n = 4, connections 0-1, 1-2, 2-0, 1-3. DFS from 0: tin 0, 1, 2 for 0, 1, 2.
Step 1 of 3
The plan
dfs(u, parent): tin = low = timer++
for v: skip parent; if new: dfs; low[u] = min(low[u], low[v]); bridge if low[v] > tin[u]
else low[u] = min(low[u], tin[v])
Complexity
- Time
O(V + E)- Space
O(V + E)
One DFS.
Edge cases
- A tree: every edge is a bridge.
- A cycle: none.
- Several pieces.
I've built my logic, show the code
Java
import java.util.*;
class Solution {
private int timer;
private int[] tin, low;
private List<List<Integer>> adj, out;
public List<List<Integer>> criticalConnections(int n, int[][] connections) {
adj = new ArrayList<>();
for (int i = 0; i < n; i++) adj.add(new ArrayList<>());
for (int[] c : connections) { adj.get(c[0]).add(c[1]); adj.get(c[1]).add(c[0]); }
tin = new int[n];
low = new int[n];
Arrays.fill(tin, -1);
out = new ArrayList<>();
timer = 0;
for (int i = 0; i < n; i++) if (tin[i] == -1) dfs(i, -1);
out.sort((a, b) -> a.get(0) != b.get(0) ? a.get(0) - b.get(0) : a.get(1) - b.get(1));
return out;
}
private void dfs(int u, int parent) {
tin[u] = low[u] = timer++; // low: the earliest vertex reachable from u's subtree by one back edge
for (int v : adj.get(u)) {
if (v == parent) continue;
if (tin[v] == -1) {
dfs(v, u);
low[u] = Math.min(low[u], low[v]);
if (low[v] > tin[u]) out.add(Arrays.asList(Math.min(u, v), Math.max(u, v))); // v's side cannot get back past u
} else low[u] = Math.min(low[u], tin[v]);
}
}
}C++
class Solution {
public:
int timer;
vector<int> tin, low;
vector<vector<int>> adj, out;
vector<vector<int>> criticalConnections(int n, vector<vector<int>>& connections) {
adj.assign(n, vector<int>());
for (const vector<int>& c : connections) { adj[c[0]].push_back(c[1]); adj[c[1]].push_back(c[0]); }
tin.assign(n, -1);
low.assign(n, 0);
out.clear();
timer = 0;
for (int i = 0; i < n; i++) if (tin[i] == -1) dfs(i, -1);
sort(out.begin(), out.end());
return out;
}
void dfs(int u, int parent) {
tin[u] = low[u] = timer++; // low: the earliest vertex reachable from u's subtree by one back edge
for (int v : adj[u]) {
if (v == parent) continue;
if (tin[v] == -1) {
dfs(v, u);
low[u] = min(low[u], low[v]);
if (low[v] > tin[u]) out.push_back(vector<int>{min(u, v), max(u, v)}); // v's side cannot get back past u
} else low[u] = min(low[u], tin[v]);
}
}
};Python
class Solution:
def criticalConnections(self, n: int, connections: list[list[int]]) -> list[list[int]]:
adj = [[] for _ in range(n)]
for u, v in connections:
adj[u].append(v)
adj[v].append(u)
tin, low, out, timer = [-1] * n, [0] * n, [], 0
def dfs(u, parent): # low: the earliest vertex reachable from u's subtree by one back edge
nonlocal timer
tin[u] = low[u] = timer
timer += 1
for v in adj[u]:
if v == parent:
continue
if tin[v] == -1:
dfs(v, u)
low[u] = min(low[u], low[v])
if low[v] > tin[u]:
out.append([min(u, v), max(u, v)]) # v's side cannot get back past u
else:
low[u] = min(low[u], tin[v])
for i in range(n):
if tin[i] == -1:
dfs(i, -1)
return sorted(out)Common mistakes
- Using
>=(that is the articulation point test). - Treating the parent edge as a back edge.