A directed acyclic graph (DAG) is a directed graph with no directed cycle. Direction gives each edge a one-way meaning such as prerequisite → dependent. Acyclicity prevents a dependency chain from returning to its start. The underlying Graph representation may vary. The DAG property concerns the relationships it represents.
Define u ≺ v when v is reachable from u by a path of positive length. In a DAG this relation is transitive because paths compose, and irreflexive because u ≺ u would be a directed cycle. Positive-length reachability is therefore a strict partial order on the vertices. Equivalently, allowing paths of length zero makes reachability reflexive and produces a partial order.
Finiteness matters for the standard structural consequences. Every non-empty finite DAG has at least one source and at least one sink. A finite directed graph admits a topological ordering exactly when it is acyclic: the order linearizes the dependency constraints without changing them.
A DAG need not be a tree. It may have several sources or sinks, and two vertices may be connected by several distinct directed paths. A rooted directed tree is the narrower case in which every non-root vertex has exactly one parent.
Both variants model the spreadsheet example below as a graph. P is price, Q quantity, S subtotal, X tax, D discount and T total, and each edge points from a formula input to the cell that reads it. A depth-first search starts from the roots P, Q and D. A cell stays on the stack while its outgoing edges are explored and becomes finished once all of them are done.
Each edge falls into one of three cases. An edge to an unvisited cell descends. An edge to a finished cell is safe, because every path out of that cell was already explored without returning to the stack, so it cannot close a cycle with the current path. An edge to a cell still on the stack is a back edge. The stack holds a directed path from that cell to the current one, and the edge closes it into a cycle. The converse also holds: the first vertex of any cycle that DFS enters stays on the stack until the rest of the cycle is reached, so the edge returning to it is always seen as a back edge.
The acyclic run finishes T, X, S and P, then Q, then D. Reversing that gives the evaluation order D → Q → P → S → X → T. The order is valid because no back edge appeared. For every edge u → v, v either finished during u’s exploration or had already finished, so v finishes before u and lands after it in the reversed list. The circular-reference variant adds T → P, so total feeds back into price. DFS walks P → S → X → T, finds T → P with P still on the stack and stops, because no order can place P both before and after T. That is the claim above in executable form: a finite digraph has a topological order exactly when DFS finds no back edge, which is exactly when it is acyclic.
- m
- number of edges
- n
- number of vertices
Real-World Examples
- Build pipeline:
source → compile → test → package. Each edge points from a prerequisite to a dependent step, and no later step feeds back into an earlier one. A topological ordering therefore gives a valid execution sequence. - Course prerequisites:
Algebra → Calculus → Numerical Methods. A valid prerequisite chain cannot eventually require the starting course again. The absence of that cycle makes it possible to order courses so every prerequisite comes first. - Spreadsheet formulas:
price, quantity → subtotal → tax → total. Each formula reads values computed earlier in the dependency graph. Without a circular reference, the spreadsheet can evaluate cells in dependency order.
DAGs model build and task dependencies directly. They also describe the state-dependency order in finite one-pass Dynamic Programming. For a general digraph, contracting each Strongly Connected Component produces a condensation graph that is always a DAG, separating cyclic regions from the order between them.