On this page
You cannot study differentiation before limits, or limits before functions. Any list of tasks with 'this before that' rules is a directed graph: each task is a vertex, and an arrow from u to v means u comes first.
What is a topological sort of a directed acyclic graph?
A topological sort is a linear ordering of a graph's vertices in which, for every edge from u to v, u comes before v (Wikipedia: Topological sorting). A directed graph has a topological order if and only if it is a directed acyclic graph, or DAG, meaning it has no directed cycle (Sedgewick and Wayne, Algorithms, section 4.2).
Our teaching example (not any board's official sequence) has six topics and six prerequisite arrows:
Sets (S) → Functions (F)
Algebra (A) → Functions (F)
Algebra (A) → Trigonometry (T)
Functions (F) → Limits (L)
Trigonometry (T) → Limits (L)
Limits (L) → Differentiation (D)
How does Kahn's algorithm work on a course-prerequisite graph?
A. B. Kahn published the method in 1962 as Topological sorting of large networks in Communications of the ACM. His abstract says a PERT network of 30,000 activities can be ordered in less than one hour of machine time on the 7090 computer.
Count each vertex's incoming arrows (its in-degree), then repeatedly remove a vertex with count zero, lowering the counts of the vertices it points to.
In-degrees: S 0, A 0, F 2, T 1, L 2, D 1. Ready: A and S.
Take A (we break ties alphabetically). F drops to 1, T drops to 0. Ready: S and T.
Take S. F drops to 0. Ready: F and T.
Take F. L drops to 1. Ready: T.
Take T. L drops to 0. Ready: L.
Take L, then D.
The output is A, S, F, T, L, D. Check it: every arrow points from left to right. Each vertex and each edge is handled once, which is why the usual algorithms run in time O(|V| + |E|) (Wikipedia: Topological sorting).
If your in-degree bookkeeping slips, the Fermi Problem Library has a step-by-step solver that explains the reasoning.
How does the DFS finishing-time method find an order?
Depth-first search goes as deep as it can; a vertex finishes when everything it points to is done. In a DAG, the reverse of the finishing order (reverse postorder) is a topological sort (Sedgewick and Wayne, section 4.2). It seems to have first appeared in print in a 1976 paper by Robert Tarjan (Wikipedia: Topological sorting).
On our graph, visiting neighbours alphabetically:
Start at A and go to F, then L, then D. D points nowhere, so D finishes first, then L, then F.
Back at A, visit T. Its only neighbour, L, is done, so T finishes, then A.
S is still unvisited. Its neighbour F is done, so S finishes last.
Finishing order: D, L, F, T, A, S. Reversed: S, A, T, F, L, D. That differs from Kahn's answer, and both are correct. It works because u finishes only after everything it points to, so reversing puts u first.
Why does a cycle make topological sorting impossible?
Suppose someone adds an arrow D → A. Now A → T → L → D → A is a cycle. Each vertex on it must come before the next, so A would have to come before itself. No order can do that.
Kahn's algorithm detects this. Now only S is ready; removing it drops F to 1, and nothing else is ever ready while five vertices remain. If the algorithm ends with edges left over, the graph must contain a cycle (Wikipedia: Topological sorting).
How many valid orders can a DAG have?
The order is unique exactly when it forms a Hamiltonian path, a chain of edges through every vertex; otherwise there are at least two (Wikipedia: Topological sorting). In our graph, L and D always come last, since everything else leads into L. The first four places must keep S before F, A before F and A before T:
A, S, F, T
A, S, T, F
A, T, S, F
S, A, F, T
S, A, T, F
So there are exactly 5 valid orders. Kahn's run gave the first and DFS gave the last.
Listing fails on large graphs. Graham Brightwell and Peter Winkler showed in 1991 that counting these orders (the linear extensions of a partial order) is #P-complete, so no fast exact method is known in general (Counting linear extensions is #P-complete). Special shapes do have formulas: the hook length formula counts Young tableaux, orderings of a grid-shaped partial order. For how wide a partial order is, see Dilworth's theorem on chains and antichains.
How can you order your own revision this way?
Draw an arrow from each chapter to every chapter that truly needs it.
Start with chapters that need nothing, removing each as you finish it.
Stuck with chapters left? Find the loop: often one arrow is 'helpful', not 'required'. Delete it.
When several chapters are ready, choose: weakest first, or the one with most arrows out.
To revise DAGs, partial orders and depth-first search as connected ideas, the Fermi Concept Library covers every concept in physics, chemistry, biology, mathematics and computing.



