Skip to content

12.1 Graphs, DAGs, and an iterative topological sort

Concepts from other sections (11)

This section uses these ideas. Each has a short definition and a link to the section that explains it in full. Tap a dotted term in the text to preview it.

  • Automatic differentiation / autodiff: computing exact derivatives of a program by applying the chain rule to each primitive operation it executes, in forward mode (Jacobian-vector products) or reverse mode (vector-Jacobian products); it is how neural networks get their gradients, with no step size and no symbolic formula. Read more: Chapter 14: Matrix Calculus and Autodiff
  • Proof by contradiction: a proof that assumes the statement is false and derives an impossibility, so the statement must be true; it is the standard route to results such as the irrationality of 2\sqrt 2. Read more: Chapter 11: Discrete Math 1
  • Depth-first search (DFS): traversal that follows one path as far as possible before backtracking, using a stack. Read more: 18.6 Graph as an adjacency list in Programming and Algorithms
  • Chain rule: Jg∘f(x)=Jg(f(x)) Jf(x)J_{g \circ f}(x) = J_g(f(x))\,J_f(x). Read more: 10.2 Jacobians, the multivariable chain rule, and numeric JVP/VJP
  • Reverse mode: automatic differentiation that records the computation and walks it backward once from the output, pushing adjoints into inputs. Read more: 14.2 Scalar reverse mode: the Value graph
  • Kahn’s algorithm: repeatedly output a node of in-degree 0 and decrement its successors’ in-degrees. Read more: 12.4 Discrete math 2 problem set, part a: DAGs, modular arithmetic, hashing
  • In-degree: the number of edges entering a vertex, counting parallel edges separately. Read more: 18.6 Graph as an adjacency list in Programming and Algorithms
  • First moment (mm, exp_avg): the EMA of the gradient. Read more: 16.3 Adam and AdamW: bias correction and decoupled weight decay
  • Constrained decoding: masking, at every step, every token that would take the output outside an allowed language. Read more: 23.10 Tool calls and constrained decoding in AI Engineering
  • Back edge: an edge found during depth-first search that points to a node still in progress on the current path; in a directed graph, tracked with three node states (unvisited, in progress, visited), a back edge is exactly a cycle. Read more: Chapter 8: Graphs in Programming and Algorithms
  • Transformation (JAX): a function such as jit, grad, vmap, or pmap that takes a pure function and returns a compiled, differentiated, batched, or parallel version of it. Read more: 16.3 Embeddings, small models, and platforms in AI Engineering

Automatic differentiation is built first on scalars, in Scalar reverse mode: the Value graph, and then on tensors, in Tensor, broadcasting backward, and no_grad from AI Engineering. In both, a loss is computed from the parameters through hundreds of operations, and backward has to push the derivative of the loss back through all of them to every parameter. Each operation can pass a gradient to its inputs only once its own gradient is complete, and a value used twice (a weight shared by every time step, the x in x * y + x) receives contributions from several places.

Get the order wrong and nothing crashes. A shared weight is simply updated with half its gradient: the loss goes down a little, then stalls. Get the traversal wrong in a different way, by writing it recursively, and the first recurrent model you unroll over 1000 tokens dies with RecursionError. This section defines graphs and DAGs from the beginning and derives the one function that fixes the order, toposort(root, parents). Its solution is graph.py, and its contract is graph.pyi.

The only background you need is sets, relations, induction, and proof by contradiction, all from Discrete Math 1.

Learning objectives

  • Define directed graphs, DAGs, and topological orders, and prove that a topological order exists exactly when the graph has no directed cycle.
  • Explain why backward needs every node before its inputs, and why the reverse of a depth-first post-order provides that order while breadth-first order and pre-order do not.
  • Run an iterative depth-first search by hand, tracking the stack of (node, iterator) pairs and the white, gray, and black states.
  • Implement toposort so that it is linear in the size of the graph, survives a chain of 10510^5 nodes, works on unhashable nodes, and raises on a cycle.

The notation used throughout the section is collected here.

