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

Edge cases to test

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 bridges

C++

#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