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 . 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: . 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 (,
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, orpmapthat 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
toposortso that it is linear in the size of the graph, survives a chain of nodes, works on unhashable nodes, and raises on a cycle.
Directed graphs
Section titled “Directed graphs”The notation used throughout the section is collected here.
| Symbol | Meaning | Type |
|---|---|---|
| a directed graph: a set of vertices and a set of edges | ||
| an edge from to , an element | ||
| , | number of vertices and of edges | int |
| the root: the loss, where backward starts | a node | |
| the values was computed from: the edges | iterable of nodes | |
| an order of the vertices, a list in which each appears once | list | |
| the position of in | int |
A directed graph is a set of vertices and a set of ordered pairs called edges. The edge is written : it leaves and enters . A path is a sequence of edges; a cycle is a path with that ends where it started. A vertex is reachable from when some path leads from to .
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.
DAGs and topological orders
Section titled “DAGs and topological orders”A directed acyclic graph (DAG) is a directed graph with no cycle. A topological order is a list of the vertices in which every edge points forward: implies .
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 , the order would need , 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 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.
Computation graphs and the backward order
Section titled “Computation graphs and the backward order”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, for every : has edges and . The loss reaches every node that matters.
Backward applies the chain rule. If is used by consumers , then
so may pass its gradient on only after every consumer has contributed. In edge terms, every edge needs processed before . That is precisely a topological order of the graph with edges , 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.
Depth-first search and the post-order
Section titled “Depth-first search and the post-order”So you need a topological order of the graph reachable from the root. Depth-first search (DFS) from 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 and look at the moment DFS examines it, while is gray. If is white, DFS enters and finishes it before it can finish . If is black, it has already finished. If is gray, is an ancestor of on the current path, so there is a path , and with the edge 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 turns black before .
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 .
The black state is what buys linearity. Without it, a vertex reached along two paths is explored twice, and a stack of diamonds (each node with two parents that share one parent below) has paths.
Iterative DFS
Section titled “Iterative DFS”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):
- Push
(r, iter(parents(r)))and markrgray. - Look at the top pair
(v, it)and take the next parentpfromit. Ifpis 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. - When
itis exhausted, pop, markvblack, and appendvto 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.
Identity, not equality
Section titled “Identity, not equality”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.
One fixed order
Section titled “One fixed order”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.
Working it by hand
Section titled “Working it by hand”Take with intermediate nodes and :
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.
| Step | Top of stack | Next parent | Action | Post-order so far |
|---|---|---|---|---|
| 1 | L [m, s] | m (white) | push m | |
| 2 | m [x, y] | x (white) | push x | |
| 3 | x [] | none | pop, emit x | x |
| 4 | m [y] | y (white) | push y | x |
| 5 | y [] | none | pop, emit y | x, y |
| 6 | m [] | none | pop, emit m | x, y, m |
| 7 | L [s] | s (white) | push s | x, y, m |
| 8 | s [x, c] | x (black) | skip | x, y, m |
| 9 | s [c] | c (white) | push c | x, y, m |
| 10 | c [] | none | pop, emit c | x, y, m, c |
| 11 | s [] | none | pop, emit s | x, y, m, c, s |
| 12 | L [] | none | pop, emit L | x, 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 , , (so , , ), starting from . Each row shows a node’s gradient at the moment it is visited and what it passes on.
| Visit | Gradient complete | Passes on |
|---|---|---|
L | 1 | to s: 1, to m: 1 |
s | 1 | to x: 1, to c: 1 |
c | 1 | |
m | 1 | to x: , to y: |
y | 2 | |
x | 1 + 3 = 4 |
The result, , 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.
Designing the interface
Section titled “Designing the interface”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.
Building it step by step
Section titled “Building it step by step”Start from what the previous sections established and the design follows almost line by line.
- Backward may pass a node’s gradient on only after every consumer has added its share. With edges from a node to its parents, that is a topological order starting at the root: every node before each of its parents.
- 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.
- The graph can be 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. - 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. - The order must be one fixed list, not any valid one. Parents are visited in the order
parentsyields 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 postOn 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.
Why this design
Section titled “Why this design”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 time and space for the marks, the stack, and the output, and parents is called times. Twelve stacked diamonds have 37 nodes and 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 memory at once. Backward needs the whole order anyway, because it walks it after building it.
What can go wrong
Section titled “What can go wrong”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.
| Case | Kind of check | Checks | Why it matters downstream |
|---|---|---|---|
| Hand example | unit | the hand-trace order exactly | bit-identical gradients across runs |
| Shared input | property | x after both m and s | complete gradients for shared weights |
| Uneven depth | boundary | a parent that is also a grandparent comes last | breadth-first order is not enough |
| Stacked diamonds | property | 12 stacked diamonds: 37 nodes, parents once each | linear time on deep networks |
| Repeated parent | boundary | x * x lists x twice, the order once | squares and self-products |
| Unhashable nodes | boundary | nodes whose == raises and whose hash is None | tensors as nodes in Tensor.backward |
| Deep chain | boundary | a chain of 100 001 nodes | long unrolled recurrences in Vanilla RNN with manual BPTT and truncation |
| Single node | boundary | a leaf as the root | a parameter used directly as the loss |
| Cycle | boundary | ValueError on a reachable cycle | automata in constrained decoding |
| Random DAGs | property, differential | 200 random DAGs valid; with a back edge added, raises exactly when the stdlib graphlib finds a cycle | an 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:
| Pitfall | Symptom | How the solution avoids it |
|---|---|---|
| a recursive DFS | RecursionError once the graph is about 1000 nodes deep | an explicit stack of (node, iterator) pairs |
| emitting a node when it is first entered (pre-order) | a shared input passes on a partial gradient | appends a node only when its iterator is exhausted (the else branch) |
| putting nodes in a set or using them as dict keys | TypeError: unhashable type on the first tensor | keys every mark by id(node) |
| no mark for finished nodes | duplicates in the order, exponential time on diamonds | marks finished nodes _DONE and skips them |
| breadth-first order | invalid as soon as one input sits at two depths | uses the depth-first post-order, never levels |
| treating a gray node like a black one | a cyclic automaton gets a silently wrong order | raises ValueError on an _ON_STACK parent |
| visiting parents in reverse | still valid, but a different order and different gradient bits | takes 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.
Where this leads
Section titled “Where this leads”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_dependenciesintorch/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_passinjax/_src/interpreters/ad.py. - Python’s
graphlib.TopologicalSorter(CPythonLib/graphlib.py) implements Kahn’s algorithm with incrementalget_ready/donefor 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.
Further reading
Section titled “Further reading”- 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).
Key terms
Section titled “Key terms”- 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.
Summary
Section titled “Summary”- 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 instead of exponential in its depth.
- The walk must be iterative, keyed by
id, and fixed in its parent order: a -deep chain breaks a recursive one, tensors cannot go in a set, and a different order changes gradient bits.
Review questions
Section titled “Review questions”-
Why is a node appended to
postin theelsebranch, and not when it is pushed?Answer
The
elsebranch runs when the node’s iterator is exhausted, that is, when every parent is black and already inpost. 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], wherexis processed before its second consumershas contributed. -
A parent is found in state
_DONE. Why is that skipped silently, while_ON_STACKraises?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.
-
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.
-
id()values can be reused by Python. Why is keying onid(node)safe here?Answer
An id is unique among objects that are alive at the same time. Every node that has an entry in
stateis referenced by the stack (while gray) or bypost(once black), so none of them can be freed and have its id handed to a new object during the walk. -
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.