def topological_sort_as_sets(dependency_graph):
 todo = dependency_graph.copy()
 while todo:
  current = {node for (node, deps) in todo.items() if (len(deps) == 0)}
  if (not current):
   raise ValueError('Cyclic dependency in graph: {}'.format(', '.join((repr(x) for x in todo.items()))))
  (yield current)
  todo = {node: (dependencies - current) for (node, dependencies) in todo.items() if (node not in current)}

def topological_sort_as_sets ( dependency_graph ) : todo = dependency_graph . copy ( ) while todo : current = { node for ( node , deps ) in todo . items ( ) if ( len ( deps ) == 0 ) } if ( not current ) : raise ValueError ( 'Cyclic dependency in graph: {}' . format ( ', ' . join ( ( repr ( x ) for x in todo . items ( ) ) ) ) ) ( yield current ) todo = { node : ( dependencies - current ) for ( node , dependencies ) in todo . items ( ) if ( node not in current ) }
variation of kahns algorithm that returns sets .