Skip to content

Instantly share code, notes, and snippets.

@d3lm
Created January 9, 2019 15:12
Show Gist options
  • Save d3lm/5b457b3538affbefe2cd50576a69c9c1 to your computer and use it in GitHub Desktop.
Save d3lm/5b457b3538affbefe2cd50576a69c9c1 to your computer and use it in GitHub Desktop.
def topology_sort(operation):
ordering = []
visited_nodes = set()
def recursive_helper(node):
if isinstance(node, Operation):
for input_node in node.input_nodes:
if input_node not in visited_nodes:
recursive_helper(input_node)
visited_nodes.add(node)
ordering.append(node)
# start recursive depth-first search
recursive_helper(operation)
return ordering
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment