code: def toposort(prereqs_d):
      """
      Sorts prereqs_d.keys() topologically.
  
      prereqs_d[x] contains all the elements that must come before x
      in the ordering.
  
      """
  
      #     all1 = set(prereqs_d.keys())
      #     all2 = set()
      #     for x, y in iteritems(prereqs_d):
      #         all2.update(y)
      #     print all1.difference(all2)
  
      seq = []
      done = set()
      postreqs_d = {}
      for x, prereqs in iteritems(prereqs_d):
          for prereq in prereqs:
              postreqs_d.setdefault(prereq, set()).add(x)
      next = set([k for k in prereqs_d if not prereqs_d[k]])
      while next:
          bases = next
          next = set()
          for x in bases:
              done.add(x)
              seq.append(x)
          for x in bases:
              for postreq in postreqs_d.get(x, []):
                  if not prereqs_d[postreq].difference(done):
                      next.add(postreq)
      if len(prereqs_d) != len(seq):
          raise Exception(
              "Cannot sort topologically: there might be cycles, "
              "prereqs_d does not have a key for each element or "
              "some orderings contain invalid elements."
          )
      return seq
  
imports: from __future__ import absolute_import, print_function, division
  import linecache
  import sys
  import traceback
  import numpy
  from six import iteritems, integer_types, string_types, with_metaclass
  from six.moves import StringIO
  from theano import config
  from theano.compat import PY3
label_desc: Use image node_id 11 for calling a global function with example usage: toposort(prereqs_d) and returns: seq