title: Incorrect order with Kahn's algorithm
body: <p>I'm trying to implement the topological ordering with Kahn's algorithm:</p>
  
  <pre><code>from collections import defaultdict
  
  def ordering(graph:dict):
      in_counts = defaultdict(int)
      for k, v in graph.items():
          if not k in in_counts:
              in_counts[k] = 0
          for in_node in v:
              in_counts[in_node] += 1
  
      queue = []
      for in_node in in_counts.keys():
          if in_counts[in_node] == 0:
              queue.append(in_node)
  
      topological_order = []
  
      while queue:
          print("in_counts:", in_counts)
          print("queue:", queue)
          current_node = queue.pop(0)
          topological_order.append(current_node)
          print("result:", topological_order)
          if not current_node in graph:
              continue
          for in_node in graph[current_node]:
              in_counts[in_node] -= 1
              if in_counts[in_node] == 0:
                  queue.append(in_node)
  
      return topological_order
  
  print(ordering({'GTTGA': ['CTGAG'], 'TTCAC': ['GTTGA'], 'AAGGC': ['GTTGA', 'TAACA', 'TTCAC'], 'CGGGC': ['GTTGA', 'TAACA'], 'CCCTC': ['GTTGA', 'TAACA', 'TTCAC', 'AAGGC', 'CGGGC', 'CTGAG'], 'AGCTT': ['GTTGA']}))
  
  </code></pre>
  
  <p>The debug output:</p>
  
  <pre><code>in_counts: defaultdict(&lt;class 'int'&gt;, {'GTTGA': 5, 'CTGAG': 2, 'TTCAC': 2, 'AAGGC': 1, 'TAACA': 3, 'CGGGC': 1, 'CCCTC': 0, 'AGCTT': 0})
  queue: ['CCCTC', 'AGCTT']
  result: ['CCCTC']
  in_counts: defaultdict(&lt;class 'int'&gt;, {'GTTGA': 4, 'CTGAG': 1, 'TTCAC': 1, 'AAGGC': 0, 'TAACA': 2, 'CGGGC': 0, 'CCCTC': 0, 'AGCTT': 0})
  queue: ['AGCTT', 'AAGGC', 'CGGGC']
  result: ['CCCTC', 'AGCTT']
  in_counts: defaultdict(&lt;class 'int'&gt;, {'GTTGA': 3, 'CTGAG': 1, 'TTCAC': 1, 'AAGGC': 0, 'TAACA': 2, 'CGGGC': 0, 'CCCTC': 0, 'AGCTT': 0})
  queue: ['AAGGC', 'CGGGC']
  result: ['CCCTC', 'AGCTT', 'AAGGC']
  in_counts: defaultdict(&lt;class 'int'&gt;, {'GTTGA': 2, 'CTGAG': 1, 'TTCAC': 0, 'AAGGC': 0, 'TAACA': 1, 'CGGGC': 0, 'CCCTC': 0, 'AGCTT': 0})
  queue: ['CGGGC', 'TTCAC']
  result: ['CCCTC', 'AGCTT', 'AAGGC', 'CGGGC']
  in_counts: defaultdict(&lt;class 'int'&gt;, {'GTTGA': 1, 'CTGAG': 1, 'TTCAC': 0, 'AAGGC': 0, 'TAACA': 0, 'CGGGC': 0, 'CCCTC': 0, 'AGCTT': 0})
  queue: ['TTCAC', 'TAACA']
  result: ['CCCTC', 'AGCTT', 'AAGGC', 'CGGGC', 'TTCAC']
  in_counts: defaultdict(&lt;class 'int'&gt;, {'GTTGA': 0, 'CTGAG': 1, 'TTCAC': 0, 'AAGGC': 0, 'TAACA': 0, 'CGGGC': 0, 'CCCTC': 0, 'AGCTT': 0})
  queue: ['TAACA', 'GTTGA']
  result: ['CCCTC', 'AGCTT', 'AAGGC', 'CGGGC', 'TTCAC', 'TAACA']
  in_counts: defaultdict(&lt;class 'int'&gt;, {'GTTGA': 0, 'CTGAG': 1, 'TTCAC': 0, 'AAGGC': 0, 'TAACA': 0, 'CGGGC': 0, 'CCCTC': 0, 'AGCTT': 0})
  queue: ['GTTGA']
  result: ['CCCTC', 'AGCTT', 'AAGGC', 'CGGGC', 'TTCAC', 'TAACA', 'GTTGA']
  in_counts: defaultdict(&lt;class 'int'&gt;, {'GTTGA': 0, 'CTGAG': 0, 'TTCAC': 0, 'AAGGC': 0, 'TAACA': 0, 'CGGGC': 0, 'CCCTC': 0, 'AGCTT': 0})
  queue: ['CTGAG']
  result: ['CCCTC', 'AGCTT', 'AAGGC', 'CGGGC', 'TTCAC', 'TAACA', 'GTTGA', 'CTGAG']
  </code></pre>
  
  <p>The expected result is:</p>
  
  <pre><code>['CCCTC', 'CGGGC', 'AGCTT', 'AAGGC', 'TAACA', 'TTCAC', 'GTTGA', 'CTGAG']
  </code></pre>
  
  <p>which is different from my result(see above).</p>
  
  <p>I understand that <code>CGGGC</code> should be placed right after <code>CCCTC</code>, however I don't understand how this would be not conflicting with the algorithm: at the start both <code>CCCTC</code> and <code>AGCTT</code> have the in-degree of <code>0</code>, and <code>CGGGC</code> gets on the queue after <code>AGCTT</code>.</p>
  
  <p>What am I missing?</p>