Lesson 4 of 17

Topological Sort

Ordering the Computation Graph

To compute gradients correctly, we need to visit nodes in reverse topological order — children before parents. That way, when we process a node's gradient, all downstream gradients are already accumulated.

The Graph

Every Value is a node in a DAG (Directed Acyclic Graph). Edges point from children to parents — the arrows follow the computation forward.

For z = (a + b) * c:

a ──┐
    ├── (a+b) ──┐
b ──┘           ├── z
c ──────────────┘

To run backward from z, we need to visit z first (set its gradient to 1), then (a+b) and c, then a and b.

DFS Post-Order

A DFS that appends each node after recursing its children produces topological order:

def build_topo(v):
    topo = []
    visited = set()

    def dfs(node):
        if node not in visited:
            visited.add(node)
            for child in node._children:
                dfs(child)
            topo.append(node)  # append AFTER children

    dfs(v)
    return topo

The root (starting node) will always be last in the list.

Your Task

Implement build_topo(v) and return the sorted list.

Python runtime loading...
Loading...
Click "Run" to execute your code.