A topological ordering places each prerequisite before the tasks that depend on it. Such an ordering exists for a finite directed graph exactly when the graph has no directed cycle.
Worked example
For a small build, let compiling A precede linking B and let compiling C also precede B. Begin with tasks that have no unmet prerequisites. After removing one task from consideration, decrease the unmet count of its dependents. A queue or another ready-task collection then identifies what may run next. Several valid orders can exist.
What to check
If tasks remain but none is ready, a dependency cycle prevents a complete ordering. Report that condition instead of silently returning a partial schedule as a finished answer. With adjacency lists and indegree counts, the procedure uses O(V + E) time. A topological order provides dependency order, while actual execution scheduling also needs resource limits, durations, and failure behavior.
Continue with the book
Explore the full treatment, worked examples, and exercises in Algorithms Made Simple.
Companion article for Algorithms Made Simple, published by Choral Software. Article text and original examples © 2026 Choral Software. All rights reserved.
