Definition · Topological orderA topological order of a digraph is a listing of all its vertices such that for every arc (u, v), u appears before v. A digraph with such a listing is often drawn left to right, which makes the condition visible: no arc points leftwards.
Theorem · ExistenceA digraph has a topological order if and only if it is acyclic.
If a topological order exists and v1, v2, ..., vk, v1 is a cycle, then each arc of the cycle forces its tail earlier than its head in the listing, so v1 appears strictly before itself. That is impossible, so there is no cycle.
If the digraph is acyclic, run one depth-first walk. It has no back arc, so for every arc (u, v) the head v is either discovered inside u and finished before it, or already finished when the arc is examined. Either way f[v] < f[u]. Listing the vertices by decreasing f therefore puts every tail before its head.
□
The proof is also the algorithm, which is why the first method costs nothing beyond the walk: sort by finish time, or equivalently push each vertex onto a stack as it closes and read the stack off. The lab reports 6 vertex visits and 6 arc reads on the opening graph, the same Θ(V + E) the walk itself costs.
Kahn's method, which never mentions a finish time
- Compute the in-degree of every vertex, and put every vertex of in-degree 0 into a queue.
- Remove a vertex from the queue, emit it, and decrement the in-degree of each of its successors.
- Any successor whose in-degree has just reached 0 joins the queue.
- Stop when the queue is empty. If fewer than V vertices were emitted, the remaining ones lie on or below a cycle and no order exists.
On the opening graph 1>3, 2>3, 3>4, 3>5, 4>6, 5>6 the starting in-degrees are 0, 0, 2, 1, 1, 2, two vertices are in the queue at once, and the method emits 1 2 3 4 5 6 with 4 queue pushes, 6 pops and 6 arc reads. The walk's answer on the same graph is 2 1 3 5 4 6. Both are checked arc by arc in the panel and both pass, and the disagreement is not a defect in either.
Example · Four valid orders, enumeratedFor that graph the lab enumerates every valid order and finds 4: 1 2 3 4 5 6, 1 2 3 5 4 6, 2 1 3 4 5 6 and 2 1 3 5 4 6. Kahn's answer is the first and the walk's answer is the last. The freedom is exactly the two independent starts and the two independent middles, and 2 times 2 is 4.
The enumeration is a real search over all orders, not a formula, so it is refused rather than run slowly above the size it can finish: nine vertices is declined with a message naming the cap, and the reason is that eight vertices with no arcs already have 8! = 40 320 orders. Where it does run, the number it prints is a fact about every order rather than about the one produced.
Uniqueness, and what it takes
A chain of five vertices has exactly 1 valid order; a diamond has 2; five vertices with no arcs have 5! = 120. The chain is the shape uniqueness needs: there must be an arc between each consecutive pair in the order, because two vertices with no path between them can always be swapped. So uniqueness is equivalent to the order being a Hamiltonian path of the digraph, which is a much stronger condition than acyclicity.
A cycle shows up twice, by two different mechanisms, and the lab prints both. On 1>2, 2>3, 3>1, 3>4 the depth-first walk finds a back arc and returns no order; Kahn's queue starts empty, because every one of the first three vertices has in-degree 1, and it emits nothing at all. The enumeration independently returns a count of 0. Three witnesses, one cause.
The measured counts are worth putting beside the bound. Kahn's method on the opening graph did 4 pushes, 6 pops and 6 arc reads, which is V pops and E reads. That is Θ(V + E) for this graph. The bound is the claim that it is Θ(V + E) for every graph, and the argument is that each vertex enters the queue at most once — it enters when its in-degree reaches 0, and the in-degree only ever decreases — and each arc is read exactly once, when its tail is emitted.