GATE / CS ARCHIVE

GATE CSE 2017 SET 2 / QUESTION 15

GATE CSE 2017 Set 2 Q15

Algorithms · Graph Algorithms · Full GATE CSE 2017 Set 2 paper

GATE 2017 Set 2, Q15 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