Algo-Sim › Advanced Graphs
Critical Connections in a Network
Hard · Advanced Graphs · step through it in Java, Python and C++ on your own input
Find every edge whose removal disconnects the graph (a bridge) in one DFS, with Tarjan’s discovery times and low-links.
Problem
There are n servers 0 … n − 1 joined by undirected connections, and every server can reach every other. A connection is critical if removing it leaves some server unable to reach another. Return all critical connections in any order.
Constraints
- 2 ≤ n ≤ 10⁵
- n − 1 ≤ connections.length ≤ 10⁵
- No repeated connections or self-loops
- The network is connected
Edge cases to test
- A tree: every edge is a bridge
- A single cycle: no bridges
- Parallel edges (only if the input allows them)
Follow-up questions
- What does low[v] mean?
- The smallest discovery time reachable from v’s subtree using one back edge. Edge (u, v) is a bridge when low[v] > disc[u].
- How do you find articulation points?
- The same DFS with
low[v] ≥ disc[u]for a non-root u, and a root with two or more DFS children.
Solution
Java
List<List<Integer>> bridges = new ArrayList<>();
List<List<Integer>> adj = new ArrayList<>();
int[] disc, low;
int time = 0;
public List<List<Integer>> criticalConnections(int n, List<List<Integer>> connections) {
for (int i = 0; i < n; i++) adj.add(new ArrayList<>());
for (List<Integer> e : connections) {
adj.get(e.get(0)).add(e.get(1));
adj.get(e.get(1)).add(e.get(0));
}
disc = new int[n];
low = new int[n];
Arrays.fill(disc, -1);
dfs(0, -1);
return bridges;
}
private void dfs(int u, int parent) {
disc[u] = low[u] = time++;
for (int v : adj.get(u)) {
if (v == parent) continue;
if (disc[v] == -1) {
dfs(v, u);
low[u] = Math.min(low[u], low[v]);
if (low[v] > disc[u]) bridges.add(Arrays.asList(u, v));
} else {
low[u] = Math.min(low[u], disc[v]);
}
}
}Python
def critical_connections(n, connections):
adj = [[] for _ in range(n)]
for u, v in connections:
adj[u].append(v)
adj[v].append(u)
disc = [-1] * n
low = [0] * n
bridges = []
time = 0
def dfs(u, parent):
nonlocal time
disc[u] = low[u] = time
time += 1
for v in adj[u]:
if v == parent:
continue
if disc[v] == -1:
dfs(v, u)
low[u] = min(low[u], low[v])
if low[v] > disc[u]:
bridges.append([u, v])
else:
low[u] = min(low[u], disc[v])
dfs(0, -1)
return bridgesC++
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
vector<vector<int>> bridges = {};
vector<vector<int>> adj = {};
vector<int> disc, low;
int time = 0;
vector<vector<int>> criticalConnections(int n, vector<vector<int>> &connections) {
for (int i = 0; i < n; i++)
adj.push_back({});
for (vector<int> &e : connections) {
adj[e[0]].push_back(e[1]);
adj[e[1]].push_back(e[0]);
}
disc = vector<int>(n);
low = vector<int>(n);
fill(disc.begin(), disc.end(), -1);
dfs(0, -1);
return bridges;
}
void dfs(int u, int parent) {
disc[u] = low[u] = time++;
for (int v : adj[u]) {
if (v == parent)
continue;
if (disc[v] == -1) {
dfs(v, u);
low[u] = std::min(low[u], low[v]);
if (low[v] > disc[u])
bridges.push_back({u, v});
} else {
low[u] = std::min(low[u], disc[v]);
}
}
}
};
More Advanced Graphs problems
- Network Delay Time (Medium)
- Evaluate Division (Medium)
- Course Schedule II (Medium)
- Alien Dictionary (Hard)
- Redundant Connection (Medium)
- Number of Connected Components (Union-Find) (Medium)
- Accounts Merge (Medium)
- Minimum Spanning Tree (Kruskal) (Medium)
- Minimum Spanning Tree (Prim) (Medium)
- Min Cost to Connect All Points (Medium)
- Cheapest Flights Within K Stops (Medium)
- Bellman-Ford (negative weights) (Medium)
- Find the City (Floyd-Warshall) (Medium)
- Articulation Points (Cut Vertices) (Hard)
- Strongly Connected Components (Hard)
- Reconstruct Itinerary (Hard)
- Minimum Height Trees (Medium)