SymbolMeaningType
G=(V,E)G = (V, E)a directed graph: a set of vertices VV and a set of edges EE
u→vu \to van edge from uu to vv, an element (u,v)∈E(u, v) \in E
n=∣V∣n = \lvert V \rvert, m=∣E∣m = \lvert E \rvertnumber of vertices and of edgesint
rrthe root: the loss, where backward startsa node
parents(v)\mathrm{parents}(v)the values vv was computed from: the edges v→pv \to piterable of nodes
π\pian order of the vertices, a list in which each appears oncelist
π(v)\pi(v)the position of vv in π\piint

A directed graph is a set of vertices VV and a set of ordered pairs E⊆V×VE \subseteq V \times V called edges. The edge (u,v)(u, v) is written u→vu \to v: it leaves uu and enters vv. A path is a sequence v0→v1→⋯→vkv_0 \to v_1 \to \dots \to v_k of edges; a cycle is a path with k≥1k \ge 1 that ends where it started. A vertex vv is reachable from rr when some path leads from rr to vv.

In code, a graph is usually stored as adjacency lists: for each vertex, the list of vertices its edges enter. The parents argument of our function is exactly that, a function from a node to the list of its out-neighbours.

A directed acyclic graph (DAG) is a directed graph with no cycle. A topological order is a list π\pi of the vertices in which every edge points forward: u→vu \to v implies π(u)<π(v)\pi(u) < \pi(v).

The central fact is that a graph has a topological order if and only if it is a DAG. One direction is immediate: if there were a cycle v0→v1→⋯→v0v_0 \to v_1 \to \dots \to v_0, the order would need π(v0)<π(v1)<⋯<π(v0)\pi(v_0) < \pi(v_1) < \dots < \pi(v_0), which is impossible. For the other direction, notice that a finite DAG has a vertex with no incoming edge. Follow edges backwards from any vertex; without a cycle you must eventually stop, and where you stop nothing points in. Put that vertex first and delete it. What remains is still a DAG, so by induction on nn it has a topological order too.

That proof is also an algorithm, Kahn’s: repeatedly take a vertex with no remaining incoming edge. We will come back to it when comparing designs.

Existence is settled; the next question is which order backward needs. A computation graph has one node per value. The edges go from each value to the values it was computed from, v→pv \to p for every p∈parents(v)p \in \mathrm{parents}(v): L=m+sL = m + s has edges L→mL \to m and L→sL \to s. The loss r=Lr = L reaches every node that matters.

Backward applies the chain rule. If pp is used by consumers v1,…,vkv_1, \dots, v_k, then

∂L∂p=∑i=1k∂L∂vi∂vi∂p,\frac{\partial L}{\partial p} = \sum_{i=1}^{k} \frac{\partial L}{\partial v_i} \frac{\partial v_i}{\partial p},

so pp may pass its gradient on only after every consumer viv_i has contributed. In edge terms, every edge v→pv \to p needs vv processed before pp. That is precisely a topological order of the graph with edges v→pv \to p, starting at the root. Seen from the forward computation, where inputs come first, it is the reverse topological order: the same thing with the arrows flipped.

So you need a topological order of the graph reachable from the root. Depth-first search (DFS) from rr produces one almost for free. DFS follows one edge as deep as it can go before trying the next. Each vertex is in one of three states: white (not seen), gray (entered, still exploring its edges, on the stack), or black (finished). The post-order lists vertices in the order they turn black.

Claim: in the post-order, every vertex comes after all the vertices it points to. Take an edge v→pv \to p and look at the moment DFS examines it, while vv is gray. If pp is white, DFS enters pp and finishes it before it can finish vv. If pp is black, it has already finished. If pp is gray, pp is an ancestor of vv on the current path, so there is a path p→⋯→vp \to \dots \to v, and with the edge v→pv \to p that is a cycle. In a DAG this cannot happen, and meeting a gray vertex is exactly how DFS detects a cycle. In both remaining cases pp turns black before vv.

