Graph Traversal (BFS and DFS) · Algorithms
16 questions · asked in 11 of 27 years (2000–2026)
MCQ 12 · MSQ 2 · NAT 21-mark 12 · 2-mark 4
Questions 6–10 of 16
1.a b e f d g c
2.a b e f c g d
3.a d g e b c f
4.a d b c g e f
A Depth First Search (DFS) is started at node a. The nodes are listed in the order they are first visited. Which of the above is/are possible output(s)?
Answer: (B)
1. : Once is visited, the next node must be or (its unvisited neighbours). It goes to , which is not a neighbour of . So this traversal is not valid.
2. : are all edges. has no unvisited neighbour, so DFS goes back to , then and . This is valid.
3. : are all edges, and each step goes to an unvisited neighbour. This is valid.
4. : Once is visited, the next node must be or . It goes to , which is not a neighbour of . So this traversal is not valid.
So only and are possible.
Answer: (B)
Answer: (C)
- BFS starts at . Its neighbours must all be visited before , which is two steps away. Here comes before and , so this is wrong.
- BFS starts at . Its neighbours are , but is not one of them. So must come before . Here it does not, so this is wrong.
- Start at . Its neighbours go into the queue. Visit : joins the queue. Visit : joins the queue. Visit : nothing new. The queue now gives and then . The order is , which is a valid BFS order.
- Here is visited before , so 's new neighbour is queued before 's new neighbour . So must be visited before , and this order is wrong.
Answer: (C)
Answer: (C)
Cost: each vertex is visited once, which takes , including the check for unvisited vertices. Each edge is scanned twice, once from each end, which takes . Total . Every vertex and every edge has to be looked at, so no algorithm can do better in general. and alone leave out part of the work, and is slower.
Answer: (C)
Answer: (D)
Why D always holds. Take any edge . When DFS scans this edge from , either is not yet visited or it is already finished. It cannot be still in progress, because that would be a back edge and a DAG has no cycle. If is not yet visited, it is visited inside the call of and ends before ends. If is already finished, it ended even earlier. Either way .
Why A, B and C can fail. Use the DAG in the figure, with edges , and . Start DFS at first: . Then start at : , go to : . The edge leads to a finished vertex, so , .
For the edge : (A fails), (B fails), (C fails), while holds.
Answer: (D)
Which of the following is NOT a topological ordering?
Answer: (D)
(A) 1 2 3 4 5 6: 1 is before 2 and 3; 2 and 3 are before 4; 4 is before 5 and 6; 2 is before 5; 3 is before 6. Valid.
(B) 1 3 2 4 5 6: the same checks hold. Valid.
(C) 1 3 2 4 6 5: 5 and 6 can be in either order, since there is no edge between them. Valid.
(D) 3 2 4 1 6 5: the edges and need 1 before 2 and 3, but here 3, 2 and 4 all come before 1. Not valid.
Answer: (D)
Keep practising