Resource · Guide or article
Topological Sorting
Defines topological ordering of a directed graph and when one exists: exactly when the graph has no directed cycles. A primer for course-schedule and build-order style problems.
Reviewed
Open the original: Topological Sorting (opens the original site in a new tab)Practice this topicOpens the practice interviewer with a question on Graphs.
Add to a roadmap
Keep this resource with a roadmap you follow. It appears as your own extra resource; the roadmap's steps don't change and it doesn't count toward your tracked steps.
Source content: English
In the field of computer science, a topological sort or topological ordering of a directed graph is a linear ordering of its vertices such that for every directed edge uv from vertex u to vertex v, u comes before v in the ordering. For instance, the vertices of the graph may represent tasks to be performed, and the edges may represent constraints that one task must be performed before another; in this application, a topological ordering is just a valid sequence for the tasks. A topological ordering is possible if and only if the graph has no directed cycles, that is, if it is a directed acyclic graph (DAG). Any DAG has at least one topological ordering, and algorithms are known for constructing a topological ordering of any DAG in linear time.
PrepKit overview
PrepKit editors wrote and reviewed this overview from the source repository. It is not text from the resource itself.
- Suited to
- The source does not state a level
- Format
- Reference
What it covers
- The acyclicity condition, and the statement that linear-time construction algorithms exist
- Six valid orderings of one example DAG, each labeled by the rule that produced it
- Job scheduling and package dependency resolution as applications
How to use it
PrepKit study advice, not a claim made by the source.
Implement Kahn's method and DFS post-order on the example graph and check each output is valid. Make both report cycles.
Scope and limitations
Names no specific algorithm and gives no pseudocode or complexity beyond saying linear-time methods exist. Cycle reporting is not covered.
Access and format
Free English README on GitHub in the MIT-licensed trekhleb/javascript-algorithms repository.
Reviewed by PrepKit on Oct 2, 2026. Listing source commit: 85293e3e2b88
Evidence behind this overview
- src/algorithms/graph/topological-sorting/README.md
Pages that include this
Get the PrepKit newsletter
Optional. You don't need it to use PrepKit.