Reversing the post-order therefore puts every vertex before the vertices it points to: the root first, each node before its parents. Every vertex is entered once (the colour test sees to that), and each edge is examined once, so the whole walk costs O(n+m)O(n + m).

The black state is what buys linearity. Without it, a vertex reached along two paths is explored twice, and a stack of dd diamonds (each node with two parents that share one parent below) has 2d2^d paths.

The textbook DFS is recursive, and recursion keeps the gray path on the call stack. CPython stops at about 1000 nested calls (sys.getrecursionlimit()), and a 1000-step unrolled recurrence is a path 1000 nodes long. The iterative version keeps the path on an explicit list of pairs (node, iterator over its parents):

  1. Push (r, iter(parents(r))) and mark r gray.
  2. Look at the top pair (v, it) and take the next parent p from it. If p is white, mark it gray, push (p, iter(parents(p))), and go back to step 2 (descend). If it is gray, there is a cycle: raise. If it is black, skip it and take the next parent.
  3. When it is exhausted, pop, mark v black, and append v to the post-order.

Keeping the iterator on the stack is what makes this the same traversal as the recursive one: when a child finishes, the parent resumes exactly where it left off. As a bonus, parents(v) is called once per node, at the moment the node is pushed.

Two practical details remain before the algorithm is ready for tensors. The first is how to remember a node’s colour. Tensors overload == to compare elementwise and return an array, and a Python class that defines __eq__ without __hash__ becomes unhashable. Two different tensors can also hold equal values and still be different nodes. So the visited state is a dictionary keyed by id(node), the object’s identity. An id is unique only among objects that are alive, but every node we record stays alive, because it sits on the stack or in the output list.

The second detail is determinism. A DAG usually has many topological orders, and toposort returns one specific order: the reverse post-order of the DFS that visits parents in the order parents lists them. That matters because backward adds contributions in this order, and floating-point addition is not associative. A different valid order changes the last bits of the gradients, and two runs with the same seed stop being bit-identical. Fixing the order here is what keeps the “same seed, same run” promise of Training loop and the autograd bigram.

Take L=(x⋅y)+(x+c)L = (x \cdot y) + (x + c) with intermediate nodes m=x⋅ym = x \cdot y and s=x+cs = x + c:

parents(L) = [m, s] parents(m) = [x, y] parents(s) = [x, c]
parents(x) = parents(y) = parents(c) = []

The table traces the iterative DFS one step per row, with each node on the stack shown alongside what its iterator has left.

StepTop of stackNext parentActionPost-order so far
1L [m, s]m (white)push m
2m [x, y]x (white)push x
3x []nonepop, emit xx
4m [y]y (white)push yx
5y []nonepop, emit yx, y
6m []nonepop, emit mx, y, m
7L [s]s (white)push sx, y, m
8s [x, c]x (black)skipx, y, m
9s [c]c (white)push cx, y, m
10c []nonepop, emit cx, y, m, c
11s []nonepop, emit sx, y, m, c, s
12L []nonepop, emit Lx, y, m, c, s, L

Reversed, the order is [L, s, c, m, y, x]. Check every edge: L comes before m and s; m before x and y; s before x and c. A correct solution must return exactly this list.

Now run backward in that order with x=2x = 2, y=3y = 3, c=1c = 1 (so m=6m = 6, s=3s = 3, L=9L = 9), starting from ∂L/∂L=1\partial L / \partial L = 1. Each row shows a node’s gradient at the moment it is visited and what it passes on.

VisitGradient completePasses on
L1to s: 1, to m: 1
s1to x: 1, to c: 1
c1
m1to x: y=3y = 3, to y: x=2x = 2
y2
x1 + 3 = 4

The result, ∂L/∂x=y+1=4\partial L / \partial x = y + 1 = 4, is correct. Contrast a pre-order, which emits each node on entry: it gives [L, m, x, y, s, c]. There x is visited right after m, holding only 3, and passes 3 on before s has contributed its 1. That is the bug a correct solution must not have.

