title: topological sort using queue in a graph
body: <p>The below is my reading of a topological sort algorithm on a queue, written in my textbook:</p>
  
  <pre><code>void topologicalsort(struct Graph* G){
      struct queue* Q;
      int counter;
      int v,w;
      Q=createqueue();
      counter=0;
      for(v=0;v&lt;G-&gt;V;v++){
         if(indegree[v]==0)
            enqueue(Q,v);
         while(!isemptyqueue(Q)){
            v=dequeue(Q);
            topologicalorder[v]=++counter;
            for each w to adjacent to v
               if(--indegree[w]==0){
               enqueue(Q,w);
               }
  
  
         }
      } 
  } 
  </code></pre>
  
  <p>The algorithm is failing for the following graph:</p>
  
  <p><a href="https://i.stack.imgur.com/cHZ7U.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/cHZ7U.jpg" alt="graph the algorithm fails on"></a></p>
  
  <p>If in the given graph initially 7 5 3 has in-degree zero then they will be inserted in the queue but for any of the vertices adjacent to <code>7 5 3</code> we are not having any vertex with degree 1. This implies that <code>if(--indegree[w]==0)</code> will not hold true for <code>7 5 3</code> hence there will be no further enqueuing inside the queue, and hence the algorithm will not process further vertices. <strong>I'd like to know why is the algorithm failing if the graph is a DAG? In which way is it incorrect?</strong></p>
  
  <p>I know that we can also implement topological sort using DFS, but I want to implement the below as-is:</p>
  
  <p><a href="https://i.stack.imgur.com/86vI3.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/86vI3.jpg" alt="the pseudocode of the algorithm I want to implement"></a></p>