graph algorithms void graph::topsortforint counter counter num_verticescounter+vertex findnewvertexofindegreezero)ifv =not_a_vertex throw cyclefoundexception} topnum counterfor each vertex adjacent to indegree--figure simple topological sort pseudocode graph is sparsewe would expect that only few vertices have their indegrees updated during each iteration howeverin the search for vertex of indegree we look at (potentiallyall the verticeseven though only few have changed we can remove this inefficiency by keeping all the (unassignedvertices of indegree in special box the findnewvertexofindegreezero function then returns (and removesany vertex in the box when we decrement the indegrees of the adjacent verticeswe check each vertex and place it in the box if its indegree falls to to implement the boxwe can use either stack or queuewe will use queue firstthe indegree is computed for every vertex then all vertices of indegree are placed on an initially empty queue while the queue is not emptya vertex is removedand all vertices adjacent to have their indegrees decremented vertex is put on the queue as soon as its indegree falls to the topological ordering then is the order in which the vertices dequeue figure shows the status after each phase vertex indegree before dequeue enqueue dequeue figure result of applying topological sort to the graph in figure