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

Ordering What Depends on What

Topological Sort 28 minKahn's algorithm: emit what nothing is waiting on

dsa / Ordering What Depends on What

Explore each scene at your own pace.

01 · Needs are directed edges

Needs are directed edges

Ordering What Depends on What

Definition
A topological order of a directed graph lists every node so that each edge points from an earlier node to a later one; one exists exactly when the graph has no cycle. Kahn's algorithm builds it by repeatedly removing a node with no unmet prerequisites.
In plain words
Think of getting dressed: socks go on before shoes, and trousers before a belt. A topological order is any sequence that respects every 'this first' rule, and if two things each wait on the other, no sequence can exist.

In short: a topological order lists every job so each one's needs come before it. Kahn's algorithm builds one by counting, for each job, how many needs it's still waiting on — its in-degree — and emitting any job whose count reaches zero. Emitting a job lowers its dependants' counts, which can make more jobs ready, so the queue keeps feeding itself until it runs dry. If jobs are left over when it does, they wait on each other in a cycle, and no order exists.

Five build jobs as a graph: survey and permit start with an in-degree badge of 0, grade with 2, pave with 1, signals with 2, with arrows survey to grade, permit to grade, grade to pave, pave to signals and permit to signals

Welcome to Meridian, a city logistics network. It has depots, roads, one-way streets, vans, drivers and jobs, and over the next few sections you will build the software that runs it. This section builds Meridian's model of the city: what the network is made of, which parts hold it together, and where it is fragile. Routing parcels through it comes later. First, the network has to exist.

Meridian is opening a depot in the north of the city, and the build-out is a list of jobs with strings attached. The site has to be surveyed before it can be graded. Grading also needs a permit. Paving waits for grading, and the traffic signals wait for both the paving and the permit. Five jobs, five "this before that" rules, and one question: in what order can the crews work?

Needs are directed edges

Draw each job as a node and each rule [before, after] as an arrow from before to after. The result is a directed graph, and it is the same adjacency-list shape you used for the follow graph in the graphs section, with one extra number per node: how many arrows point into it. That count is the node's in-degree, and here it means "how many jobs am I still waiting for?"

python

An order that puts every before ahead of its after is a topological order. It is not unique: survey and permit have no needs at all, so either can go first. It can also fail to exist, as you'll see shortly. Checking whether a proposed order is valid is easy: every rule's before must sit at an earlier position.

What does this print?
needs = [["survey", "grade"], ["permit", "grade"], ["grade", "pave"]]
order = ["permit", "grade", "survey", "pave"]
position = {task: i for i, task in enumerate(order)}
print(all(position[a] < position[b] for a, b in needs))
Scene 1 of 12
Project context and further resources
You're building a piece ofMeridian — the network model
This piece — build_order(): Orders the city build-out so no job starts before the jobs it depends on.
Scenario Meridian is opening its north depot. Surveying, permits, grading, paving and signals all have to happen, and some can't start until others finish. The build schedule turns that tangle of 'this before that' into a day-by-day plan — and refuses to plan at all if two jobs are waiting on each other.
build_order.py
Call the function you wrote — just like your app's frontend would. Edit the inputs and hit Call.
build_order(tasks, needs) → listThe build-out in a dependency-respecting order, alphabetical among ready tasks, or [] if the needs are circular.
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.