GATE / CS ARCHIVE

SIMILAR QUESTIONS / ALGORITHMS

Check BFS traversal order against graph edges

2 questions solved the same way, newest first. Back to Algorithms

How these are solved

Both ask which option is a possible BFS traversal, but the 2017 graph has edge Q-O that the 2008 graph does not. The six labeled vertices are the same; adjacency structure and answer options change.

1 mark · MCQ
Report issue

Graph Algorithms

Question 15, GATE 2017: An undirected graph has vertices M,N,O,P,Q,R and edges MN,NO,MR,MQ,NQ,OQ,OP,PQ. Which is a possible BFS visiting order, with the first vertex as the start? A:MNOPQR. B:NQMPOR. C:QMNROP. D:POQNMR.
Tap to enlarge
Your answer
Answer & solution

Official answer: D

Step through it (4 options)

1. BFS removes vertices from a FIFO queue and discovers all vertices at a smaller distance before any at a larger distance. For D, start at P and enqueue its neighbors O then Q. The visited prefix is P,O,Q.

2. Processing O discovers N; its other neighbors P and Q are already discovered. Processing Q then discovers M, with N already queued. Processing N adds nothing, and processing M finally discovers R. The resulting order is P,O,Q,N,M,R.

3. A fails from M because O at distance 2 precedes its distance-1 neighbors Q and R. B fails from N because P at distance 2 precedes O at distance 1. C fails from Q because R at distance 2 precedes O and P at distance 1. Thus D is possible; BFS neighbor ordering need not be alphabetical.

Original paper, page 6 · Answer key

1 mark · MCQ
Report issue

Valid queue-based BFS order

Graph Algorithms

Question 19, GATE 2008: An undirected graph has vertices M,N,O,P,Q,R and edges MN,NO,OP,PQ,QM,QN,MR. Which is a possible queue-based breadth-first traversal?

(A) MNOPQR
(B) NQMPOR
(C) QMNPRO
(D) QMNPOR
Tap to enlarge
Your answer
Answer & solution

Answer: C (no official key was published)

Step through it (4 options)

1. BFS uses a FIFO queue, so all newly discovered neighbors enter behind previously queued vertices. Start at Q and discover M,N,P in that order.
2. Processing M next discovers R. Processing N then discovers O. Processing P adds nothing because O is already discovered.
3. The dequeue/visit order is Q,M,N,P,R,O, C. D puts O before R despite the specified first-layer order; the other offered orders violate BFS layers.

Original paper, page 4