DAGs and topological ordering [00N8]
- September 24, 2026
Definition DAG [00N9]
- September 24, 2026
A directed acyclic graph (DAG) is a directed graph with no directed cycles.
Remark DAG examples [00NA]
- September 24, 2026
In general, we tend to use DAGs to represent dependency relationships. For example:
- The graph of courses at Hendrix where there is a directed edge X \to Y when class Y has class X as a prerequisite
- The graph of modules in a software project where an edge X \to Y means that module Y imports module X
- A graph of tasks where an edge X \to Y means that task X must be completed before task Y
Definition Topological ordering [00NB]
- September 24, 2026
A topological ordering (topological order, topological sort, topsort) of a directed graph is an ordering of nodes v_1, \dots , v_n such that for every (v_i, v_j) \in E, we have i < j.
Remark Topological ordering means "edges all point to the right" [00NC]
- September 24, 2026
In other words, a topological ordering is a way to put the vertices of a graph in order so that the edges only point to the right. This corresponds to a consistent order in which classes can be taken, tasks can be done, etc. so the prerequisites are always met.
Theorem DAG \leftrightarrow topsort [00ND]
- September 24, 2026
A directed graph G has a topological ordering iff G is a DAG.
Proof.
- September 24, 2026
Subproof. DAG \leftrightarrow topsort ›
- September 24, 2026
Subproof. DAG \leftrightarrow topsort ›
- September 24, 2026
Lemma. DAG \to indegree 0 [00NE]
- September 24, 2026
Any DAG has a vertex with indegree 0.
Proof.
- September 24, 2026
Remark. [00NF]
- September 24, 2026
Note that this proof implicitly depends on the fact that the graph is finite (we only consider finite graphs in this class). Does the lemma still hold for infinite DAGs?
Now we prove that if G is a DAG, it has a topological ordering. Proof by induction on n / recursive algorithm.
- Base case: if n = 1 there is only one vertex and no edges, so there is a trivial topological ordering.
- If n > 1, find a vertex v with indegree 0 by the previous lemma/algorithm. Note that G - \{v\} (the graph formed by deleting vertex v and any adjacent edges) is also a DAG, since deleting things cannot create any cycles. Then by the induction hypothesis, G - \{v\} has a topological ordering. Adding v at the beginning then makes a valid topological ordering for G since v has no incoming edges.
Time complexity of naive topsort [00NG]
- September 24, 2026
The algorithm described in the above proof, for computing a topological ordering of a DAG, takes \Theta (V^2) time: searching for an indegree-0 vertex takes \Theta (V) time in the worst case (if we have to walk backwards through most of the graph), and we have to do it once for each vertex. But we can do better when the graph is sparse. The idea is to maintain some extra information that allows us to quickly find a vertex with indegree 0 on each iteration without searching.
Theorem Efficient topsort [00NH]
- September 24, 2026
A topological sorting algorithm can be implemented in \Theta (V+E) time.
Proof.
- September 24, 2026
We assume an adjacency list representation for the input graph.
We need to be able to quickly find the next remaining vertex with indegree 0 on each iteration (we don’t want to re-run a linear search every time), and also quickly delete a vertex (updating the indegrees of other vertices appropriately).
The idea is to maintain two data structures:
- An array/dictionary \mathit {in} where \mathit {in}[v] = the indegree of v. We can initialize this dictionary in \Theta (V + E) time: just iterate through every vertex u and increment \mathit {in}[v] for each outgoing edge u \to v.
- A queue/stack/whatever Z of vertices with indegree 0. After creating \mathit {in}, we can initialize Z in \Theta (V) time: iterate through the vertices and add each v to Z if \mathit {in}[v] = 0.
At each iteration, we pop/dequeue a vertex u from Z in \Theta (1). To "delete" u, we don't actually need to delete it from any graph data structure; it's enough to decrement \mathit {in}[v] for each edge u \to v, and add v to Z if \mathit {in}[v] becomes zero. This is \Theta (1) per edge and only looks at each edge once in total. Hence the entire algorithm is \Theta (V+E) overall.
Algorithm Kahn's Algorithm [00NI]
- September 24, 2026
The resulting algorithm is known as Kahn’s Algorithm, and was first published by Arthur Kahn in 1962.
Kahn's Algorithm analysis [00PF]
- October 16, 2026
How long does Kahn's Algorithm take?
- Creating T, Z, and \mathit {in} each take \Theta (1).
- Initializing \mathit {in} (the first \mathsf {for} loop) takes \Theta (V+E) since we loop over all vertices and then look at every edge once.
- Initializing Z (the second \mathsf {for} loop) takes \Theta (V), assuming we can add to Z in \Theta (1) time.
- The main \mathsf {while} loop will execute \Theta (V) times; in each loop we do some \Theta (1) operations (remove from Z, append to T), and do \Theta (1) work for each of several edges. Again, we will look at each edge once in total, so this main loop takes \Theta (V+E) time.