Tutorials › Graph Theory (Undergraduate) › Topological Orders and Linear Extensions

Directed Graphs and Tournaments · Tutorial 695 of 799

Topological Orders and Linear Extensions

Define topological orders and linear extensions, then practise finding and checking every valid order for small precedence digraphs.

Intermediate 10 min read

What You'll Learn

  • Define a topological order by requiring every directed edge to point forward in the order.
  • Translate directed paths into precedence constraints.
  • Relate topological orders of a DAG to linear extensions of its reachability order.
  • Enumerate all valid orders by branching on the next task whose constraints are satisfied.
  • Check a proposed order by comparing the positions of each arc’s endpoints.
  • Recognize why cycles obstruct topological orders and why disconnected tasks may interleave.

Orders That Respect Directed Constraints

A directed graph can record which tasks must happen before others, which events depend on earlier events, or which steps in a computation must precede later steps. An arc \(u\to v\) expresses the constraint that \(u\) comes before \(v\). A topological order is a way to list all vertices that satisfies every such constraint at once. When the constraints have more than one valid ordering, finding all of them reveals exactly which choices remain flexible.

Throughout, a digraph is finite and has directed arcs; an arc \(u\to v\) points from its tail \(u\) to its head \(v\). A directed path follows the arc directions. A directed acyclic graph, or DAG, is a digraph with no directed cycle. The empty digraph has an empty vertex set and no arcs.

Definition: Let \(D=(V,A)\) be a digraph with \(n\) vertices. A topological order of \(D\) is a listing \(v_1,v_2,\ldots,v_n\) of every vertex exactly once such that, for every arc \(v_i\to v_j\), \(i<j\). The position of a vertex \(v\) in the listing is its index. The empty digraph has one topological order: the empty listing.

Thus an arc constrains relative position, not adjacency in the list: if \(u\to v\), other vertices may appear between \(u\) and \(v\). A topological order is also called a linear ordering that respects the arcs. In a precedence model, vertices are tasks and an arc \(u\to v\) means that task \(u\) must be completed before task \(v\).

A directed path can impose a constraint even when there is no direct arc between its endpoints. For example, if \(u\to w\) and \(w\to v\), every topological order places \(u\) before \(w\) and \(w\) before \(v\), hence also places \(u\) before \(v\). This is why it is useful to compare topological orders with linear extensions of a partial order.

Definition: A partial order on a set is a relation that is reflexive, antisymmetric and transitive. Its strict version, written \(u\prec v\), means that \(u\) is strictly before \(v\). A linear extension of a partial order is a listing of all its elements exactly once in which \(u\) appears before \(v\) whenever \(u\prec v\).

Reachability and Linear Extensions

For a DAG \(D=(V,A)\), define \(u\prec v\) when there is a directed path of positive length from \(u\) to \(v\). This relation records all precedence constraints forced by directed paths, not just those written as individual arcs.

Proposition: In a DAG, the reachability relation \(\prec\) is a strict partial order, and a listing is a topological order exactly when it is a linear extension of this reachability order.

Proof. We first verify the strict partial order properties. No vertex \(u\) satisfies \(u\prec u\), because such a positive-length directed path would be a directed closed walk and would contain a directed cycle. Thus the relation is irreflexive. If \(u\prec v\) and \(v\prec w\), concatenating the two directed paths gives a directed walk from \(u\) to \(w\). Removing any repeated-vertex portions gives a directed path from \(u\) to \(w\); since \(u=w\) would produce a directed cycle, the endpoints are distinct. Hence \(u\prec w\). Finally, \(u\prec v\) and \(v\prec u\) would concatenate to a directed closed walk of positive length, which contains a directed cycle. This is impossible in a DAG. So the relation is transitive and no two distinct vertices precede each other in both directions.

Now suppose a listing is a topological order. Along every arc, the tail occurs before the head. Along a directed path, applying this fact successively shows that its first vertex occurs before its last vertex. Therefore the listing respects every pair \(u\prec v\), so it is a linear extension. Conversely, every arc \(u\to v\) is itself a directed path of length one, so \(u\prec v\). A linear extension must therefore place \(u\) before \(v\) for every arc, which is precisely the definition of a topological order. \(\square\)

There is also a simple necessary condition for a topological order that applies to any digraph: it cannot contain a directed cycle. If a directed cycle has vertices \(v_1,\ldots,v_k\) in cyclic order, its arcs would require \(v_1\) before \(v_2\), \(v_2\) before \(v_3\), and so on, with \(v_k\) before \(v_1\). These requirements cannot all hold in a linear listing. This obstruction is useful when checking whether a precedence model is consistent.

Enumerating All Valid Orders

For a small digraph, one reliable way to enumerate orders is to build each listing from left to right. At every step, choose a vertex whose required predecessors have already appeared. A required predecessor of \(v\) is any vertex \(u\) with a directed path to \(v\); checking just the direct incoming arcs also works if all those constraints have already been respected. If no vertices remain, the listing is complete. If a choice branches, explore each choice. This is an enumeration method for small examples, not a claim that listing all orders is efficient for large digraphs.

Worked Example: Two Tasks Before a Shared Task

Let \(D=(V,A)\) be the digraph with \(V=\{a,b,c\}\) and \(A=\{a\to c,\ b\to c\}\). The tasks \(a\) and \(b\) must both precede \(c\), while there is no constraint between \(a\) and \(b\).

The first position cannot be \(c\), because both \(a\) and \(b\) must come before it. If \(a\) is first, then \(b\) must be second and \(c\) third. If \(b\) is first, then \(a\) must be second and \(c\) third. The complete list is therefore \(a,b,c\) and \(b,a,c\).

