Topological sort calculator
Order dependencies in a directed acyclic graph or detect a cycle. Your inputs stay on your device.
Set up your task
Starting local processing
Ready. Processing stays in this browser.
2 Your result
Your result updates in this browser. Check your input if the result looks unexpected.
How to use Topological sort calculator
Order dependencies in a directed acyclic graph or detect a cycle. The pre-filled problem gives you a worked starting point. Compare the response with your own working before trying the self-check below.
- Check the example inputs: Vertices (space-separated), Edges (from and to, one per line).
- Change the inputs for your problem. Results update as you type, so finish entering the values before copying an answer.
- Use the working to check an intermediate value or assumption. Then change the example as suggested in the self-check and compare the result.
Supported inputs and limits
List up to 20 vertices and 80 directed edges. Each edge points from a prerequisite to a dependent task.
How it works
A topological order places every prerequisite before the tasks that rely on it. A directed edge U to V means that U must come first. This matters in course planning, build systems and deciding the execution order for a graph of computations.
The calculator uses Kahn’s algorithm. First take vertices with no incoming edges, then reduce the dependency count for each of their successors. A newly independent successor becomes available. If the queue is exhausted while vertices remain, they include a cycle and no complete order exists.
Example
Run the pre-filled example. Its first result is “Topological order: A → B → C → D”.
- Vertices (space-separated)
- A B C D
- Edges (from and to, one per line)
- A B
A C
B D
C D
Topological order: A → B → C → D Remove a vertex with no remaining incoming edges; repeat until the graph is empty.
Questions about Topological sort calculator
Can an isolated vertex appear in the order?
Yes. With no adjacent dependencies, an isolated vertex can be placed anywhere in a valid topological order.
Can the same graph have several correct orders?
Yes. If two tasks are both free of remaining prerequisites, either can be chosen first.
Add the edge D A to the example.
Expect a cycle error. It requires D before A even though A is already a prerequisite for D through another vertex.
Related tools
Last updated