Follow your curiosity

What discovery has been shared with you?

Start with one fact. Explore it, go deeper, then follow whichever branch catches your imagination.

Choose subjects for a surprise

Exploring any topic

Begin your discovery

Your next discovery is one click away.

Choose one or more subjects above, or leave Any Topic selected and let curiosity decide.

Mathematics

Directed Graphs and Topological Sorting

Quick fact

A topological sort exists for a directed graph if and only if the graph has no directed cycles—such graphs are called directed acyclic graphs (DAGs). This linear ordering respects every edge direction, making it essential for scheduling tasks with prerequisites.

Why this is interesting

You need to take a set of courses, but some depend on others. How do you find a valid order to take them all?

Read the full explanation

Understanding Directed Graphs and Topological Sorting

Imagine you have a list of tasks, each with a list of prerequisites. For example, to bake a cake, you must first buy ingredients, then mix them, then bake. This is a dependency: buying ingredients must come before mixing, mixing before baking. A directed graph can represent these dependencies: each task is a node (a dot), and each dependency is an arrow (an edge) pointing from the prerequisite to the task that depends on it. So an arrow from 'buy ingredients' to 'mix' means 'buy ingredients' must happen before 'mix'. A topological sort is an ordering of all the nodes such that for every arrow from A to B, A comes before B in the ordering. In the cake example, a valid order is: buy ingredients, mix, bake. Another valid order could be: buy ingredients, preheat oven, mix, bake (if preheating has no dependencies). The key is that you never put a task before one of its prerequisites. However, not every directed graph has such an ordering. If there is a cycle—like 'A depends on B' and 'B depends on A'—then it's impossible to decide which comes first. Such a cycle would mean a contradiction: you can't do A before B and also B before A. So topological sorting is only possible for directed acyclic graphs (DAGs), which have no directed cycles.

A deeper explanation

The underlying principle is that a directed acyclic graph encodes a partial order on its vertices: a relation that is transitive (if A < B and B < C, then A < C) and antisymmetric (no cycles). A topological sort is simply a linear extension of this partial order—a total order that respects all the pairwise constraints. How do we find such an ordering? There are two classic algorithms, both based on simple ideas. One method uses depth-first search (DFS). Starting from an arbitrary node, we recursively explore all its outgoing neighbors. When we finish exploring a node (i.e., all its descendants have been visited), we record it on a stack. At the end, the stack (in reverse order of finishing times) gives a topological sort. Why does this work? A DFS finishes a node only after all nodes reachable from it have been finished. Since an edge from A to B means B is reachable from A, B will finish before A (or A will finish after B). Therefore, when we pop the stack, we get A before B. This method also naturally detects cycles: if during DFS we encounter a back edge (an edge to a node currently on the recursion stack), a cycle exists. Another method, Kahn's algorithm, repeatedly finds nodes with in-degree zero (no prerequisites) and removes them, along with their outgoing edges, adding them to the output order. Each time we remove a node, we decrease the in-degrees of its successors; if a successor's in-degree becomes zero, it becomes a new candidate. This process continues until all nodes are removed. If at some point no zero-in-degree node remains but there are still nodes left, then a cycle exists. Kahn's algorithm is intuitive because it mimics the real-world process of completing all tasks that have no remaining prerequisites. Topological sorting is fundamental in many applications: build systems (like make) order compilation of source files, package managers resolve dependencies, and course schedulers sequence prerequisites. It also appears in circuit design, where signal flow must respect component ordering, and in genealogy, where a topological sort of a family tree gives a chronological order of generations (when ignoring marriages). Understanding this concept reveals how a simple graph property (acyclicity) guarantees a useful ordering, and it connects graph theory to the notion of partial orders, a deep mathematical structure.

Keep FACTREE close

Internet access is required. Updates arrive when you reopen or reload the app. You may need to sign in again in the installed app.