Critical Connections in a Network

HardDepth-first searchLeetCode 1192 ↗World 6-19
0:00 / 0:00

Find every link whose loss splits a network: one DFS stamps discovery times, carries low links up, and cuts where a child can't reach back.

▼

The problem

LeetCode 1192 (Hard). There are n servers 0 .. n-1 and undirected connections between them (the network is connected, no repeated connections). A connection is critical if removing it leaves some server unable to reach some other server. Return every critical connection, in any order. These are the graph's *bridges*.

Example (LeetCode's first): n = 4, connections = [[0,1],[1,2],[2,0],[1,3]] → [[1,3]] (0, 1, 2 are a loop; 3 hangs off 1).

TRY IT ON LEETCODE ▶

The solution

def critical_connections(n, connections):
    graph = [[] for _ in range(n)]
    for a, b in connections:
        graph[a].append(b); graph[b].append(a)
    disc, low, out = [-1] * n, [0] * n, []
    clock = iter(range(n))
    def dfs(u, parent):
        disc[u] = low[u] = next(clock)
        for v in graph[u]:
            if v == parent:
                continue
            if disc[v] == -1:
                dfs(v, u)
                low[u] = min(low[u], low[v])
                if low[v] > disc[u]:
                    out.append([u, v])
            else:
                low[u] = min(low[u], disc[v])
    dfs(0, -1)
    return out

Transcript

Critical Connections in a Network. n servers are joined by cables. A cable is critical if cutting it leaves some server cut off from the rest. Return every critical cable.

Here, zero, one and two form a loop, and three hangs off one. Cut a loop cable and power flows the long way around. Cut one to three, and three goes dark. That's the only critical cable.

The simple way: cut each cable, then search to see if every server still has power. Two of these eight cuts cause a blackout, but that's a whole search per cable: E times V plus E.

Tarjan's idea uses one depth-first search. Walk from zero, stamping each server with the time you first reach it. The cables you walk form a tree; every other cable is a back edge, a shortcut to a server stamped earlier.

Each server also gets a low: the earliest time its subtree can reach by one back edge, skipping the cable to its parent. Here, two's side reaches zero: a way back, so its low is zero. Without that edge, two's low stays two, later than its parent's one. When a child's low is later than its parent's time, that cable is critical.

Walking the example: zero gets time zero, one gets one, two gets two. Two sees zero by a back edge, so its low drops to zero, and so does one's. Three gets time three, a dead end. Its low, three, is later than one's time: one to three is critical. The loop cables keep low zero: safe.

In code, stamp disc and low on entry. For each neighbor but the parent: if it's new, recurse, pull its low up, and check it against disc. If it's seen, take its time.

Each server is stamped once, and each cable is checked from both ends: O of V plus E time. The lists, stamps and call stack take O of V plus E space.

Stamp the times, carry the lows up, and cut where a low comes too late. That's Critical Connections.