Bridges and articulation points (Tarjan)

Video lessonComing soon

1 One closed road splits the district

A district has six villages joined by roads. Villages 0, 1 and 2 sit on one loop of roads, and 3, 4 and 5 on another. The road 1 – 4 joins the two loops.

012345
Two loops of roads, joined by the road 1 – 4

If the road 0 – 2 closes, traffic goes round through 1, and nobody is cut off. If the road 1 – 4 closes, the district splits in two. That road is a bridge.

A junction can split the district too. If junction 1 closes, 0 and 2 can no longer reach 3, 4 and 5. Such a junction is an articulation point, or cut vertex. Here, 1 and 4 are articulation points.

The direct check closes each road in turn and runs a BFS. That is E searches of O(V + E) each. At 10⁵ junctions and 10⁵ roads, it is around 10¹⁰ steps. At about 10⁸ steps a second, it is far past the time limit, so it gets Time Limit Exceeded (TLE). Tarjan's method finds them all in a single DFS.

2 The DFS tree and its back roads

We run a DFS from junction 0. Each road it takes to a new junction is a tree edge, and these roads form the DFS tree. Every other road is a back edge.

012345
Bold: tree edges. Thin: back edges
021354
The DFS tree, hung from 0

Both back edges, 2 – 0 and 5 – 4, join a junction to its grandparent. That is no accident. In an undirected DFS, every back edge joins a junction to one of its ancestors, never to another branch.

The proof is short. Take a road between x and y, where the search reaches x first. Before the call for x ends, it tries the road to y. If y is still new, the search walks to y from x. If not, y was reached after x, while the call for x was running. Either way, y sits below x.

So a subtree has only two ways out: the tree edge up to its parent, and back edges up to its ancestors.

3 How high can a subtree climb?

We measure height in the tree with two numbers per junction:

  • tin[v], the discovery time: when the search first reaches v, counting from 1. An ancestor always has a smaller tin, so smaller is higher up.
  • low[v]: the smallest tin that v's subtree reaches, going down tree edges and then up at most one back edge. It answers one question: how high can this subtree climb without the road to its parent?

Take a tree edge from u down to its child v. If the subtree under v can climb to u or higher another way, closing u – v cuts nobody off. If it cannot, u – v is its only way out, so it is a bridge:

the tree edge u – v is a bridge when low[v] > tin[u]

A back edge is never a bridge. It closes a loop with the tree path between its ends.

The search calculates low with three rules:

  1. When u is first reached, low[u] = tin[u].
  2. For a back edge from u to w, low[u] = min(low[u], tin[w]).
  3. After a child v returns, low[u] = min(low[u], low[v]), because u's subtree holds v's.

The road down from the parent is not a way round, so it never counts as a back edge.

4 Watching tin and low fill in

Here is the search on our district. Above each junction is tin/low, or ? before the search reaches it. Bold roads are tree edges, filled junctions are the calls still running, and a ring marks where a back edge lands.

Tarjan's search on the district
01/11?2?3?4?5?
Above each junction: tin/low
dfs(0)top →
Start at 0: tin 1, low 1

In step 7, 4 also looks at its road to 5. Junction 5 has tin 6, which is not smaller than 4, so rule 2 changes nothing.

Each time a child returns, the parent takes its low and tests the road between them:

Child returns tolow[child]tin[parent]low[child] > tin[parent]?low[parent] after
2 to 112no1
5 to 345no4
3 to 444no4
4 to 142yes: 1 – 4 is a bridge1
1 to 011no1

At the end, the left loop has low 1 everywhere, and the right loop has low 4. Each loop climbs to its own top junction and no higher. Only the subtree under 4 cannot climb past its parent, so only 1 – 4 is a bridge.

5 The code, and parallel roads

LeetCode's Critical Connections in a Network asks for every bridge, so the function is criticalConnections. Each road's id is its index in connections, and each junction's list holds pairs of a neighbour and a road id. A tin of 0 marks a junction not reached yet.

On our district it returns [[1,4]], and on LeetCode's first example, [[1,3]].

Skip the parent road, not the parent junction. Two roads can join the same pair of junctions. With a second road between 1 and 4, closing either one cuts nobody off. Skipping the parent junction would skip both roads back to 1, so the code would still report 1 – 4. Skipping only the road we came down lets the second road count as a back edge. Then low[4] becomes 2, and the answer is empty.

C++

State as members. adj, tin, low and timer live in the class, so the helper does not pass five arguments down every call. assign resets them on each call to criticalConnections.

