1192. Critical Connections in a Network
Asked at 10Amazon41Microsoft28Bloomberg22Google20Meta5.0InMobi79Akuna Capital62ServiceNow55Flipkart52Walmart Labs43
Problem
Servers 0…n − 1 are joined by undirected connections, and the network is connected. A connection is critical if removing it leaves some server unable to reach another. Return all critical connections, in any order.
Example: n = 4, connections = [[0,1],[1,2],[2,0],[1,3]] → [[1,3]]
Constraints: n ≤ 10⁵.
Tarjan's bridge-finding: DFS assigns discovery time tin[u]; low[u] = smallest tin reachable from u's subtree using at most one back edge. Tree edge (u, v) is a bridge iff low[v] > tin[u] (v's subtree can't reach u or above without it).
0 - 1 - 3 tin: 0:0 1:1 2:2 3:3
\ / low: 0:0 1:0 2:0 3:3
2 low[3]=3 > tin[1]=1 -> bridge (1,3)
Complexity: O(V + E).
class Solution:
def criticalConnections(self, n: int, connections: List[List[int]]) -> List[List[int]]:
g = defaultdict(list)
for a, b in connections:
g[a].append(b)
g[b].append(a)
tin, low, res, timer = [-1] * n, [0] * n, [], [0]
def dfs(u, parent):
tin[u] = low[u] = timer[0]
timer[0] += 1
for v in g[u]:
if v == parent:
continue
if tin[v] == -1:
dfs(v, u)
low[u] = min(low[u], low[v])
if low[v] > tin[u]:
res.append([u, v])
else:
low[u] = min(low[u], tin[v])
dfs(0, -1)
return res
assert [sorted(e) for e in Solution().criticalConnections(4, [[0, 1], [1, 2], [2, 0], [1, 3]])] == [[1, 3]]
assert sorted(sorted(e) for e in Solution().criticalConnections(2, [[0, 1]])) == [[0, 1]]
assert sorted(sorted(e) for e in Solution().criticalConnections(6, [[0, 1], [1, 2], [2, 0], [1, 3], [3, 4], [4, 5], [5, 3]])) == [[1, 3]]
assert sorted(sorted(e) for e in Solution().criticalConnections(4, [[0, 1], [1, 2], [2, 3]])) == [[0, 1], [1, 2], [2, 3]]
class Solution {
private int timer = 0;
private int[] tin, low;
private List<List<Integer>> g;
private final List<List<Integer>> res = new ArrayList<>();
public List<List<Integer>> criticalConnections(int n, List<List<Integer>> connections) {
g = new ArrayList<>();
for (int i = 0; i < n; i++) g.add(new ArrayList<>());
for (List<Integer> e : connections) { g.get(e.get(0)).add(e.get(1)); g.get(e.get(1)).add(e.get(0)); }
tin = new int[n];
low = new int[n];
Arrays.fill(tin, -1);
dfs(0, -1);
return res;
}
private void dfs(int u, int parent) {
tin[u] = low[u] = timer++;
for (int v : g.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]) res.add(List.of(u, v));
} else low[u] = Math.min(low[u], tin[v]);
}
}
}
func criticalConnections(n int, connections [][]int) [][]int {
g := make([][]int, n)
for _, c := range connections {
g[c[0]] = append(g[c[0]], c[1])
g[c[1]] = append(g[c[1]], c[0])
}
tin, low := make([]int, n), make([]int, n)
for i := range tin {
tin[i] = -1
}
res := [][]int{}
timer := 0
var dfs func(u, parent int)
dfs = func(u, parent int) {
tin[u], low[u] = timer, timer
timer++
for _, v := range g[u] {
if v == parent {
continue
}
if tin[v] == -1 {
dfs(v, u)
low[u] = min(low[u], low[v])
if low[v] > tin[u] { // v's subtree cannot reach u or above without this edge
res = append(res, []int{u, v})
}
} else {
low[u] = min(low[u], tin[v])
}
}
}
dfs(0, -1)
return res
}