Check: In \(a,b,c\), the positions satisfy \(\operatorname{pos}(a)=1<3=\operatorname{pos}(c)\) and \(\operatorname{pos}(b)=2<3=\operatorname{pos}(c)\). In \(b,a,c\), they satisfy \(\operatorname{pos}(a)=2<3=\operatorname{pos}(c)\) and \(\operatorname{pos}(b)=1<3=\operatorname{pos}(c)\). The branching covers both possible first vertices, so no other order is omitted.

Worked Example: Branching on Available Tasks

Let \(D=(V,A)\) have \(V=\{a,b,c,d\}\) and \(A=\{a\to c,\ b\to c,\ b\to d\}\). The constraints are \(a\prec c\), \(b\prec c\) and \(b\prec d\). We will enumerate all orders by considering the first choice and then the choices it permits.

The first vertex must be \(a\) or \(b\), since \(c\) requires both \(a,b\), and \(d\) requires \(b\). If \(a\) is first, \(b\) is the only possible second vertex. Then both \(c\) and \(d\) may follow in either order, giving \(a,b,c,d\) and \(a,b,d,c\).

If \(b\) is first, the next vertex can be \(a\) or \(d\). After \(b,a\), both \(c\) and \(d\) can be placed in either order, giving \(b,a,c,d\) and \(b,a,d,c\). After \(b,d\), vertex \(a\) must come next, followed by \(c\), giving \(b,d,a,c\). Thus there are \(2+2+1=5\) orders.

OrderPositions of \(a,c\)Positions of \(b,c\)Positions of \(b,d\)
\(a,b,c,d\)\(1<3\)\(2<3\)\(2<4\)
\(a,b,d,c\)\(1<4\)\(2<4\)\(2<3\)
\(b,a,c,d\)\(2<3\)\(1<3\)\(1<4\)
\(b,a,d,c\)\(2<4\)\(1<4\)\(1<3\)
\(b,d,a,c\)\(3<4\)\(1<4\)\(1<2\)

Check: Each row verifies all three arcs by comparing the endpoint positions. The branching is exhaustive: the first choice is \(a\) or \(b\); after \(a\), only \(b\) can come next; after \(b\), the next choice is \(a\) or \(d\). The resulting branches give exactly five listings.

Worked Example: Interleaving Two Independent Chains

Let \(D=(V,A)\) have \(V=\{a,b,c,d\}\) and \(A=\{a\to c,\ b\to d\}\). The constraints form two chains, \(a\) before \(c\) and \(b\) before \(d\), with no constraint between the chains. In any valid order, the pair \(a,c\) must occur in that order, and the pair \(b,d\) must occur in that order.

Choose the two positions occupied by \(a,c\); they are then filled in that order, and the remaining positions are filled by \(b,d\) in that order. The six choices of positions give:

  • Positions \(1,2\) for \(a,c\): \(a,c,b,d\).
  • Positions \(1,3\) for \(a,c\): \(a,b,c,d\).
  • Positions \(1,4\) for \(a,c\): \(a,b,d,c\).
  • Positions \(2,3\) for \(a,c\): \(b,a,c,d\).
  • Positions \(2,4\) for \(a,c\): \(b,a,d,c\).
  • Positions \(3,4\) for \(a,c\): \(b,d,a,c\).

There are \(\binom{4}{2}=6\) choices of positions, so this construction gives six orders. Check: In each listing, \(a\) occurs before \(c\), and \(b\) occurs before \(d\); these are the only two arcs. Conversely, every valid order determines exactly one pair of positions for \(a,c\). Hence the six listings are complete and none is repeated.

Why the Orders Matter—and a Common Pitfall

Topological orders make precedence information concrete. One order is a possible schedule when tasks have no additional restrictions such as durations or resource limits. Multiple orders indicate that some tasks can be rearranged without violating the stated constraints. The reachability partial order separates forced precedence from incidental choices: if neither \(u\prec v\) nor \(v\prec u\), the constraints do not force either one to precede the other, although other tasks may still affect where they can appear in a full listing.

A frequent counting error is to treat independent chains as indivisible blocks. For the digraph in the third example, one might regard \(a,c\) as a block and \(b,d\) as a block, then count only two orders: \(a,c,b,d\) and \(b,d,a,c\). That calculation wrongly excludes interleavings such as \(a,b,c,d\). The correction is to preserve the order within each chain while allowing their elements to interleave; choosing the two positions for \(a,c\) gives \(\binom{4}{2}=6\) orders.

Another useful check is to test every arc, not just the first and last entries or a few sample constraints. A proposed listing is valid exactly when every arc’s tail has a smaller position than its head. For a DAG, it is equivalent to check all reachability constraints, but checking the arcs is enough because the inequalities along a directed path force the corresponding endpoint inequality.

Check Your Understanding

For each question, state the relevant constraints explicitly and verify proposed orders against every arc.

  1. Find all topological orders of the digraph with \(V=\{p,q,r\}\) and \(A=\{p\to q,\ p\to r\}\).
  2. For \(V=\{a,b,c,d\}\) and \(A=\{a\to b,\ b\to c,\ a\to d\}\), list every topological order.
  3. Explain why a directed cycle prevents a topological order, using the required inequalities of positions.
  4. For \(V=\{x,y,z\}\) and \(A=\{x\to z,\ y\to z\}\), identify the reachability constraints and give the linear extensions.
  5. In the digraph with arcs \(a\to c\) and \(b\to d\), explain why treating each directed chain as one indivisible block misses valid orders.