nodes: 0, module; 1, function_definition; 2, function_name:topological_sort; 3, parameters; 4, block; 5, identifier:dependency_pairs; 6, expression_statement; 7, expression_statement; 8, comment:# num arrows pointing in; 9, expression_statement; 10, comment:# list of arrows going out; 11, expression_statement; 12, comment:# unique list of heads in order first seen; 13, for_statement; 14, expression_statement; 15, for_statement; 16, expression_statement; 17, return_statement; 18, string:"Sort values subject to dependency constraints"; 19, assignment; 20, assignment; 21, assignment; 22, pattern_list; 23, identifier:dependency_pairs; 24, block; 25, assignment; 26, identifier:h; 27, identifier:ordered; 28, block; 29, assignment; 30, call; 31, identifier:num_heads; 32, call; 33, identifier:tails; 34, call; 35, identifier:heads; 36, list; 37, identifier:h; 38, identifier:t; 39, expression_statement; 40, if_statement; 41, identifier:ordered; 42, list_comprehension; 43, for_statement; 44, identifier:cyclic; 45, list_comprehension; 46, identifier:Results; 47, argument_list; 48, identifier:defaultdict; 49, argument_list; 50, identifier:defaultdict; 51, argument_list; 52, augmented_assignment; 53, comparison_operator:h in tails; 54, block; 55, else_clause; 56, identifier:h; 57, for_in_clause; 58, if_clause; 59, identifier:t; 60, subscript; 61, block; 62, identifier:n; 63, for_in_clause; 64, if_clause; 65, identifier:ordered; 66, identifier:cyclic; 67, identifier:int; 68, identifier:list; 69, subscript; 70, integer:1; 71, identifier:h; 72, identifier:tails; 73, expression_statement; 74, block; 75, identifier:h; 76, identifier:heads; 77, comparison_operator:h not in num_heads; 78, identifier:tails; 79, identifier:h; 80, expression_statement; 81, if_statement; 82, pattern_list; 83, call; 84, identifier:heads; 85, identifier:num_heads; 86, identifier:t; 87, call; 88, expression_statement; 89, expression_statement; 90, identifier:h; 91, identifier:num_heads; 92, augmented_assignment; 93, not_operator; 94, block; 95, identifier:n; 96, identifier:heads; 97, attribute; 98, argument_list; 99, attribute; 100, argument_list; 101, assignment; 102, call; 103, subscript; 104, integer:1; 105, subscript; 106, expression_statement; 107, identifier:num_heads; 108, identifier:items; 109, subscript; 110, identifier:append; 111, identifier:t; 112, subscript; 113, list; 114, attribute; 115, argument_list; 116, identifier:num_heads; 117, identifier:t; 118, identifier:num_heads; 119, identifier:t; 120, call; 121, identifier:tails; 122, identifier:h; 123, identifier:tails; 124, identifier:h; 125, identifier:t; 126, identifier:heads; 127, identifier:append; 128, identifier:h; 129, attribute; 130, argument_list; 131, identifier:ordered; 132, identifier:append; 133, identifier:t
code: def topological_sort(dependency_pairs):
      "Sort values subject to dependency constraints"
      num_heads = defaultdict(int)  # num arrows pointing in
      tails = defaultdict(list)  # list of arrows going out
      heads = []  # unique list of heads in order first seen
      for h, t in dependency_pairs:
          num_heads[t] += 1
          if h in tails:
              tails[h].append(t)
          else:
              tails[h] = [t]
              heads.append(h)
  
      ordered = [h for h in heads if h not in num_heads]
      for h in ordered:
          for t in tails[h]:
              num_heads[t] -= 1
              if not num_heads[t]:
                  ordered.append(t)
      cyclic = [n for n, heads in num_heads.items() if heads]
      return Results(ordered, cyclic)