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.
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"]
A set can track IDs reached through parent links.
enumerate records each node’s original position for tie-breaking.
heapq can prioritize ready nodes by their original positions.
Accepts: array
Accepts: string
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.
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"]
A set can track IDs reached through parent links.
enumerate records each node’s original position for tie-breaking.
heapq can prioritize ready nodes by their original positions.
Accepts: array
Accepts: string