Skip to content
DeepForgeby svx
All problems
HardGraph Algorithmsgraph-065

Bridge-Connected Components Count

Problem

Count the 2-edge-connected components of an undirected graph with n nodes: the connected components left after deleting every bridge.

Find bridges with a Tarjan DFS using discovery times and low-link values, then union all non-bridge edges with union-find and count the resulting components. edges is a list of [u, v] pairs.

Hint

Two nodes are 2-edge-connected exactly when there are two edge-disjoint paths between them.

Test cases

case 1
in: [3,[[0,1],[1,2]]]
expected: 3
case 2
in: [4,[[0,1],[1,2],[2,0],[2,3]]]
expected: 2
case 3
in: [6,[[0,1],[1,2],[2,0],[2,3],[3,4],[4,5],[5,3]]]
expected: 2
case 4
in: [4,[[0,1],[0,2],[0,3],[1,2],[1,3],[2,3]]]
expected: 1
case 5
in: [2,[]]
expected: 2
Starter codePython
def bridge_connected_components(n, edges):
    # Your code here
    pass

5 test cases · run your code against them in the editor