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

Strongly Connected Components

Strongly Connected Components 34 minWhere, on one-way streets, everywhere reaches everywhere

dsa / Strongly Connected Components

Explore each scene at your own pace.

01 · Reachability runs one way

Reachability runs one way

Strongly Connected Components

Definition
A strongly connected component of a directed graph is a maximal group of nodes in which every node can reach every other by following edge directions. Kosaraju's and Tarjan's algorithms find all of them in linear time.
In plain words
On a city of one-way streets, a district is a set of junctions where you can get from any one to any other and back again. Streets that only lead out of a district never let you return, so they mark its edge.

In short: a strongly connected component, or district, is a largest group of junctions where every one can reach every other along one-way streets. Collapse each district to a blob and keep only the streets between them, and you get a condensation with no loop — a dependency graph of districts. Kosaraju's finds every district in two DFS passes: record each junction's finish order, flip every street, then peel districts off in reverse finish order, each search landing in exactly one district because it starts in what is now a sink.

Three district blobs: {dock, fair, gate} connected in a ring, {hill, inn} connected both ways, and {jetty} alone, with one arrow gate to hill and one arrow inn to jetty crossing between blobs

The old town on Meridian's east side is a maze of one-way streets. A driver who starts at the dock can loop round the fair and the gate and come back to the dock. Take the street from the gate up to the hill, though, and you can circle between the hill and the inn all day without ever finding a way back down to the dock.

Dispatch wants to group junctions into districts: groups where every junction can drive to every other and back. A van assigned to a district can then serve all of it without getting stranded outside. In graph terms, a district is a strongly connected component, or SCC. It's a largest set of nodes in which every node can reach every other along directed edges.

Reachability runs one way

In the graphs section, "connected" was symmetric: if Ana could reach Bo, Bo could reach Ana. One-way streets break that.

What does this print?
ahead = {"lane": ["dock"], "dock": ["fair"], "fair": ["dock"]}

def reach(start):
    seen, todo = {start}, [start]
    while todo:
        j = todo.pop()
        for nxt in ahead[j]:
            if nxt not in seen:
                seen.add(nxt)
                todo.append(nxt)
    return seen

print("dock" in reach("lane"), "lane" in reach("dock"))

So "same district" means "reachable both ways". Testing that for every pair of junctions takes one search per junction, O(V × (V + E)), and it gets slow fast. Two classic algorithms find every district in linear time: Kosaraju's, which is easier to understand, and Tarjan's, which does it in a single pass.

Scene 1 of 10
Project context and further resources
You're building a piece ofMeridian — the network model
This piece — districts(): Groups junctions into districts where, on one-way streets, everywhere reaches everywhere.
Scenario The old town is a maze of one-way streets. Some blocks you can drive around freely; others you can drive into but never back out of. Meridian's dispatch groups junctions into districts where every junction reaches every other, so a van assigned to a district never gets stranded outside it.
districts.py
Call the function you wrote — just like your app's frontend would. Edit the inputs and hit Call.
districts(junctions, streets) → listEvery strongly connected district, each sorted, the list 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.