Section 14.19

Topological ordering

Definition

A topological ordering of a directed graph, a set of vertices joined by one-way edges, is a list of all the vertices in which, for every edge from a vertex uu to a vertex ww, uu comes before ww. When an edge u→wu \to w means that ww needs uu first, a topological ordering lists every item after everything it needs, as a build order must. Such an ordering exists exactly when the graph has no directed cycle, a route of edges that returns to its start.

Where it fits

Main entry: Directed acyclic graph (DAG) (§14.17).

The directed acyclic graph (DAG) entry treats graphs without directed cycles and builds a topological ordering with Kahn’s algorithm §14.1 [248], published by A. B. Kahn in 1962: count the incoming edges of every vertex, repeatedly output a vertex whose count is 0, and after each output subtract 1 from the count of every vertex that the output vertex’s edges lead to. With adjacency lists (for each vertex, the list of vertices its edges lead to) this takes time proportional to the number of vertices plus the number of edges. A second method uses depth-first search §14.18: start a search from every vertex not yet visited, list each vertex when its visit returns, and reverse the list.

A topological ordering is usually not unique. The graph with edges A→BA \to B, A→CA \to C, B→DB \to D and C→DC \to D has exactly two, A,B,C,DA, B, C, D and A,C,B,DA, C, B, D, because no edge orders BB and CC. Every finite DAG has at least one. A finite DAG always has a vertex with no incoming edge, since a walk backwards along incoming edges that never stopped would repeat a vertex of the finite set and so trace a cycle. That vertex can go first; removing it leaves a smaller DAG, and the argument repeats.

Kahn’s algorithm also detects cycles. If it stops while vertices remain, every remaining vertex still has an incoming edge from another remaining vertex, so a backward walk inside the remaining set repeats a vertex and exposes a directed cycle. The vertices left over are those on a cycle and those that a cycle reaches.

Limits and common mistakes

An ordering says only which item may go before which. It does not say that an item will succeed or that resources suffice to run it. Two items that the graph leaves unordered may run in either order, or at the same time, only if the edges record every real dependency.

Questions answered

Why does no topological ordering exist for a directed cycle?

No topological ordering exists for a directed cycle because the cycle requires each of its vertices to come before the next, and following those requirements around the cycle puts the first vertex before itself. Take a cycle v1→v2→…→vk→v1v_1 \to v_2 \to \dots \to v_k \to v_1, and write pos(v)\mathrm{pos}(v) for the position of a vertex vv in a proposed list. A topological ordering needs pos(v1)<pos(v2)<…<pos(vk)<pos(v1)\mathrm{pos}(v_1) < \mathrm{pos}(v_2) < \dots < \mathrm{pos}(v_k) < \mathrm{pos}(v_1), hence pos(v1)<pos(v1)\mathrm{pos}(v_1) < \mathrm{pos}(v_1), which no position satisfies. With the two edges B→DB \to D and D→BD \to B, listing BB first breaks the edge D→BD \to B, and listing DD first breaks the edge B→DB \to D.

Read as dependencies, every vertex on the cycle needs another vertex of the cycle first, so none of them can go first. Kahn’s algorithm, which repeatedly outputs a vertex with no remaining incoming edge, shows this concretely: no vertex on the cycle ever reaches zero incoming edges, so the algorithm stops with those vertices left over. The repair is to the graph: remove or reinterpret an edge, or merge each strongly connected component, a largest set of vertices in which every vertex can reach every other, into one vertex. The merged graph has no directed cycle, so it has a topological ordering.

Related entries: Directed acyclic graph (DAG) (§14.17), Graph reachability search (§14.18). Further reading: [248].