Question: we have discussed how to use DFS for topological sorting of a DAG. Someone tries to come up with something new by trying to use
we have discussed how to use DFS for topological sorting of a DAG. Someone tries to come up with something new by trying to use BFS for topological sorting of a DAG. Below is his/her code. Does his/her code work ? If yes, explain why ? If no, give a counter example.

time = 0; //global clock //G: the graph. s: the vertex to start with graph_BFS (G, S) { time = time + 1; //New operation s.start_time = time; //New operation s.visited = true; FIFO.enqueue (s); while (FIFO.size > 0) u = FIFO.dequeue (); print (u); time = time + 1; u.end_time = time; //New operation //New operation for every(u, v) in E if v.visited == false v.visited = rue; time = time + 1; v.start_time = time; FIFO.enqueue (v); //New operation //New operation } BFS (G) { for each vertex u lin G.V u.visited = false; for each u \in G.V if u.visited = false graph_BFS (G, u) } topological_sort (G) { 1. call BFS (G) to compute finishing time u.end_time for each vertex u 2. as each vertex is finished, insert it onto the *END* of a linked list 3. return the linked list of vertices }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
