nodes: 0, module; 1, function_definition; 2, function_name:sort_topologically; 3, parameters; 4, block; 5, identifier:graph; 6, expression_statement; 7, expression_statement; 8, expression_statement; 9, function_definition; 10, function_definition; 11, for_statement; 12, return_statement; 13, comment:"""
      Stackless topological sorting.
  
      graph = {
          3: [1],
          5: [3],
          4: [2],
          6: [4],
      }
  
      sort_topologically(graph)
      [[1, 2], [3, 4], [5, 6]]
      """; 14, assignment; 15, assignment; 16, function_name:add_level_to_name; 17, parameters; 18, block; 19, function_name:walk_depth_first; 20, parameters; 21, block; 22, identifier:name; 23, identifier:graph; 24, block; 25, call; 26, identifier:levels_by_name; 27, dictionary; 28, identifier:names_by_level; 29, call; 30, identifier:name; 31, identifier:level; 32, expression_statement; 33, expression_statement; 34, identifier:name; 35, expression_statement; 36, while_statement; 37, expression_statement; 38, identifier:list; 39, argument_list; 40, identifier:defaultdict; 41, argument_list; 42, assignment; 43, call; 44, assignment; 45, parenthesized_expression; 46, block; 47, call; 48, call; 49, identifier:list; 50, subscript; 51, identifier:level; 52, attribute; 53, argument_list; 54, identifier:stack; 55, list; 56, identifier:stack; 57, expression_statement; 58, if_statement; 59, if_statement; 60, expression_statement; 61, expression_statement; 62, if_statement; 63, expression_statement; 64, expression_statement; 65, identifier:walk_depth_first; 66, argument_list; 67, attribute; 68, argument_list; 69, identifier:levels_by_name; 70, identifier:name; 71, subscript; 72, identifier:append; 73, identifier:name; 74, identifier:name; 75, assignment; 76, comparison_operator:name in levels_by_name; 77, block; 78, boolean_operator; 79, block; 80, assignment; 81, assignment; 82, identifier:children_not_calculated; 83, block; 84, assignment; 85, call; 86, identifier:name; 87, identifier:itertools; 88, identifier:takewhile; 89, lambda; 90, generator_expression; 91, identifier:names_by_level; 92, identifier:level; 93, identifier:name; 94, call; 95, identifier:name; 96, identifier:levels_by_name; 97, continue_statement; 98, comparison_operator:name not in graph; 99, not_operator; 100, expression_statement; 101, expression_statement; 102, continue_statement; 103, identifier:children; 104, subscript; 105, identifier:children_not_calculated; 106, list_comprehension; 107, expression_statement; 108, expression_statement; 109, continue_statement; 110, identifier:level; 111, binary_operator:1 + max(levels_by_name[lname] for lname in children); 112, identifier:add_level_to_name; 113, argument_list; 114, lambda_parameters; 115, comparison_operator:x is not None; 116, call; 117, for_in_clause; 118, attribute; 119, argument_list; 120, identifier:name; 121, identifier:graph; 122, subscript; 123, assignment; 124, call; 125, identifier:graph; 126, identifier:name; 127, identifier:child; 128, for_in_clause; 129, if_clause; 130, call; 131, call; 132, integer:1; 133, call; 134, identifier:name; 135, identifier:level; 136, identifier:x; 137, identifier:x; 138, None; 139, attribute; 140, argument_list; 141, identifier:i; 142, call; 143, identifier:stack; 144, identifier:pop; 145, identifier:graph; 146, identifier:name; 147, identifier:level; 148, integer:0; 149, identifier:add_level_to_name; 150, argument_list; 151, identifier:child; 152, identifier:children; 153, comparison_operator:child not in levels_by_name; 154, attribute; 155, argument_list; 156, attribute; 157, argument_list; 158, identifier:max; 159, generator_expression; 160, identifier:names_by_level; 161, identifier:get; 162, identifier:i; 163, None; 164, attribute; 165, argument_list; 166, identifier:name; 167, identifier:level; 168, identifier:child; 169, identifier:levels_by_name; 170, identifier:stack; 171, identifier:append; 172, identifier:name; 173, identifier:stack; 174, identifier:extend; 175, identifier:children_not_calculated; 176, subscript; 177, for_in_clause; 178, identifier:itertools; 179, identifier:count; 180, identifier:levels_by_name; 181, identifier:lname; 182, identifier:lname; 183, identifier:children
code: def sort_topologically(graph):
      """
      Stackless topological sorting.
  
      graph = {
          3: [1],
          5: [3],
          4: [2],
          6: [4],
      }
  
      sort_topologically(graph)
      [[1, 2], [3, 4], [5, 6]]
      """
      levels_by_name = {}
      names_by_level = defaultdict(list)
  
      def add_level_to_name(name, level):
          levels_by_name[name] = level
          names_by_level[level].append(name)
  
  
      def walk_depth_first(name):
          stack = [name]
          while(stack):
              name = stack.pop()
              if name in levels_by_name:
                  continue
  
              if name not in graph or not graph[name]:
                  level = 0
                  add_level_to_name(name, level)
                  continue
  
              children = graph[name]
  
              children_not_calculated = [child for child in children if child not in levels_by_name]
              if children_not_calculated:
                  stack.append(name)
                  stack.extend(children_not_calculated)
                  continue
  
              level = 1 + max(levels_by_name[lname] for lname in children)
              add_level_to_name(name, level)
  
      for name in graph:
          walk_depth_first(name)
  
      return list(itertools.takewhile(lambda x: x is not None,
                                      (names_by_level.get(i, None)
                                       for i in itertools.count())))