The whole algorithm sits behind one function:

def toposort(root: T, parents: Callable[[T], Iterable[T]]) -> list[T]:
"""Every node reachable from root, each once, root first, every node before
each of its parents: the reverse DFS post-order, parents taken in the order
`parents` yields them. Keyed by id(); iterative; parents called once per node.
ValueError when a cycle is reachable from root."""

Scalar reverse mode and Tensor.backward call it as toposort(loss, lambda v: v._parents) and then walk the list, each node adding its local derivatives into its parents’ gradients. Passing parents as a function rather than requiring a particular node class keeps toposort usable for any graph, including the automata of Constrained decoding.

Start from what the previous sections established and the design follows almost line by line.

  1. Backward may pass a node’s gradient on only after every consumer has added its share. With edges v→pv \to p from a node to its parents, that is a topological order starting at the root: every node before each of its parents.
  2. The reverse of a depth-first post-order is such an order, it visits each node and edge once, and the gray state detects a cycle for free. Kahn’s algorithm would also work, but it needs every in-degree before it can start, which is a second full pass over the graph.
  3. The graph can be 10510^5 nodes deep, so the gray path cannot live on Python’s call stack. It goes on an explicit list of (node, iterator over its parents) pairs. Keeping the iterator, not the list of parents, means that when a child finishes, its consumer resumes at the next parent, exactly as a recursive call would return. The order is the same as the recursive DFS, and a node stays on the stack, gray, for exactly as long as it is on the current path.
  4. Nodes may be tensors, which are unhashable and compare elementwise, so the three colours live in a dictionary keyed by id(node). White is “no entry”; only gray and black need storing.
  5. The order must be one fixed list, not any valid one. Parents are visited in the order parents yields them, and the post-order is reversed once at the end.

The state is two integer marks and a dictionary. The root starts gray, because it is on the path from the first moment:

_ON_STACK = 1 # entered, some parents still being explored ("gray")
_DONE = 2 # every parent emitted, node emitted ("black")
def toposort(root: T, parents: Callable[[T], Iterable[T]]) -> list[T]:
# Keyed by id(node): tensors overload == elementwise and are unhashable,
# so a set of nodes would fail. Every node we key on stays alive (it sits
# on the stack or in `post`), so no id is ever reused during the walk.
# A node with no entry is white (not seen yet).
state: dict[int, int] = {id(root): _ON_STACK}
# The explicit stack replaces the call stack of a recursive DFS. Each entry
# holds the node and a live iterator over its parents, so `parents` is
# called exactly once per node, at the moment it is pushed.
stack: list[tuple[T, Iterator[T]]] = [(root, iter(parents(root)))]
post: list[T] = []

The loop is the three steps of the iterative DFS. It always works on the top of the stack, and the for loop resumes that node’s iterator where it stopped last time:

while stack:
node, pending = stack[-1]
for p in pending: # continues from the last parent taken
s = state.get(id(p))
if s is None: # white: enter p and descend into it
state[id(p)] = _ON_STACK
stack.append((p, iter(parents(p))))
break # resume this node's iterator later
if s == _ON_STACK: # gray: p is on the current path
raise ValueError("toposort: the graph has a cycle reachable from root")
# _DONE (black): p was already emitted through another path, as the
# shared x is at step 8 of the hand trace. Skip it and take the next parent.
else:
# The for loop ran out without `break`: every parent is black, so
# emitting the node now keeps "parents before node" in the post-order.
stack.pop()
state[id(node)] = _DONE
post.append(node)

Python’s for ... else runs the else branch only when the loop ends without break, which here means “the iterator is exhausted”. That is the moment the node turns black. Emitting it any earlier, when it is pushed, would produce the pre-order from the hand trace, where the shared x holds only part of its gradient.

Finally the post-order is reversed, so that the root comes first:

post.reverse() # node before its parents, root first
return post

On the graph of the hand trace the loop performs exactly the twelve steps of the table and returns [L, s, c, m, y, x]. A parent that appears twice in one list (x * x) is entered the first time and found black the second, so it is listed once. The full solution is graph.py.

