TensorTonicTensorTonic
Problems
Study PlansProjectsInterviewPricingFeedback
Study Plan
Topologically Sort a Computation DAG
Neural NetworksCalculus
Medium

Find a forward evaluation order for the selected output and its ancestors. Each node is a dictionary with id and parents, an ordered list of parent IDs. The node list need not already be in dependency order.

Every parent must appear before its child. Whenever several remaining nodes have all their parents already placed, the one appearing earliest in the original nodes list must come next. Exclude disconnected nodes.

Return a Python list of reachable node IDs as strings in this deterministic topological order.

Loading visualization...

Examples

Input: nodes=[{"id":"x","parents":[]},{"id":"square","parents":["x"]},{"id":"out","parents":["square"]}], output_id="out"

Output: ["x","square","out"]

Explanation: Each node appears after its only parent.

Input: nodes=[{"id":"a","parents":[]},{"id":"b","parents":[]},{"id":"c","parents":["b"]},{"id":"d","parents":["a"]},{"id":"out","parents":["c","d"]}], output_id="out"

Output: ["a","b","c","d","out"]

Input: nodes=[{"id":"unused","parents":[]},{"id":"x","parents":[]},{"id":"out","parents":["x"]}], output_id="out"

Output: ["x","out"]

Hint 1

A set can track IDs reached through parent links.

Hint 2

enumerate records each node’s original position for tie-breaking.

Hint 3

heapq can prioritize ready nodes by their original positions.

Requirements

  • Python lists, dictionaries, and sets are sufficient; NumPy is available.
  • Preserve identifiers and leave nodes unchanged.
  • No floating-point calculation is required.

Constraints

  • nodes is a nonempty list of dictionaries containing exactly id and parents.
  • Node IDs are unique nonempty strings; every parent ID exists.
  • The graph is acyclic, and each parents list contains no duplicate IDs.
  • output_id identifies a node in the graph.
Try Similar Problems
Run Reverse-Mode AutodiffHardGradient-Check an Autodiff GraphHardBackpropagate Through a Scalar NeuronMediumAccumulate Shared-Path GradientsMediumTrace a Reachable Expression GraphMediumGradient-Check a Scalar NeuronMediumBuild a Scalar Expression GraphMediumCreate a Multiplication Value NodeEasyChain 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.

Topologically Sort a Computation DAG

Neural NetworksCalculus
Medium

Find a forward evaluation order for the selected output and its ancestors. Each node is a dictionary with id and parents, an ordered list of parent IDs. The node list need not already be in dependency order.

Every parent must appear before its child. Whenever several remaining nodes have all their parents already placed, the one appearing earliest in the original nodes list must come next. Exclude disconnected nodes.

Return a Python list of reachable node IDs as strings in this deterministic topological order.

Loading visualization...

Examples

Input: nodes=[{"id":"x","parents":[]},{"id":"square","parents":["x"]},{"id":"out","parents":["square"]}], output_id="out"

Output: ["x","square","out"]

Explanation: Each node appears after its only parent.

Input: nodes=[{"id":"a","parents":[]},{"id":"b","parents":[]},{"id":"c","parents":["b"]},{"id":"d","parents":["a"]},{"id":"out","parents":["c","d"]}], output_id="out"

Output: ["a","b","c","d","out"]

Input: nodes=[{"id":"unused","parents":[]},{"id":"x","parents":[]},{"id":"out","parents":["x"]}], output_id="out"

Output: ["x","out"]

Hint 1

A set can track IDs reached through parent links.

Hint 2

enumerate records each node’s original position for tie-breaking.

Hint 3

heapq can prioritize ready nodes by their original positions.

Requirements

  • Python lists, dictionaries, and sets are sufficient; NumPy is available.
  • Preserve identifiers and leave nodes unchanged.
  • No floating-point calculation is required.

Constraints

  • nodes is a nonempty list of dictionaries containing exactly id and parents.
  • Node IDs are unique nonempty strings; every parent ID exists.
  • The graph is acyclic, and each parents list contains no duplicate IDs.
  • output_id identifies a node in the graph.
Try Similar Problems
Run Reverse-Mode AutodiffHardGradient-Check an Autodiff GraphHardBackpropagate Through a Scalar NeuronMediumAccumulate Shared-Path GradientsMediumTrace a Reachable Expression GraphMediumGradient-Check a Scalar NeuronMediumBuild a Scalar Expression GraphMediumCreate a Multiplication Value NodeEasyChain 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.