Skip to main content
Logo image

Section 19.6 Topological sorting

Sometimes we want to turn a partial order into a total order. What makes an order partial instead of total is the presence of pairs of incomparable elements. So to convert our partial order into a total order we just need to impose an order relation on those previously incomparable element pairs. However, for each pair of incomparable elements there is a choice to be made of which will become the smaller and which the larger in the new total order. And we cannot carry out these choices completely arbitrarily, because we risk contradicting the required properties of a partial order (see Example 19.6.1).
The following definitions apply to a partial order on a set A.
compatible total order
a total order on A such that if a1a2 then a1a2
topological sorting
a process for determining a compatible total order

Example 19.6.1. A failed attempt at topological sorting.

The relation on P(N) is a partial order but not a total order. Consider what happens when we begin trying to build a total order on P(N) out of by choosing relations between previously incomparable elements arbitrarily.
  • Elements {1},{2,3} are -incomparable; choose {2,3}{1}.
  • Elements {1},{2} are -incomparable; choose {1}{2}.
Now, {2}{2,3} is already true, so to be compatible we must set {2}{2,3} in the new total order. But now {1}{2}{2,3} would dictate {1}{2,3} to satisfy the transitive property, but this contradicts our first arbitrary choice above.

Note 19.6.2.

If A is countable, whether finite or countably infinite, then specifying a total order on A amounts to writing the elements of A in an ordered list. (See Example 19.4.6 and Remark 19.4.7.) In that case, topological sorting amounts to creating such an ordered list so that if ab then a appears before b in the list.

Note 19.6.4.

In Step 2 of the algorithm, if Ai contains a minimum element, then you must choose that element, since in that case no other minimal element can exist (see the Fact 19.5.13).

Worked Example 19.6.5.

Consider
A=P({0,1,2})={,{0},{1},{2},{0,1},{0,2},{1,2},{0,1,2}}.
Apply Algorithm 19.6.3 to determine a total order on A that is compatible with .
Solution 1. Algorithm solution
In A0=A, we must choose a0=, since it is the minimum. Now remove a0 so that
A1={{0},{1},{2},{0,1},{0,2},{1,2},{0,1,2}}.
Choose a minimal element from A1: let a1={2}. Now remove a1; set
A2={{0},{1},{0,1},{0,2},{1,2},{0,1,2}}.
Choose a minimal element from A2: let a2={0}. Now remove a2; set
A3={{1},{0,1},{0,2},{1,2},{0,1,2}}.
Choose a minimal element from A3: let a3={0,2}. Now remove a3; set
A4={{1},{0,1},{1,2},{0,1,2}}.
We must choose a4={1}, since it is the minimum in A4. Now remove a4; set
A5={{0,1},{1,2},{0,1,2}}.
Choose a minimal element from A5: let a5={1,2}. Now remove a5; set A6={{0,1},{0,1,2}}. We must choose a6={0,1}, since it is the minimum in A6. There is only one element left; set A7={{0,1,2}} and choose a7={0,1,2}. So we have
{2}{0}{0,2}{1}{1,2}{0,1}{0,1,2}.
Notice that the maximum element of A ended up at the “top” of the total order and the minimum element was forced to the “bottom.”
Solution 2. Graphical solution
We can perform the algorithm of topological sorting graphically; at each step, choose a vertex that has no adjacent vertices below it in the graph, then cross that vertex and any adjacent edges out of the graph. (See Figure 19.6.6.)
Our end result is a list of our choices, in order:
{2}{0}{0,2}{1}{1,2}{0,1}{0,1,2}.
Notice that the maximum element of A ended up at the “top” of the total order and the minimum element was forced to the “bottom”.
First step in a graphical approach to topological sorting.
(a) Choose a0=.
Second step in a graphical approach to topological sorting.
(b) Choose a1={2}.
Third step in a graphical approach to topological sorting.
(c) Choose a2={0}.
Fourth step in a graphical approach to topological sorting.
(d) Choose a3={0,2}.
Fifth step in a graphical approach to topological sorting.
(e) Choose a4={1}.
Sixth step in a graphical approach to topological sorting.
(f) Choose a5={1,2}.
Seventh step in a graphical approach to topological sorting.
(g) Choose a6={0,1}.
Eighth step in a graphical approach to topological sorting.
(h) Choose a7={0,1,2}.
Figure 19.6.6. Example of a graphical approach to topological sorting.

Note 19.6.7.

Compatible total orders are not unique: in the previous worked example, the order in which the elements of A were originally written represents another compatible total order:
{0}{1}{2}{0,1}{0,2}{1,2}{0,1,2}.