Every node is pushed once (only white nodes are pushed, and pushing makes them gray) and popped once, and every edge is taken from an iterator once. So the walk is O(n+m)O(n + m) time and O(n)O(n) space for the marks, the stack, and the output, and parents is called nn times. Twelve stacked diamonds have 37 nodes and 2122^{12} root-to-leaf paths; the black mark is what keeps the cost at 37 visits.

The alternatives each fail somewhere. A recursive DFS is shorter, but it fails at about 1000 nested calls, and raising sys.setrecursionlimit only moves the crash to the C stack, where it is a segfault instead of an exception. Kahn’s algorithm, which is what PyTorch’s engine does, needs a first pass that counts each node’s consumers, keyed by id, then a second pass with a ready queue. It is the right choice for a multithreaded scheduler; here it doubles the work and produces a different, though valid, order. Pushing all parents at once, the common “iterative DFS” with a plain stack of nodes, loses the current path: a node is no longer on the stack exactly while its descendants are explored, so the gray test cannot detect cycles, and the emitted order is not a post-order without a second marker pass. Breadth-first order is not a topological order once a node sits at two depths (a parent that is also a grandparent), and pre-order emits a shared input before all of its consumers.

One tradeoff is accepted deliberately: the function returns a full list rather than yielding nodes, so it holds O(n)O(n) memory at once. Backward needs the whole order anyway, because it walks it after building it.

A correct solution has to pass a specific set of checks, each tied to something that breaks downstream if it fails. The table lists them.

CaseKind of checkChecksWhy it matters downstream
Hand exampleunitthe hand-trace order exactlybit-identical gradients across runs
Shared inputpropertyx after both m and scomplete gradients for shared weights
Uneven depthboundarya parent that is also a grandparent comes lastbreadth-first order is not enough
Stacked diamondsproperty12 stacked diamonds: 37 nodes, parents once eachlinear time on deep networks
Repeated parentboundaryx * x lists x twice, the order oncesquares and self-products
Unhashable nodesboundarynodes whose == raises and whose hash is Nonetensors as nodes in Tensor.backward
Deep chainboundarya chain of 100 001 nodeslong unrolled recurrences in Vanilla RNN with manual BPTT and truncation
Single nodeboundarya leaf as the roota parameter used directly as the loss
CycleboundaryValueError on a reachable cycleautomata in constrained decoding
Random DAGsproperty, differential200 random DAGs valid; with a back edge added, raises exactly when the stdlib graphlib finds a cyclean independent implementation agrees

The common mistakes map onto those checks. Each has a recognisable symptom and a specific line of the solution that prevents it:

PitfallSymptomHow the solution avoids it
a recursive DFSRecursionError once the graph is about 1000 nodes deepan explicit stack of (node, iterator) pairs
emitting a node when it is first entered (pre-order)a shared input passes on a partial gradientappends a node only when its iterator is exhausted (the else branch)
putting nodes in a set or using them as dict keysTypeError: unhashable type on the first tensorkeys every mark by id(node)
no mark for finished nodesduplicates in the order, exponential time on diamondsmarks finished nodes _DONE and skips them
breadth-first orderinvalid as soon as one input sits at two depthsuses the depth-first post-order, never levels
treating a gray node like a black onea cyclic automaton gets a silently wrong orderraises ValueError on an _ON_STACK parent
visiting parents in reversestill valid, but a different order and different gradient bitstakes parents in the order parents yields them

Warning: The pre-order bug is the dangerous one, because it produces a valid-looking list and plausible gradients. Only a shared input exposes it, so always test with a node that has two consumers.

The next consumer is Scalar reverse mode: the Value graph, whose backward walks toposort(loss, parents). In AI Engineering, Tensor, broadcasting backward, and no_grad does the same over tensors, accumulating gradients in this order, and Constrained decoding: from regex to token masks treats regex and JSON-schema automata as graphs whose reachability and cycles come from this traversal. Within this chapter, Trees and tries, longest-prefix match turns to trees, the simplest DAGs.

