Advanced maths

Topological sort calculator for a directed acyclic graph

Return a valid topological ordering and precedence layers for a DAG, or a closed directed-cycle witness with explicitly partial layers for cyclic input.

Your numbers

An edge u,v requires u before v. Use at most 200 rows with labels 1 through the vertex count. Blank permits an edgeless graph.

Calculated on your device. Your inputs aren’t sent to us.

Your result

Ready when you are

Enter your inputs and select Calculate.

Report an issue

Behind the numbers

How this calculation works

A topological order lists every vertex so each directed precedence edge points from an earlier position to a later one. This page uses indegree removal to produce one deterministic order and a layer for each removal round. If not every vertex can be removed, it finds a concrete directed cycle. Any already removed layers are labeled partial rather than presented as a valid ordering of the entire cyclic graph.

THE FORMULARepeatedly remove all zero-indegree vertices; a full ordering exists exactly when no directed cycle remains.

A worked example

For edges 1→3, 2→3 and 3→4, the first layer is {1,2}, the second is {3} and the third is {4}. The order 1,2,3,4 is valid, as is 2,1,3,4. Adding 4→1 creates the cycle 1→3→4→1, making a full topological order impossible. The tie between vertices one and two is resolved by label order, not by a computed importance score.

Good to know

Directed graphs with 1–40 vertices and at most 200 edge rows. Isolated vertices remain present and start in the first layer. Duplicate edges are tolerated as repeated constraints, while a self-loop makes the graph cyclic. Layers represent successive precedence stages without durations, deadlines or resources. Cyclic input receives a witness and possibly partial layers, not a full schedule. No unique ordering is promised when several vertices are simultaneously available.

A layer is a set of currently available vertices, not a duration estimate

The first layer contains vertices with no unmet incoming requirement. Removing that whole layer can free the next set, and the procedure continues. There is no directed edge between two vertices in one such layer, because that edge would give its target an unmet incoming requirement at the start of the round. That makes the layers useful for inspecting dependency depth without pretending that all tasks take the same real execution time.

For the default example, vertices one and two must both precede three, but the graph does not order one relative to two. A label-based tie choice supplies a repeatable linear output while preserving that freedom. If a fifth isolated vertex is declared, it joins the first layer even without appearing in an edge row. Counting only labels encountered in the edges would incorrectly lose that declared task from the ordering.

Two initial prerequisites merge into a third task before the final task1234Layer 1Layer 2Layer 3
Edges 1→3,2→3,3→4 give layers {1,2}, {3}, {4}. Either order of vertices one and two is valid within the first layer. The displayed geometry does not encode task durations.

The Strongly connected components calculator for a directed graph groups mutually reachable vertices and reduces the remaining relations to a condensation DAG.

A closed directed cycle explains the obstruction more clearly than a missing order

If removing zero-indegree vertices stalls, the remaining graph contains a directed cycle. The page walks the directed adjacency structure to return a closed witness whose last vertex repeats the first. Each consecutive pair is a genuine directed edge. Merely returning an unordered set of leftover vertices would not establish that they themselves form a cycle, because some may only be downstream from one.

The already removed prefix can still contain useful dependency information, but it is not a topological order of the whole cyclic input. Its table is therefore explicitly labeled partial. The same distinction matters in applications: a circular prerequisite cannot be repaired by listing the remaining vertices in arbitrary numeric order. Nor does a valid DAG order solve machine allocation or project scheduling with unequal durations; those need additional data and a different objective beyond the precedence relation entered here.

A few questions, answered

Does a self-loop count as a cycle?

Yes. Requiring a vertex before itself is already a directed cycle, and no topological ordering can satisfy that constraint.

Why can a different solver return another valid order?

Independent vertices can be placed in more than one relative order. Check that every vertex appears once and each edge goes forward; matching one particular tie order is not required.

Find your calculator

Start typing to explore the library.