Sub topic 4
Why Only DAGs Have Topological Orders
This chapter assumes you know what a directed graph is, what a directed cycle is, and the definition of a topological order from the previous chapter. In a topological order, every edge u → v places u before v. You should also be comfortable with a simple proof by induction.
The claim we prove is this: a directed graph has a topological order if and only if it has no directed cycle. "If and only if" means two separate statements, so the proof has two directions.
Direction 1: A cycle rules out any order
Suppose the graph contains a directed cycle v₁ → v₂ → … → vₖ → v₁. Assume, for contradiction, that some topological order exists. Write pos(x) for the position of node x in that order.
Each edge on the cycle forces its tail before its head:
- pos(v₁) < pos(v₂)
- pos(v₂) < pos(v₃)
- …
- pos(vₖ) < pos(v₁)
If you chain these inequalities together, you get pos(v₁) < pos(v₁), which is impossible. So no topological order exists.
A self-loop (an edge u → u) is the smallest case of this. It would need pos(u) < pos(u).
Direction 2: Every DAG has an order
This direction is constructive, and it is the idea behind the algorithms in later chapters.
Step 1: Every nonempty DAG has a source
A source is a node with no incoming edges. Pick any node and walk backward: from the current node, follow some incoming edge to its tail, and repeat. There are two possible outcomes:
- You reach a node with no incoming edge. That node is a source.
- You keep walking forever. The graph has only n nodes, so after at most n steps some node must repeat. A repeated node means you walked around a directed cycle, which a DAG cannot contain.
Outcome 2 is impossible in a DAG, so the walk always ends at a source.
Step 2: Remove the source and recurse
The proof uses induction on the number of nodes n.
- Base case: a graph with zero nodes (or one node) has a trivial order.
- Inductive step: take a DAG with n nodes and find a source s. Put s first. Delete s and its outgoing edges. Deleting nodes can't create a cycle, so what remains is a DAG with n − 1 nodes. By the inductive hypothesis, it has a topological order. Append that order after s.
Now check every edge. Edges inside the smaller graph are respected by the inductive hypothesis. Every edge that touches s leaves s, since s has no incoming edges, and s comes first. So every edge points forward.
Common mistakes in this argument
| Mistake | Why it fails |
|---|---|
| Claiming every node in a DAG is a source or a sink | Middle nodes are neither. The proof only needs at least one source. |
| Walking forward instead of backward to find a source | A forward walk finds a sink (a node with no outgoing edges). That works too, but then you place the sink *last*. |
| Forgetting the finiteness of the graph | An infinite graph with no cycles can lack a source, for example an edge i+1 → i for every integer i. The pigeonhole step needs finitely many nodes. |
| Assuming the source is unique | A DAG can have many sources, and choosing different ones gives different valid orders. |
Summary
- A cycle forces a node to come before itself, so cyclic graphs have no topological order.
- A finite DAG always has a source, and removing it leaves a smaller DAG. Applying this repeatedly builds an order.
- Together these show that "has a topological order" and "is a DAG" mean the same thing.
Exercise
The source-finding argument walks backward along incoming edges. Rewrite the inductive proof so it uses a sink (a node with no outgoing edges) instead. Where does the sink go in the order, and why does every edge still point forward?
Show answer
Walk forward along outgoing edges. The walk can't repeat a node because that would mean a cycle, so in a finite DAG it ends at a sink t. Place t last. Remove t, which leaves a DAG with n − 1 nodes, and by induction it has an order. Put that order before t. Every edge touching t enters t, because t has no outgoing edges, and t comes after everything else. Edges among the other nodes are respected by the inductive hypothesis.
Exercise
A graph has nodes {a, b, c, d} and edges a→b, b→c, c→d, d→b. Use the proof to explain why no topological order exists, and name the cycle.
Show answer
The cycle is b → c → d → b. Any order would need pos(b) < pos(c) < pos(d) < pos(b), which is a contradiction. Node a is a source, but once you remove it, b, c, and d each still have an incoming edge, so there is no source left to take.