Theorem. DAG \leftrightarrow topsort [00ND]
- September 24, 2026
A directed graph G has a topological ordering iff G is a DAG.
Proof.
- September 24, 2026
Subproof. DAG \leftrightarrow topsort ›
- September 24, 2026
(\Longrightarrow ) Suppose G has a topological ordering v_1, \dots , v_n. We must show that G has no directed cycles. Intuitively, this is because any directed cycle must have at least one edge pointing backwards. More formally, suppose there is a cycle C, whose lowest-numbered vertex is v_i, with the previous vertex in the cycle being v_j. But then there is an edge v_j \to v_i with i < j, a contradiction since v_1, \dots , v_n is a topological ordering. Hence G has no cycles.
Subproof. DAG \leftrightarrow topsort ›
- September 24, 2026
(\Longleftarrow ) Suppose G is a DAG; we will show that it has a topological ordering by giving an algorithm to compute one.
Lemma. DAG \to indegree 0 [00NE]
- September 24, 2026
Any DAG has a vertex with indegree 0.
Proof.
- September 24, 2026
Pick any starting vertex v, and keep following incoming edges backwards until finding a vertex with no incoming edges. This process must stop: if not, by the pigeonhole principle, since there are only finitely many vertices, we must eventually visit a vertex twice, but this would form a directed cycle, and we assumed the graph is a DAG.
Remark. [00NF]
- September 24, 2026
Note that this proof implicitly depends on the fact that the graph is finite (we only consider finite graphs in this class). Does the lemma still hold for infinite DAGs?
Now we prove that if G is a DAG, it has a topological ordering. Proof by induction on n / recursive algorithm.
- Base case: if n = 1 there is only one vertex and no edges, so there is a trivial topological ordering.
- If n > 1, find a vertex v with indegree 0 by the previous lemma/algorithm. Note that G - \{v\} (the graph formed by deleting vertex v and any adjacent edges) is also a DAG, since deleting things cannot create any cycles. Then by the induction hypothesis, G - \{v\} has a topological ordering. Adding v at the beginning then makes a valid topological ordering for G since v has no incoming edges.