Created
January 9, 2019 15:12
-
-
Save d3lm/5b457b3538affbefe2cd50576a69c9c1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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