Section 14.17

Directed acyclic graph (DAG)

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 u→vu \to v means that uu must come before vv. Some tools draw v→uv \to u for “vv depends on uu”; 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.

  1. Count each vertex’s incoming edges; this number is its in-degree.

  2. Put every vertex with in-degree 0 into a ready set.

  3. 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.

  4. 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 A→BA \to B, A→CA \to C, B→DB \to D and C→DC \to D. The in-degrees are 0 for A, 1 for B and for C, and 2 for D.

  1. Output A; B and C fall to 0 and become ready.

  2. Output B; D falls to 1.

  3. Output C; D falls to 0 and becomes ready.

  4. 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 D→BD \to B. 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 B→D→BB \to D \to B.

Authority. Let u→vu \to v mean that uu authorizes vv, 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 R→XR \to X, and let Y and Z reference each other: Y→ZY \to Z and Z→YZ \to Y. Compute the authorized set forward, repeatedly adding whatever an authorized vertex authorizes: it starts as {R}\{R\}, 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 G=(V,E)G = (V, E) has a finite set VV of vertices and a set EE of ordered pairs of vertices, the edges; |V||V| and |E||E| denote their numbers. A directed cycle is a sequence v0,v1,…,vkv_0, v_1, \dots, v_k with k≥1k \ge 1, vk=v0v_k = v_0 and each (vi,vi+1)(v_i, v_{i+1}) in EE; an edge v→vv \to v is a cycle with k=1k = 1. GG is a DAG if it has no directed cycle. A topological ordering gives the vertices distinct numbers ord(v)\mathrm{ord}(v) from 1 to |V||V| with ord(u)<ord(v)\mathrm{ord}(u) < \mathrm{ord}(v) for every edge (u,v)(u, v).

GG has a topological ordering if and only if it is a DAG. If GG has a cycle, an ordering would need ord(v0)<ord(v1)<…<ord(vk)=ord(v0)\mathrm{ord}(v_0) < \mathrm{ord}(v_1) < \dots < \mathrm{ord}(v_k) = \mathrm{ord}(v_0), which is impossible. If GG 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 |V|+|E||V| + |E|.

In a DAG, “a path of one or more edges leads from uu to vv” is a strict partial order §19.2.6: no vertex precedes itself, and if uu precedes vv and vv precedes ww, then uu precedes ww. A topological ordering extends it to a total order, which ranks every pair.

With roots of trust R⊆VR \subseteq V and u→vu \to v meaning that uu authorizes vv, the authorized set is the smallest set SS that contains RR and contains vv whenever it contains some uu with u→vu \to v: the vertices reachable from RR. The forward computation finds SS in time proportional to |V|+|E||V| + |E| even if GG 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 nn diamonds, each a vertex with edges to two vertices that both lead to the next diamond’s first vertex, has 1+3n1 + 3n vertices but 2n2^n paths from top to bottom: for n=20n = 20, 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].