Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 23

Topological Ordering and DAGs

Recommended resources

In our previous lesson, we introduced the core concepts of Directed Acyclic Graphs (DAGs) and topological ordering. You learned that a topological sort provides a linear arrangement of a graph's vertices that respects all prerequisite constraints. We concluded by stating the fundamental theorem that connects these two ideas: a directed graph has a topological ordering if and only if it is a DAG.

This lesson is dedicated to formally proving this crucial theorem. As you have a preference for understanding the theoretical underpinnings of algorithms, we will carefully walk through both directions of this "if and only if" statement. Mastering this proof will not only solidify your conceptual understanding but also reveal the logic that powers the topological sorting algorithms we will implement next.

The theorem we will prove is:

A directed graph has a topological ordering if and only if is a Directed Acyclic Graph.

1. Proof: Topological Order ⇒ DAG

Let's begin with the first implication: if a directed graph has a topological ordering, then it must be a DAG.

The most direct way to prove this is by contradiction. We will assume that a graph has a topological ordering and a cycle, and show that this leads to a logical impossibility.

[PDF] Topological Sort of a Directed Acyclic Graph - Luca Trevisan

This handout from Bocconi University provides a concise and formal proof by contradiction.

Read from the beginning of the document down to the end of the proof for Theorem 1. Focus on the section starting with Theorem 1. Pay close attention to how the existence of a cycle and a topological sort are shown to be mutually exclusive.

Let's summarize the argument.

  1. Assume the opposite: We assume a graph has both a topological ordering and a directed cycle.
  2. Characterize the cycle: Let the cycle be represented by the sequence of vertices .
  3. Apply the definition of topological ordering: According to the ordering , for every edge , must appear before . Applying this to our cycle:
    • The edge implies comes before in .
    • The edge implies comes before in .
    • ...and so on, until...
    • The edge implies comes before in .
  4. Find the contradiction: If we chain these conditions together, we get that must come before , which comes before ..., which comes before , which in turn comes before . This creates the absurd conclusion that must come before itself in the ordering.
  5. Conclude: Since our initial assumption leads to a contradiction, it must be false. Therefore, a graph that has a topological ordering cannot have a cycle. In other words, it must be a DAG.

This half of the proof is quite intuitive. A topological ordering establishes a clear "before" and "after" for all connected vertices, while a cycle represents a "before and after itself" paradox.


2. Proof: DAG ⇒ Topological Order

Now for the second, more involved implication: if a graph is a DAG, then it must have a topological ordering.

To prove this, we need to show that we can always construct such an ordering for any given DAG. The proof hinges on a critical property that we touched upon in the last lesson: every non-empty DAG must have at least one vertex with an in-degree of 0. Such a vertex is often called a source.

Let's first prove this property, as it is the cornerstone of the main proof.

1 DFS Properties 2 DAGs

These lecture notes from the University of Washington provide a clean proof by contradiction for this essential lemma.

Please read the section for Lemma 5. The argument involves starting at an arbitrary vertex and repeatedly walking backward along incoming edges.

The logic of that proof is as follows:

  • Assume for contradiction that a finite DAG has no source node, meaning every vertex has an in-degree of at least 1.
  • Pick any vertex . Since it has an incoming edge, we can walk backward to another vertex, .
  • Since also has an incoming edge, we can walk back to , and so on.
  • Since the graph has a finite number of vertices, this backward walk must eventually repeat a vertex. The portion of the path between the repeated vertices forms a cycle.
  • This contradicts our initial premise that the graph is a DAG. Thus, our assumption must be false, and every DAG must have at least one source node.

With this lemma established, we can now prove the main statement using mathematical induction. This is a very common and powerful proof technique in computer science and is perfectly suited for this problem.

1 DFS Properties 2 DAGs

Immediately following the previous lemma, these notes present a formal proof by induction that any DAG has a topological order.

Read the proof for Lemma 6. Follow the structure of the proof: base case, inductive hypothesis, and the inductive step.

Let's unpack the inductive argument:

  • Base Case: For a graph with vertex, a topological order trivially exists (it's just the single vertex).
  • Inductive Hypothesis (IH): Assume that any DAG with vertices has a topological ordering.
  • Inductive Step: We must show that a DAG with vertices also has a topological order.
    1. Using our lemma, we know that must have at least one source node (with in-degree 0).
    2. Let's create a new graph by removing and all of its outgoing edges from .
    3. This new graph has vertices. Importantly, it is also a DAG, because removing vertices and edges can never create a cycle.
    4. According to our inductive hypothesis, since is a DAG with vertices, it must have a topological ordering. Let's call this ordering .
    5. We can now construct a valid topological ordering for the original graph by simply placing at the beginning of . The final ordering is .
    6. Why is this valid? Because is a source node, it has no incoming edges, so placing it first violates no constraints. All of its outgoing edges point to vertices in , which all appear after in our constructed list . All constraints within are satisfied by .

Since we can always perform this step, we have proven that any DAG must have a topological ordering.


3. An Alternative View: Cycles and Depth-First Search

Before we move on to implementation, there's another powerful way to think about the relationship between cycles and DAGs, which ties into an algorithm you're already familiar with: Depth-First Search.

When we run DFS on a directed graph, we can classify its edges. One special class is back edges: an edge from a vertex u to a vertex v that is an ancestor of u in the DFS tree. A key theorem in graph theory states:

A directed graph has a cycle if and only if a DFS traversal of it reveals a back edge.

This gives us a direct algorithmic tool: to check if a graph is a DAG, run DFS and see if you find any back edges. The following video provides an excellent proof of this theorem.

Lecture 14: Depth-First Search (DFS), Topological Sort

This MIT OpenCourseWare lecture by Professor Erik Demaine provides a clear and intuitive proof for the equivalence of cycles and back edges in DFS.

Watch the segment from Cycle Detection. The proof is broken into two parts: Back edge ⇒ Cycle: This direction is straightforward. A back edge from a descendant to an ancestor, combined with the tree path between them, forms a cycle. Cycle ⇒ Back edge: This direction is more subtle. It argues that if a cycle exists, the first vertex of the cycle visited by DFS will become an ancestor to all other nodes in the cycle, ensuring that the final edge closing the cycle is classified as a back edge.

This connection between cycles and back edges is not just a theoretical curiosity. It forms the basis of the second major algorithm for topological sorting, which we will explore in a future lesson.


Conclusion

In this lesson, we have rigorously demonstrated the fundamental equivalence between a graph being acyclic and its capacity to be topologically sorted.

Key Takeaways:

  • Proof (Topo Order ⇒ DAG): Proved by contradiction. The existence of a cycle would require a vertex to appear before itself in any linear ordering, which is impossible.
  • Proof (DAG ⇒ Topo Order): Proved constructively via induction. The core insight is that every DAG has a source node (in-degree 0). By repeatedly finding a source, adding it to our list, and removing it from the graph, we can always construct a valid topological ordering.
  • Algorithmic Insight: The property that a DAG must have a source node is not just a proof trick; it's the engine behind our first topological sort algorithm. The equivalence between cycles and DFS back edges is the engine behind the second.

We have now solidified the "why." In our next lesson, you will put this theory into practice. We will implement Kahn's algorithm, a BFS-based approach that is a direct translation of the constructive proof you studied today: it iteratively processes vertices with an in-degree of 0.