TensorTonicTensorTonic
Problems
Study PlansProjectsInterviewPricingFeedback
Study Plan
Trace a Reachable Expression Graph
Neural NetworksCalculus
Medium

Find the nodes that contribute to output_id by following parent IDs backward. Include the output itself and exclude disconnected nodes. Each input node is a dictionary with id, data, grad, op, and parents; only id and parents determine reachability.

Return a tuple containing two lists: reachable node IDs as strings in original node-list order, then edges as two-item lists containing the parent ID followed by the child ID. Order edges by each reachable child’s position in nodes, then by its parent-list order; retain repeated edges.

Loading visualization...

Examples

Input: nodes=[{'id':'a','data':2.0,'grad':0.0,'op':'','parents':[]},{'id':'b','data':-3.0,'grad':0.0,'op':'','parents':[]},{'id':'c','data':10.0,'grad':0.0,'op':'','parents':[]},{'id':'f','data':-2.0,'grad':0.0,'op':'','parents':[]},{'id':'unused','data':99.0,'grad':0.0,'op':'','parents':[]},{'id':'e','data':-6.0,'grad':0.0,'op':'*','parents':['a','b']},{'id':'d','data':4.0,'grad':0.0,'op':'+','parents':['e','c']},{'id':'L','data':-8.0,'grad':0.0,'op':'*','parents':['d','f']}], output_id='L'

Output: (['a','b','c','f','e','d','L'],[['a','e'],['b','e'],['e','d'],['c','d'],['d','L'],['f','L']])

Explanation: The unused leaf has no path to L, while the remaining records form L's dependency closure.

Input: nodes=[{'id':'x','data':1.0,'grad':0.0,'op':'','parents':[]},{'id':'y','data':2.0,'grad':0.0,'op':'','parents':[]}], output_id='y'

Output: (['y'],[])

Input: nodes=[{'id':'x','data':2.0,'grad':0.0,'op':'','parents':[]},{'id':'y','data':3.0,'grad':0.0,'op':'','parents':[]},{'id':'sum','data':5.0,'grad':0.0,'op':'+','parents':['x','y']},{'id':'out','data':10.0,'grad':0.0,'op':'*','parents':['sum','x']}], output_id='out'

Output: (['x','y','sum','out'],[['x','sum'],['y','sum'],['sum','out'],['x','out']])

Hint 1

A set can track node IDs already visited through shared paths.

Hint 2

List iteration preserves the original node and parent order.

Requirements

  • Python lists, dictionaries, and sets are sufficient; NumPy is available.
  • Preserve node and parent ordering without changing nodes.
  • No floating-point calculation or gradient update is required.

Constraints

  • nodes is a nonempty list of exact id, data, grad, op, and parents records.
  • Node IDs are unique nonempty strings and output_id names an existing node.
  • data and grad are finite numbers, op is a string, and parents is a list of nonempty string IDs.
  • Every parent ID exists and the full graph is acyclic.
Try Similar Problems
Build a Scalar Expression GraphMediumCreate a Multiplication Value NodeEasyCreate an Addition Value NodeEasyBackpropagate Through a Scalar NeuronMediumTopologically Sort a Computation DAGMediumAccumulate Shared-Path GradientsMediumGradient-Check a Scalar NeuronMediumRun Reverse-Mode AutodiffHardChain Rule Through a 3-Layer NetworkMediumBackpropagation Through a Computation GraphHardJacobian of a Neural Network LayerHardBasic AutogradEasy

Accepts: array

Accepts: string

You must run your code first.

Trace a Reachable Expression Graph

Neural NetworksCalculus
Medium

Find the nodes that contribute to output_id by following parent IDs backward. Include the output itself and exclude disconnected nodes. Each input node is a dictionary with id, data, grad, op, and parents; only id and parents determine reachability.

Return a tuple containing two lists: reachable node IDs as strings in original node-list order, then edges as two-item lists containing the parent ID followed by the child ID. Order edges by each reachable child’s position in nodes, then by its parent-list order; retain repeated edges.

Loading visualization...

Examples

Input: nodes=[{'id':'a','data':2.0,'grad':0.0,'op':'','parents':[]},{'id':'b','data':-3.0,'grad':0.0,'op':'','parents':[]},{'id':'c','data':10.0,'grad':0.0,'op':'','parents':[]},{'id':'f','data':-2.0,'grad':0.0,'op':'','parents':[]},{'id':'unused','data':99.0,'grad':0.0,'op':'','parents':[]},{'id':'e','data':-6.0,'grad':0.0,'op':'*','parents':['a','b']},{'id':'d','data':4.0,'grad':0.0,'op':'+','parents':['e','c']},{'id':'L','data':-8.0,'grad':0.0,'op':'*','parents':['d','f']}], output_id='L'

Output: (['a','b','c','f','e','d','L'],[['a','e'],['b','e'],['e','d'],['c','d'],['d','L'],['f','L']])

Explanation: The unused leaf has no path to L, while the remaining records form L's dependency closure.

Input: nodes=[{'id':'x','data':1.0,'grad':0.0,'op':'','parents':[]},{'id':'y','data':2.0,'grad':0.0,'op':'','parents':[]}], output_id='y'

Output: (['y'],[])

Input: nodes=[{'id':'x','data':2.0,'grad':0.0,'op':'','parents':[]},{'id':'y','data':3.0,'grad':0.0,'op':'','parents':[]},{'id':'sum','data':5.0,'grad':0.0,'op':'+','parents':['x','y']},{'id':'out','data':10.0,'grad':0.0,'op':'*','parents':['sum','x']}], output_id='out'

Output: (['x','y','sum','out'],[['x','sum'],['y','sum'],['sum','out'],['x','out']])

Hint 1

A set can track node IDs already visited through shared paths.

Hint 2

List iteration preserves the original node and parent order.

Requirements

  • Python lists, dictionaries, and sets are sufficient; NumPy is available.
  • Preserve node and parent ordering without changing nodes.
  • No floating-point calculation or gradient update is required.

Constraints

  • nodes is a nonempty list of exact id, data, grad, op, and parents records.
  • Node IDs are unique nonempty strings and output_id names an existing node.
  • data and grad are finite numbers, op is a string, and parents is a list of nonempty string IDs.
  • Every parent ID exists and the full graph is acyclic.
Try Similar Problems
Build a Scalar Expression GraphMediumCreate a Multiplication Value NodeEasyCreate an Addition Value NodeEasyBackpropagate Through a Scalar NeuronMediumTopologically Sort a Computation DAGMediumAccumulate Shared-Path GradientsMediumGradient-Check a Scalar NeuronMediumRun Reverse-Mode AutodiffHardChain Rule Through a 3-Layer NetworkMediumBackpropagation Through a Computation GraphHardJacobian of a Neural Network LayerHardBasic AutogradEasy

Accepts: array

Accepts: string

You must run your code first.