nodes: 0, module; 1, function_definition; 2, function_name:sort_cyclic_graph_best_effort; 3, parameters; 4, block; 5, identifier:graph; 6, default_parameter; 7, expression_statement; 8, expression_statement; 9, expression_statement; 10, comment:# Go first on the pick_first chain then go back again on the others; 11, comment:# that were not visited. Given the way the graph is built both chains; 12, comment:# will always contain all the elements.; 13, if_statement; 14, expression_statement; 15, while_statement; 16, expression_statement; 17, while_statement; 18, return_statement; 19, identifier:pick_first; 20, string; 21, comment:"""Fallback for cases in which the graph has cycles."""; 22, assignment; 23, assignment; 24, comparison_operator:pick_first == 'head'; 25, block; 26, else_clause; 27, assignment; 28, comparison_operator:current is not None; 29, block; 30, assignment; 31, comparison_operator:current is not None; 32, block; 33, identifier:ordered; 34, string_content:head; 35, identifier:ordered; 36, list; 37, identifier:visited; 38, call; 39, identifier:pick_first; 40, string; 41, expression_statement; 42, block; 43, identifier:current; 44, identifier:FIRST; 45, identifier:current; 46, None; 47, expression_statement; 48, expression_statement; 49, if_statement; 50, identifier:current; 51, identifier:FIRST; 52, identifier:current; 53, None; 54, expression_statement; 55, expression_statement; 56, if_statement; 57, identifier:set; 58, argument_list; 59, string_content:head; 60, assignment; 61, expression_statement; 62, call; 63, assignment; 64, boolean_operator; 65, block; 66, call; 67, assignment; 68, boolean_operator; 69, block; 70, pattern_list; 71, tuple; 72, assignment; 73, attribute; 74, argument_list; 75, identifier:current; 76, call; 77, comparison_operator:current not in visited; 78, comparison_operator:current is not None; 79, expression_statement; 80, attribute; 81, argument_list; 82, identifier:current; 83, call; 84, comparison_operator:current not in visited; 85, comparison_operator:current is not None; 86, expression_statement; 87, identifier:fst_attr; 88, identifier:snd_attr; 89, string; 90, string; 91, pattern_list; 92, tuple; 93, identifier:visited; 94, identifier:add; 95, identifier:current; 96, identifier:getattr; 97, argument_list; 98, identifier:current; 99, identifier:visited; 100, identifier:current; 101, None; 102, call; 103, identifier:visited; 104, identifier:add; 105, identifier:current; 106, identifier:getattr; 107, argument_list; 108, identifier:current; 109, identifier:visited; 110, identifier:current; 111, None; 112, call; 113, string_content:head_node; 114, string_content:update_node; 115, identifier:fst_attr; 116, identifier:snd_attr; 117, string; 118, string; 119, subscript; 120, identifier:fst_attr; 121, attribute; 122, argument_list; 123, subscript; 124, identifier:snd_attr; 125, attribute; 126, argument_list; 127, string_content:update_node; 128, string_content:head_node; 129, identifier:graph; 130, identifier:current; 131, identifier:ordered; 132, identifier:append; 133, identifier:current; 134, identifier:graph; 135, identifier:current; 136, identifier:ordered; 137, identifier:append; 138, identifier:current
code: def sort_cyclic_graph_best_effort(graph, pick_first='head'):
      """Fallback for cases in which the graph has cycles."""
      ordered = []
      visited = set()
      # Go first on the pick_first chain then go back again on the others
      # that were not visited. Given the way the graph is built both chains
      # will always contain all the elements.
      if pick_first == 'head':
          fst_attr, snd_attr = ('head_node', 'update_node')
      else:
          fst_attr, snd_attr = ('update_node', 'head_node')
  
      current = FIRST
      while current is not None:
          visited.add(current)
          current = getattr(graph[current], fst_attr)
          if current not in visited and current is not None:
              ordered.append(current)
      current = FIRST
      while current is not None:
          visited.add(current)
          current = getattr(graph[current], snd_attr)
          if current not in visited and current is not None:
              ordered.append(current)
      return ordered