Skip to content

Topological sort

Order the vertices of a directed acyclic graph so every edge points forward, using Kahn's algorithm of repeatedly removing a vertex of in-degree 0, and detect a cycle when no such order exists.

It opens on

Six courses have prerequisites. intro and math need nothing; ds needs intro and math; algo needs math and ds. Can ds be taken before math?

Why it matters

Scheduling with dependencies is everywhere, course plans, build systems that compile files in order, spreadsheets that recompute cells, package installers; topological sort is how they find a valid order, and a leftover vertex is how they report a circular dependency.

Good to know first

  • Graphs and their representations: directed edges and in-degree
  • Breadth-first search: a queue of vertices ready to be processed
  • Depth-first search: cycles and what they block

Step by step

  1. Dependencies as a DAG

    Draw dependencies as a directed acyclic graph, compute in-degrees, and say what a topological order is.

    That answers the opening question. A topological order lists all the vertices so that every arrow points from an earlier one to a later one, a valid order to take the courses.

    A common mix-up

    There is one correct topological order.

    In fact: Any order with every edge pointing forward is valid, and most DAGs have several.

  2. Kahn's algorithm

    Run Kahn's algorithm with a queue of in-degree 0 vertices, tracing the queue and in-degrees, and check the result.

    Removing intro lowers ds from 2 to 1 and org from 1 to 0, so org joins the queue, now math, org.

    A common mix-up

    Kahn's algorithm processes vertices in the order they appear in the graph.

    In fact: It processes them in the order their in-degree reaches 0, so org came before ds although ds is listed first.

  3. Cycles and cost

    Detect a directed cycle when Kahn's algorithm stops early, and give the algorithm's O(V + E) cost.

    The run stopped with only 3 of the 6 courses ordered and reported a cycle. Any vertex left over means a cycle among the leftovers, here ds → os → ds, which also blocks algo.

    with os → ds added, stopped

    A common mix-up

    An empty queue means every vertex has been ordered.

    In fact: It can also mean the rest are stuck in or behind a cycle; compare the length of the order with V.

Try it

Give a topological order that starts with math.Answer

For example math, intro, ds, org, algo, os

Hint: Intro must still come before ds and org

Add a course ml with prerequisites algo and math. Where can ml go in the order, and what is its in-degree at the start?Answer

After algo, for example last; its in-degree starts at 2

Hint: Both arrows arrive at ml

In the original graph, add algo → intro. Does a topological order exist?Answer

No; intro → ds → algo → intro is a cycle, and the algorithm stops at once after math, since intro's in-degree is 1

Hint: Is any course left with in-degree 0 after math?

Which courses can come first in any topological order, and why?Answer

Intro or math, the courses with in-degree 0, since nothing has to come before them.

Next math is removed. Which in-degrees drop, and who joins the queue?Answer

Ds drops from 1 to 0 and algo from 2 to 1, so ds joins, making the queue org, ds.

Topological sort

30 minutes every month. No card.