| from datetime import datetime |
| from collections import defaultdict |
| # - Uses a stack instead of recursion |
| # - Forfeits optimization involving tracking currently visited nodes |
| def toposort(g, steps=None): |
| # Get set of all nodes, including those without outgoing edges |
| all_nodes=list(keys|values) |
| # DFS stack, not using recursion |
| # visiting = [] - skip, don't expect 1000s of nodes, |E|/|V| is small |
| stack.insert(0,unmarked[0]) # push first unmarked |
| # Should not happen, if so there's a loop |
| noorder = list(set(steps) - set(order)) |
| graph_file=open('planetstack.deps').read() |
| g = json.loads(graph_file) |
| if (__name__=='__main__'): |
| #print toposort({'a':'b','b':'c','c':'d','d':'c'},['d','c','b','a']) |