An int[] pair. Each list entry is new int[] {neighbour, edgeId}. Arrays.asList(u, v) builds each bridge as a List<Integer>.

About 1,000 calls deep. Python stops a recursion at about 1,000 nested calls with a RecursionError, and a chain of 10⁵ junctions goes 10⁵ deep. Raise the limit with sys.setrecursionlimit, or run the search with a stack of our own.

A closure. dfs is defined inside the function, so it reads and updates tin, low and timer without taking them as arguments.

Declare the closure first. dfs calls itself, so it is declared with var before it is assigned. The built-in min needs Go 1.21 or later.

A struct for the search. The recursive helper needs to change tin, low, timer and bridges, so they live in Search and dfs takes &mut self. The parent road is an Option<usize>, with None for a root.

Named tuples. Each list entry is (int to, int edgeId), so the code reads next.to and next.edgeId instead of Item1 and Item2.

Cost. Each junction is entered once, and each road is looked at twice, once from each end. So the search is O(V + E) time, a few hundred thousand steps at 10⁵ junctions and roads. The lists take O(V + E) space, and tin, low and the call stack O(V). On a long chain, the recursion goes V calls deep and can end in a Runtime Error. The same search can run with a stack of our own.

6 Articulation points: one sign, and the root

A junction u, other than the root, is an articulation point when some child v has

low[v] ≥ tin[u]

The bridge test had > here. When a road closes, a subtree that climbs back to u is fine, because u is still open. When junction u closes, climbing to u is no help. The subtree must reach above it.

Junction 4 shows the difference. Its child 3 has low[3] = 4, equal to tin[4], because the back edge 5 – 4 lands on 4 itself. So the road 4 – 3 is not a bridge, since 4 > 4 is false. But junction 4 is a cut point, since 4 ≥ 4 is true.

The root is special. It has the smallest tin, so every child passes the ≥ test, and every root would count as a cut point. Our root 0 is not one: closing it leaves 1 to 5 joined. Back edges only go to ancestors, so two subtrees of the root can only meet through the root. So the root is an articulation point exactly when it has two or more children in the DFS tree.

Junction 0 has only one child. The search reached 2 through 1, before 0 tried its own road to 2. A search started at 1 gives 1 two children, 0 and 4, and 1 is a cut point.

The changes to dfs:

dfs(u, parentEdge, isRoot):
    timer += 1; tin[u] = timer; low[u] = timer
    children = 0
    for each (v, edgeId) in adj[u]:
        if edgeId == parentEdge: skip
        if tin[v] == 0:
            children += 1
            dfs(v, edgeId, false)
            low[u] = min(low[u], low[v])
            if not isRoot and low[v] >= tin[u]:
                isCut[u] = true
        else:
            low[u] = min(low[u], tin[v])
    if isRoot and children >= 2:
        isCut[u] = true

A junction can pass the test through several children, so isCut is a flag per junction, not a list.

7 Mistakes and edge cases

  • ≥ in the bridge test. That is the articulation test. On our district it reports 4 – 3, 1 – 4 and 0 – 1, when only 1 – 4 is a bridge.
  • Counting the parent road as a back edge. Then every child's low is at most its parent's tin, and no road is ever a bridge. Our district comes out empty.
  • Forgetting the root rule. The articulation test then reports 0 as well as 1 and 4.
  • Skipping the parent junction, not the parent road. With two roads between 1 and 4, it still reports 1 – 4.
  • Searching only from junction 0. Add a separate road 6 – 7. A search from 0 never reaches it and misses that bridge. Start a search from every junction whose tin is still 0.
  • A junction with one road. That road is always a bridge, and the test needs no special case. Add a road 5 – 6, and low[6] = 7 > tin[5] = 6.

8 When to look for bridges

The sign is a question about removing one edge or one vertex:

  • Single points of failure. Which cables or roads would split a network if they failed? Those are the bridges.
  • Critical vertices. Which servers or junctions would cut others off if they closed? Those are the articulation points.
  • Remove and search again. When a brute force removes each edge or vertex and runs a BFS, one Tarjan search answers them all.

The same tin and low idea, on directed graphs, finds strongly connected components, as their lesson shows.

Before removing edges one at a time and searching again, ask how high each subtree of one DFS can climb. Every bridge is a subtree that cannot climb past its parent.

Practice it

Finished this lesson?

Mark it read and it is ticked on the roadmap and in the list.

Article

Send feedback

Thank you for helping. We read every report and fix what is wrong.

How do you feel about oneby7?

Sign in to oneby7

Keep your progress, your streak and your code, on any device.

New here? This creates your account too.