Definition
A directed acyclic graph (DAG) is a directed graph with no directed cycle. A directed graph is a set of vertices (nodes) joined by edges, each pointing from one vertex to another; a directed cycle is a path that follows edges in their direction back to its start. A graph is a DAG exactly when its vertices can be listed so that every edge points from an earlier to a later vertex, a listing called a topological ordering §14.19. DAGs model prerequisites, such as build steps and task dependencies.
The idea
A program is built from two source files: compiling a.c gives a.o, compiling b.c gives b.o, and linking both gives the program app. Draw an arrow from each file to each step that reads it. A build tool must run every step after the steps whose output it reads, which the arrows show. If the arrows formed a circle, say a.o needing b.o and b.o needing a.o, no step on it could run first.
Picture a slope on which every arrow points downhill. Water never returns to a point it has left, which is what acyclic means, and listing the points from highest to lowest puts every arrow forward. The picture fails in one respect: a DAG has no single height, so many orders usually fit.
How it works
First fix what an edge means. Here an edge means that must come before . Some tools draw for “ depends on ”; either convention works, but mixing them reverses the order.
Kahn’s algorithm §14.1 (1962) [248] computes a topological ordering or shows that none exists.
Count each vertex’s incoming edges; this number is its in-degree.
Put every vertex with in-degree 0 into a ready set.
While the ready set is not empty, move any vertex from it to the output; for each edge leaving that vertex, subtract 1 from the target’s in-degree, and add the target to the ready set when it reaches 0.
If every vertex was output, the output is a topological ordering. Otherwise the graph has a cycle: each remaining vertex still has an incoming edge from another remaining vertex.
Each vertex and each edge is handled once, so the cost grows with their total number.
A depth-first search §14.18, which follows edges as far as possible before backing up, marks each vertex unvisited, in progress (on the current path) or finished; an edge into a vertex in progress closes a cycle. In a DAG, the reverse of the finishing order is a topological ordering. The graph reachability search entry treats these searches.
Worked example
An ordering. Take the edges , , and . The in-degrees are 0 for A, 1 for B and for C, and 2 for D.
Output A; B and C fall to 0 and become ready.
Output B; D falls to 1.
Output C; D falls to 0 and becomes ready.
Output D.
All 4 vertices were output, so A, B, C, D is a topological ordering. So is A, C, B, D: no chain of edges joins B and C, so the graph does not order them.
A cycle. Add the edge . B now has in-degree 2, from A and D, and D has in-degree 2, from B and C. Output A: B falls to 1 and C to 0. Output C: D falls to 1. The ready set is now empty, and B and D remain, each waiting for the other. Only 2 of the 4 vertices were output, which exposes the cycle .
Authority. Let mean that authorizes , as when a key signs a certificate. Let R be a root of trust §1.33, a key accepted because a trusted process installed it, with the edge , and let Y and Z reference each other: and . Compute the authorized set forward, repeatedly adding whatever an authorized vertex authorizes: it starts as , gains X, and stops. No path from R reaches Y or Z, so neither is authorized, although each claims the other’s approval.
Stated precisely
A directed graph has a finite set of vertices and a set of ordered pairs of vertices, the edges; and denote their numbers. A directed cycle is a sequence with , and each in ; an edge is a cycle with . is a DAG if it has no directed cycle. A topological ordering gives the vertices distinct numbers from 1 to with for every edge .
has a topological ordering if and only if it is a DAG. If has a cycle, an ordering would need , which is impossible. If has no cycle, some vertex has no incoming edge: otherwise, walking backwards along incoming edges forever would, in a finite graph, repeat a vertex and so trace a cycle. Number that vertex first, remove it, and repeat on the rest, which is still acyclic. Kahn’s algorithm carries out this argument in time proportional to .
In a DAG, “a path of one or more edges leads from to ” is a strict partial order §19.2.6: no vertex precedes itself, and if precedes and precedes , then precedes . A topological ordering extends it to a total order, which ranks every pair.
With roots of trust and meaning that authorizes , the authorized set is the smallest set that contains and contains whenever it contains some with : the vertices reachable from . The forward computation finds in time proportional to even if has cycles.
Limits and common mistakes
Acyclic does not mean complete or correct. A build graph missing a real dependency is still a DAG, and a tool following it can run a step before its input is ready. Nor does acyclicity show that the roots deserve trust or that each edge is genuine.
A topological ordering is rarely unique, so a tool that needs the same order on every run must fix how it chooses among ready vertices, for example alphabetically.
A DAG can have exponentially many paths. A chain of diamonds, each a vertex with edges to two vertices that both lead to the next diamond’s first vertex, has vertices but paths from top to bottom: for , 61 vertices and 1,048,576 paths. Code that walks every path takes exponential time; storing each vertex’s result, memoization §14.20, avoids it.
Cycles are not errors in every graph: a control-flow graph §3.13, the graph of possible jumps between blocks of a program’s code, has one wherever the program loops. Acyclicity is required where an edge means “must come before” or “draws its validity from”.
Questions answered
What does a cycle in artifact authority references imply?
A cycle in artifact authority references implies circular justification: the artifacts on the cycle vouch for each other, and the cycle alone gives none of them any authority. An authority reference is an edge saying that one artifact, such as a key, a certificate or a release record, approves another. An artifact is authorized only if a chain of such edges leads to it from a root of trust, which a trusted process installed rather than another artifact approved. If Y’s authority comes only from Z, and Z’s only from Y, no chain reaches a root, so neither is authorized.
Such a cycle signals malformed or forged metadata, and an artifact that only the cycle supports must be rejected. A checker that asks recursively for each approver’s authority loops forever on the cycle unless it records visited artifacts, and one that treats a repeated visit as success accepts the forgery. The safe check runs forward from the roots, repeatedly marking whatever a marked artifact authorizes; it ends on any graph and leaves unmarked every cycle that no root reaches.
Related entries: Graph reachability search (§14.18), Topological ordering (§14.19), Cache invalidation (§14.21), Root of trust (§1.33). Further reading: [248].