Algorithms / Topological sort

Topological sort

How to line up tasks so that everything a task depends on comes before it, shown on six courses with prerequisites: by counting arrows, and by depth-first search.

Intermediate7 steps~6 min
Course — a node: one thing to schedule
Arrow — an edge: “this one comes before that one”
Counter — in-degree: prerequisites still missing
Ready — counter is 0, can be placed next
// THE SHORT VERSION

In 30 seconds

Some things must happen before others: a course before the one that builds on it, a library before the program that uses it. Draw each rule as an arrow and you get a graph.

A topological sort lists every item so that all arrows point forward. Kahn’s algorithm repeatedly takes an item with no unmet prerequisites; a depth-first search gets there by writing items down on the way back and reversing.

If the rules loop back on themselves, no such order exists, and the algorithm notices.

Topological sort, step by step

STEPS01 / 07

A graph usually has several valid orders; which one you get depends on which ready node is taken first. Real depth-first versions also mark nodes “in progress” to detect cycles.

STEP 01 / 07 Things that must wait
THE MATH
arrow A → B: A comes before B 6 courses, 6 arrows
  1. Step 1: Things that must wait. Six courses, and some need others first: ALGO needs both MATH and PROG, WEB needs ALGO and DATA. Each arrow means “before”. We want to put all six in one line so that no course comes before something it depends on. That line is a topological order.
  2. Step 2: Count incoming arrows. Give every course a counter: the number of arrows pointing at it, called its in-degree. It says how many prerequisites are still missing. MATH and PROG read 0, so nothing stops them: they are ready. This is the start of Kahn’s algorithm.
  3. Step 3: Take one, lower the rest. Move a ready course into the order and erase its outgoing arrows, lowering the counter of each course they pointed at. Taking MATH drops ALGO from 2 to 1. Taking PROG drops ALGO to 0 and DATA to 0, so those two become ready.
  4. Step 4: Repeat until empty. Keep taking ready courses: ALGO, DATA, then WEB, then PROJ. Laid out in a line, every arrow now points to the right, so every course comes after its prerequisites. Each course and each arrow was handled exactly once, so the time grows with nodes plus arrows.
  5. Step 5: The depth-first version. A second method: follow arrows as deep as possible and write a course down only when everything after it is written. Starting at MATH we reach PROJ first, so PROJ is written first; PROG is written last. Reverse that list and prerequisites come first. It is a different order, and just as valid.
  6. Step 6: A cycle has no order. Suppose someone adds a rule that ALGO needs PROJ. Now ALGO waits for PROJ, PROJ for WEB, and WEB for ALGO: a cycle. MATH, PROG and DATA come out, then no counter is 0 and the process stops with three courses left. Fewer courses placed than exist is exactly how Kahn’s algorithm detects a cycle.
  7. Step 7: Where it is used. Anything with “this before that” rules is a job for topological sort. Build tools compile a library before the program that uses it; package managers install dependencies first; spreadsheets recalculate a cell after the cells it reads. It works only on a directed graph with no cycles, a DAG.
VALUES SO FAR
graph6 nodes, 6 arcsinput
ready····count 0
ready····count 0
Kahn 1-3····order
Kahn 4-6····order
DFS 1-3····order
DFS 4-6····order
cycle····error
time····cost

Spotted a mistake on this page?