The same ideas run in production systems, usually with more machinery around them:

  • The PyTorch autograd engine counts each node’s dependencies first, then runs a ready queue (Kahn’s algorithm) across devices and threads; see compute_dependencies in torch/csrc/autograd/engine.cpp.
  • In JAX, a traced program (a jaxpr) is already a topologically ordered list, so the transpose simply walks it backwards; see backward_pass in jax/_src/interpreters/ad.py.
  • Python’s graphlib.TopologicalSorter (CPython Lib/graphlib.py) implements Kahn’s algorithm with incremental get_ready/done for parallel schedulers, and includes a cycle check.
  • Workflow engines (Airflow, Temporal child workflows) use the same order to schedule jobs whose inputs are other jobs’ outputs; see the chapter on durable orchestration in AI Engineering, starting at The append-only event log.
  • Cormen, Leiserson, Rivest, and Stein, Introduction to Algorithms, sections 20.3 and 20.4 (depth-first search, topological sort).
  • Sedgewick and Wayne, Algorithms, section 4.2.
  • Kahn, “Topological sorting of large networks” (1962).
  • Directed graph: a set of vertices and a set of ordered pairs of vertices called edges.
  • DAG: a directed graph with no directed cycle.
  • Topological order: a list of the vertices in which every edge points from an earlier vertex to a later one.
  • Computation graph: a graph with one node per value and an edge from each value to the values it was computed from.
  • Post-order: the order in which a depth-first search finishes vertices.
  • White, gray, black: the DFS states not seen, on the current path, and finished; meeting a gray vertex means a cycle.
  • A topological order exists exactly when the graph is a DAG.
  • Backward needs every node before its inputs, because a value’s gradient is complete only after every consumer has added its share; the reverse of a depth-first post-order is such an order.
  • Breadth-first order and pre-order look right on small graphs and are wrong on graphs with shared inputs or uneven depth.
  • One visited mark per node makes the walk O(n+m)O(n + m) instead of exponential in its depth.
  • The walk must be iterative, keyed by id, and fixed in its parent order: a 10510^5-deep chain breaks a recursive one, tensors cannot go in a set, and a different order changes gradient bits.
  1. Why is a node appended to post in the else branch, and not when it is pushed?

    Answer

    The else branch runs when the node’s iterator is exhausted, that is, when every parent is black and already in post. Appending there gives the post-order, in which every node comes after its parents, and reversing it puts every node before its parents. Appending on push gives the pre-order [L, m, x, y, s, c], where x is processed before its second consumer s has contributed.

  2. A parent is found in state _DONE. Why is that skipped silently, while _ON_STACK raises?

    Answer

    A black parent was finished through another path: it is a shared input (a diamond), which is normal in a DAG. A gray parent is still on the current path from the root, so there is a path from it to the current node, and the edge back to it closes a cycle. Raising on black would reject every shared weight; skipping gray would return a silently wrong order for a cyclic graph.

  3. Why does the stack hold an iterator for each node instead of its list of parents and an index, or instead of pushing all parents at once?

    Answer

    An iterator remembers where the node stopped, so after a child finishes, the node resumes at its next parent, exactly like a returning recursive call. That keeps the order identical to the recursive DFS and keeps the stack equal to the current path, which the cycle test relies on. An index into a list would also work but requires materialising each parent list; pushing all parents at once loses the path entirely.

  4. id() values can be reused by Python. Why is keying on id(node) safe here?

    Answer

    An id is unique among objects that are alive at the same time. Every node that has an entry in state is referenced by the stack (while gray) or by post (once black), so none of them can be freed and have its id handed to a new object during the walk.

  5. Why does the order have to be one specific order, when any topological order makes backward correct?

    Answer

    Backward adds the consumers’ contributions into a gradient in the order it visits them, and floating-point addition is not associative. A different valid order changes the last bits of the gradients, so two runs with the same seed would no longer be bit-identical.