Skip to main content

Welcome to a Grasp lesson

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

Lesson 22

Introduction to DAGs and Topological Ordering

Recommended resources

Welcome to a new module! In our previous lessons, you built a solid foundation in dynamic programming, learning to transform complex recursive relationships into efficient, iterative code. Now, we'll see how this powerful technique can be applied to graphs. Many problems in competitive programming involve finding optimal paths or counting possibilities within a graph, but general graphs with cycles make DP challenging.

This lesson introduces a special and very important type of graph where DP thrives: the Directed Acyclic Graph (DAG). We'll start by defining what a DAG is and explore the concept of a topological ordering, which is a linear arrangement of a DAG's vertices. Understanding these concepts is the first step toward unlocking a whole new class of graph problems that can be solved elegantly with dynamic programming.

1. Modeling Dependencies: From Partial to Total Order

In many real-world scenarios and algorithmic problems, tasks have prerequisites. You must take "Calculus I" before "Calculus II," or a software library must be compiled before the program that depends on it. This creates a set of "before" and "after" relationships. In mathematics, this is known as a partial order: some pairs of items have a required order, but not all of them. For instance, you don't need to decide whether to take "Shakespeare" before or after "Calculus I."

Graph theory provides a natural way to model these partial orders. We can represent each task as a vertex and each prerequisite relationship as a directed edge. The video below introduces this idea and gives several examples.

Topological Sort Algorithm | Graph Theory

This video from WilliamFiset introduces the motivation for topological sort through intuitive, real-world examples.

Watch the first part of the video, from the beginning to the examples. Pay close attention to how course prerequisites and program build dependencies are modeled as directed graphs. Notice the core idea: an edge from A to B means A must happen before B.

The goal, then, is to convert this partial ordering into a total ordering—a single, linear sequence of tasks that satisfies all the prerequisites. This process is called topological sorting.

Introduction to Topological Sorting in Directed Acyclic Graphs

This short video from Algorithms with Attitude frames topological sorting in a more formal context, as the process of creating a "linear extension" of a partial ordering.

Watch from the introduction to the concept of linear extension. The analogy of putting on clothes is another classic example that clearly illustrates a partial order.


2. Directed Acyclic Graphs (DAGs)

The prerequisite graphs we've been discussing have a critical property: they cannot contain cycles. If task A requires B, B requires C, and C requires A, you have a logical impossibility—a deadlock where no task can ever be started. A directed graph that contains no directed cycles is known as a Directed Acyclic Graph, or DAG.

This is the formal definition that underpins our entire discussion.

Directed Acyclic Graphs (DAGs) & Scheduling: Chapter 9.5

This excerpt from an MIT OCW textbook provides the formal definition of a DAG.

Read the short section titled Directed Acyclic Graphs & Scheduling. It formally defines a DAG and solidifies its importance in scheduling and dependency analysis.

The absence of cycles is the defining feature of a DAG and is precisely what makes a topological ordering possible.


3. Topological Ordering: A Linear View of a DAG

We've used the term "topological ordering" informally. Let's now define it precisely.

A topological ordering (or topological sort) of a DAG is a linear ordering of all its vertices such that for every directed edge from vertex u to vertex v (u -> v), vertex u comes before vertex v in the ordering.

Visually, this means you can arrange all the vertices of the DAG in a straight line so that all directed edges point from left to right. There are no "backward" edges.

This image illustrates the concept perfectly. On the left is a standard representation of a DAG. On the right, the same vertices are arranged in one possible topological order, where all edges flow from left to right.

It is important to recognize that a topological ordering for a given DAG is often not unique. In the "getting dressed" example, you must put on socks before shoes, but the ordering of "left sock" versus "right sock" is not specified, leading to multiple valid sequences. In the image above, vertices 0 and 6 have no incoming edges, so either could have come first in a topological sort.


4. The Fundamental Property of DAGs

We now arrive at the central theorem of this lesson, which connects DAGs and topological sorting.

A directed graph has a topological ordering if and only if it is a DAG.

This is a powerful "if and only if" (iff) statement, meaning the implication goes both ways. As you appreciate proofs, let's briefly sketch the logic for both directions.

1. A topological ordering exists => The graph is a DAG.
This direction is easier to prove by contradiction. Assume a graph has a cycle, say v_1 -> v_2 -> ... -> v_k -> v_1. If a topological ordering existed, then in that ordering, v_1 must come before v_2, v_2 before v_3, ..., and v_k must come before v_1. This creates a contradiction: v_1 must come before v_1. Therefore, if a graph has a cycle, no topological ordering can exist. It follows that if a topological ordering does exist, the graph must be acyclic (a DAG).

Topologically Sorting a Directed Acyclic Graph

This resource succinctly explains why a graph with a cycle cannot have a topological ordering.

Read the two bullet points under the heading Does a topological order always exist?. This formalizes the argument we just made.

2. The graph is a DAG => A topological ordering exists.
To prove this, we need to show that we can always construct a valid ordering for any finite DAG. The key insight is that every non-empty, finite DAG must have at least one vertex with an in-degree of 0 (i.e., no incoming edges).

Why is this true? Imagine it wasn't. Then every vertex has an incoming edge. If we pick an arbitrary vertex v and walk backward along an incoming edge to u, then from u to w, and so on, we would be able to walk backward indefinitely. Since the graph is finite, we must eventually revisit a vertex, which means we have found a cycle. But we assumed the graph is a DAG (has no cycles), a contradiction! Therefore, our initial assumption was wrong, and there must be at least one vertex with an in-degree of 0.

This proof by contradiction is a classic piece of graph theory reasoning.

Topologically Sorting a Directed Acyclic Graph

This section of the Bowdoin University notes provides the formal proof by contradiction we just discussed.

Read the short section titled A standalone algorithm for topological sort. Focus on the claim and its proof. This argument is the theoretical basis for Kahn's algorithm, which we will study in the next lesson.

With this property established, we can construct a topological sort:

  1. Find a vertex with an in-degree of 0. Add it to our sorted list.
  2. Remove this vertex and all its outgoing edges from the graph.
  3. The remaining graph is still a DAG. Repeat the process, finding a new vertex with an in-degree of 0 in the modified graph, until no vertices are left.

Since we can always find such a vertex at every step, this process is guaranteed to succeed for any finite DAG.


Conclusion

In this lesson, we laid the conceptual groundwork for a new chapter in our graph theory journey. You now have a formal understanding of what defines a Directed Acyclic Graph and its relationship to scheduling and dependency problems.

Key Takeaways:

  • A Directed Acyclic Graph (DAG) is a directed graph that contains no cycles.
  • A topological ordering is a linear arrangement of a DAG's vertices where for every edge u -> v, u appears before v. This is like arranging the nodes so all edges point from left to right.
  • Topological orderings are often not unique.
  • The fundamental theorem states that a directed graph has a topological ordering if and only if it is a DAG.
  • A key property used to prove this is that every finite DAG has at least one vertex with an in-degree of 0.

We have focused on the "what" and the "why." In our next lesson, we will turn to the "how." You will learn to implement two classic algorithms for finding a topological sort: one based on Breadth-First Search (Kahn's Algorithm) and another based on Depth-First Search. These algorithms are fundamental tools in any competitive programmer's arsenal.