Skip to content

Topological order

networks

Level 2 of 3

Also called Topological sort and Kahn's algorithm.

test yourself

Six questions, four to pick from and two for paper.

fresh numbers: the questions a run answers, asked again on new numbers

choose one

question 1

Topological order runs on this map, places freed by the same place joining in letter order. What number does place F take?

A0B1C1E1F2G1A
the answer, and why

the answer is B

A, B and E take 1, 2 and 3. C joined the queue before F, so C takes 4 and F takes 5.

this tests how it works, on the learn page

if you chose another

  • C takes 4. It joined before F, as B to C was crossed off before E to F.
  • E takes 3. It joined just after B.
  • G takes 6. It waits for F.

question 2

On this map, how many roads are crossed off before the queue empties?

A0B2C1E1F1G1A
the answer, and why

the answer is D

A crosses off its 2 roads, which frees E. E has no road out. B still has F to B coming in, so the queue empties.

if you chose another

  • That is every road on the map, but 4 of them sit on a loop.
  • That adds B to C, but B is never taken.
  • That is a ring with no place free, but A has no road coming in.

question 3

Which is true of a line and of every pair of nn places, from 4 places on?

the answer, and why

the answer is A

Neither has a loop, so by section 4's question 2 every place is taken.

this tests best, worst and average, on the learn page

if you chose another

  • A line crosses off n−1n - 1 and every pair n(n−1)2\frac{n(n - 1)}{2}.
  • It crosses off n(n−1)2\frac{n(n - 1)}{2}.
  • Every two are joined, so only one order fits.

question 4

On which map does the run cross off the most roads? A>B means a road from A to B.

the answer, and why

the answer is A

It has no loop, so each of its 5 roads is crossed off once.

if you chose another

  • Three roads, each crossed off once.
  • Four roads on a ring. No place is free, so it crosses off 0.
  • One road.

pen and paper

question 5about 8 minutes

The map has one-way roads A to E, A to B, E to F, B to F, B to C, F to G and C to G.

  1. Write the count at each place.
  2. Run the method, places freed by the same place joining in letter order. What is the order?
  3. How many roads are crossed off, and how many places taken?
a hint

Keep the queue in a line as you go, and cross off each road.

a worked answer

The counts are A 0, B 1, C 1, E 1, F 2 and G 2. A takes 1, and B and E join. B takes 2: C joins, and F still has 1. E takes 3, and F joins. C takes 4, and G still has 1. F takes 5, and G joins. G takes 6. So A, B, E, C, F, G. Each of the 7 roads is crossed off once, and all 6 places are taken.

A0B1C1E1F2G2
The map with each place's count above it.

this tests how it works, on the learn page

what a full answer includes
  • A 0, B 1, C 1, E 1, F 2, G 2.
  • A, B, E, C, F, G.
  • 7 roads, 6 places.

question 6about 8 minutes

The map has one-way roads A to B, B to C, C to G, G to F, F to B and G to H.

  1. Run the method. Which places get a number, and what do the others have written?
  2. Walk back from H along roads not crossed off. Which place comes twice, and what is the loop?
  3. Take out the road from F to B and run it again, places freed by the same place joining in letter order. What is the order?
a hint

Draw the map first, and mark each road you cross off.

a worked answer

A takes 1 and crosses off A to B, and the queue empties. B, C, F, G and H are not taken, with 1 written at each. Walking back from H goes G, C, B, F, then G again. So the loop is G to F, F to B, B to C and C to G, and H sits after it. With F to B out, the run gives the order A, B, C, G, F, H.

ABCFGH
The whole map. The loop, heavy, holds B, C, F and G.
Question 2: why does a run number every place, on a map with no loop? Let TT be how many it numbers.
  1. the move: when the queue empties

    the line:
    each place not takenhas a road in froma place not taken\begin{gathered} \text{each place not taken} \\ \text{has a road in from} \\ \text{a place not taken} \end{gathered}

    why it is allowed: Suppose the queue is empty and places are not taken. Each still has a count above 0, or it would have joined. A road from a place taken was crossed off, so a road not crossed off comes from a place not taken.

  2. the move: walk back

    the line:
    walking back meetsonly places not taken\begin{gathered} \text{walking back meets} \\ \text{only places not taken} \end{gathered}

    why it is allowed: Start at a place not taken and go backwards along a road not crossed off. By line 1 you reach another place not taken, which has such a road too, so the walk never has to stop.

  3. the move: a place comes twice

    the line:
    places not taken: on aloop, or after one\begin{gathered} \text{places not taken: on a} \\ \text{loop, or after one} \end{gathered}
    ABCEF
    A is taken, shaded, and the queue empties. Walk back from C: B, E, F, then B again. Read forwards, B to F, F to E and E to B, heavy, is a loop, and C sits after it.

    why it is allowed: There are only nn places, so a walk that never stops comes back to one. The roads between its two visits make a loop, and the place you started from sits on it or after it.

  4. the move: no loop, none left

    the line:
    T=nT = n

    why it is allowed: With no loop, by line 3 every place is taken, all nn. Each crosses off all its roads, so every road is crossed off. For 5 places, 5.

  5. the move: a loop, no order

    the line:
    a loop: noorder at all\begin{gathered} \text{a loop: no} \\ \text{order at all} \end{gathered}

    why it is allowed: Round any loop, as here, an order would need B before F, F before E and E before B, so B before itself. So stopping is the right answer, and places not taken mean a loop.

try it with a small nn: at n=5n = 5, line 4 gives T=5T = 5, which you can work out by hand

this tests where the formula comes from, on the learn page

what a full answer includes
  • A only; B, C, F, G and H have 1 written.
  • G. The loop is G, F, B, C and back to G.
  • A, B, C, G, F, H.