BLOGLLM Logo
Go Pro
Guest
Not signed in
All BlogsCoursesPracticeInterview prepSeriesTagsBookmarksPricing
Log in
All BlogsCoursesPracticeTagsBookmarksPricing
My PrepProblemsStudy PlansResearch PapersRoadmapInterviewContestsLeaderboardGames
Articles
Practice / Autograd1 of 11 in topic

275. Order a compute graph for backprop with a topological sort

EasyAutograd

Autograd engines like micrograd and PyTorch run backward by visiting nodes in reverse topological order: a node is processed only after every node that uses it.

Write topo_order(graph, root) where graph maps each node name to the list of its input node names. Return a list of all nodes reachable from root, ordered so that every node comes AFTER all of its inputs (root is last). Use depth-first search, visiting inputs in the listed order, so the answer is unique.

Functiondef topo_order(graph, root)

Example 1

Input: graph = {'loss': ['y'], 'y': ['x']}, root = 'loss'

Output: ['x', 'y', 'loss']

Constraints

  • Python 3 standard library. Code runs in your browser.
  • Your code must pass all 3 tests to be accepted.
Hint 1

A node can only be added after all of its inputs, which is exactly what post-order depth-first search gives you.

Hint 2

Write a recursive visit with a seen set: if a node is new, mark it, visit each of its inputs in the listed order, then append the node to the output.

Hint 3

Use graph.get(n, []) so leaf nodes that are not keys in the dict still work, and mark the node as seen before visiting its inputs so shared nodes appear only once.

Common mistakes
  • The buggy version appends before visiting inputs (pre-order), which breaks that guarantee.
Understand the idea

Backprop must process a node only after every node that uses it has passed its gradient back. So you first list nodes so that each one comes after all of its inputs (a topological order), then walk that list in reverse. Like getting dressed: socks must come before shoes. Depth-first search does it: visit all inputs of a node first, and only then append the node.

Worked example

graph {L: [a, b], a: [x], b: [x, y], x: [], y: []}, root L. Visit L, go to a, go to x, x has no inputs so append x. Append a. Go to b; x is already visited, go to y, append y. Append b. Finally append L. Order: [x, a, y, b, L]. Every node appears after its inputs, root last. Appending a node before visiting its inputs (pre-order) breaks this.

post-order DFS with a visited set, inputs in listed order. micrograd and PyTorch's autograd both rely on this ordering
Topics
Autograd
Similar questions
  • Run reverse mode over an op tape where values are reusedEasy
  • Write the backward pass of a matrix multiplyEasy
  • Run forward-mode autodiff over a tiny instruction listEasy
  • Build a tiny reverse-mode autograd engineMedium
All Autograd problems
Report
275 / 903
Python 3solution.pyPython idle
1
2
3
4
def topo_order(graph, root):
# your code here
pass
​

Run checks these cases in your browser, no sign-in needed. Submit checks all 3 tests.

Ctrl/Cmd + Enter to run
© BlogLLM, Bengaluru, IndiaAboutTermsPrivacyRefundsDMCAContactRSS