title: Topological sort (Kahn's algorithm) trouble
body: <p>I am having trouble wrapping my head around my code in the nested for loop. I am following the Kahn's algorithm here on wiki: <a href="https://en.wikipedia.org/wiki/Topological_sorting" rel="nofollow noreferrer">Kahn's</a>. I don't understand how to test for if outgoingEdge has incoming edges for each endArray element (m).</p>
  
  <p>Here is what I have so far:</p>
  
  <pre><code>    def topOrdering(self, graph):
  
      retList = []
      candidates = set()
      left = []
      right = []
  
      for key in graph:
          left.append(key)
          right.append(graph[key])
  
      flattenedRight = [val for sublist in right for val in sublist]
  
      for element in left:
          if element not in flattenedRight:
              #set of all nodes with no incoming edges
              candidates.add(element)
  
      candidates = sorted(candidates)
  
      while len(candidates) != 0:
          a = candidates.pop(0)
          retList.append(a)
          endArray = graph[a]
  
          for outGoingEdge in endArray:
  
              if outGoingEdge not in flattenedRight:
                  candidates.append(outGoingEdge)
                  #flattenedRight.remove(outGoingEdge)
  
              del outGoingEdge
  
  
      if not graph:
          return "the input graph is not a DAG"
      else:
          return retList
  </code></pre>
  
  <p>Here is a picture visualizing my algorithm. Graph is in a form of an adjacency list. 
  <a href="https://i.stack.imgur.com/7KqCq.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/7KqCq.jpg" alt="enter image description here"></a></p>