Skip to content

Instantly share code, notes, and snippets.

@aterrel
Created March 2, 2011 07:26
Show Gist options
  • Save aterrel/850598 to your computer and use it in GitHub Desktop.
Save aterrel/850598 to your computer and use it in GitHub Desktop.
def _rev_top_sort (self, dag):
"""Simple reverse top sorter given dag
dag: a dictionary with node:(neighbors)
returns a list of nodes in reversed topological order.
Used to determine update sequence.
"""
ret_list = []
visited = []
def _visit (node, deps):
if node in deps:
raise ValueError, "Given dag has a cycle."
if node not in visited:
visited.append(node)
deps = deps + [node]
map(lambda n: _visit(n, deps), dag[node])
ret_list.append(node)
map(lambda n: _visit(n, []), dag.keys())
return ret_list
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment