code: def sort_topologically(children_by_node, node_list):
      """Topological sort of a graph.
  
      Parameters
      ----------
      children_by_node : dict
          Children for any node.
      node_list : list
          All nodes (some nodes may not have children and thus a separate
          parameter is needed).
  
      Returns
      -------
      list, nodes in the topological order
      """
      levels_by_node = {}
      nodes_by_level = defaultdict(set)
  
      def walk_depth_first(node):
          if node in levels_by_node:
              return levels_by_node[node]
          children = children_by_node[node]
          level = (
              0
              if not children
              else (
                  1
                  + max(
                      walk_depth_first(lname) for lname, _ in children
                  )
              )
          )
          levels_by_node[node] = level
          nodes_by_level[level].add(node)
          return level
  
      for node in node_list:
          walk_depth_first(node)
  
      nodes_by_level = list(
          takewhile(
              lambda x: x != [],
              (list(nodes_by_level[i]) for i in count()),
          )
      )
      return list(itertools.chain.from_iterable(nodes_by_level))
  
file_path: tffm/tffm/utils.py
imports: import math
  import numpy
  import tensorflow
  import itertools
  from itertools import combinations_with_replacement, takewhile, count
  from collections import defaultdict
label_desc: Use image node_id 2 for calling a global function with example usage: sort_topologically(children_by_node, node_list) and returns: list, level, levels_by_node