Skip to the workspace
Pebellslearn by building
Graph Structure & Decomposition  ›  Lesson

Bridges & Articulation Points

Bridges & Articulation Points 32 minRoads and junctions whose loss splits the city

dsa / Bridges & Articulation Points

Explore each scene at your own pace.

01 · The obvious way, and why it's slow

The obvious way, and why it's slow

Bridges & Articulation Points

Definition
A bridge is an edge, and an articulation point is a node, whose removal disconnects a connected undirected graph. One depth-first search finds both by tracking, for each node, the earliest discovery number reachable from its subtree (its low-link).
In plain words
They are the weak points of a network: the one road whose closure cuts a town in two, or the one junction everything passes through. If there is a way round, it is not a weak point.

In short: a bridge is a road whose closure splits the network, and both come out of one DFS and one number per junction, the low-link — the smallest discovery number reachable from a junction's subtree by one back road. A tree road parent–child is a bridge exactly when low[child] > disc[parent]: nothing below child can climb back to parent or above. Junctions use the same idea with >= instead of >, since a back road landing exactly on a junction doesn't help once that junction itself is gone.

Two triangles, central-harbor-mill and north-quarry-yard, joined only by the road mill-north, with disc/low badges 0/0, 1/1, 2/0, 3/3, 4/3, 5/3 and mill-north marked red as the one bridge

Roads close. A burst water main, a parade, a resurfacing crew: every week some of Meridian's roads are shut for a day. Usually the vans just go another way. But the road from the mill up to the north side is the only way up there, and the day it closed, three depots' worth of parcels sat undelivered while the planners worked out what had happened.

This lesson finds those roads ahead of time. A road whose closure disconnects the network is a bridge. A junction whose closure disconnects it is an articulation point, also called a cut vertex. Both come from one depth-first search and one number per junction, the low-link.

The obvious way, and why it's slow

The definition gives an algorithm straight away: close each road in turn and check whether the network is still in one piece.

What does this print?
roads = [("a", "b"), ("b", "c"), ("c", "a"), ("c", "d"), ("d", "e")]

def pieces(kept):
    parent = {j: j for j in "abcde"}
    def find(x):
        while parent[x] != x:
            x = parent[x]
        return x
    for a, b in kept:
        parent[find(a)] = find(b)
    return len({find(j) for j in parent})

print(sum(pieces([r for r in roads if r != cut]) > 1 for cut in roads))

It's correct, but every closure means a full pass over the network: O(E × (V + E)). Street networks have about 1.5 roads per junction, so for n junctions one connectivity check costs about 2.5n steps, and closing every road in turn about 3.75n². That passes ten million steps at about 1,600 junctions, which is a small town. A real city has hundreds of thousands, where closing each road costs hundreds of billions of steps. The low-link method below does it in one pass of about 2.5n.

Scene 1 of 11
Project context and further resources
You're building a piece ofMeridian — the network model
This piece — critical_roads(): Lists the roads whose closure would split the city in two.
Scenario When a Meridian road closes for repairs, most of the time vans just take another way. But a few roads are the only link to a whole neighbourhood, and closing one strands every van on the far side. The critical-roads report lists them, so planners know which closures need a detour plan before the cones go out.
critical_roads.py
Call the function you wrote — just like your app's frontend would. Edit the inputs and hit Call.
critical_roads(junctions, roads) → listEvery road whose closure disconnects the network, each [a, b] with a < b, sorted.
try:
returned
Press Run to execute your code.
Press Check to run the tests for this lesson.
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.