Data Structures & AlgorithmsModerateGATE CS

An undirected graph has edges A–D, B–D, B–F, C–D, C–E, D–G, E–F, E–H, F–G, G–H. BFS starts at A and always scans neighbours in alphabetical order. In what order are the vertices visited?

  1. A. A D B F E C H G
  2. B. A D G H E F B C
  3. C. A D B C G F E H
  4. D. A D B C G E F H

Correct answer: C. A D B C G F E H

BFS dequeues level by level, and within a level in the order vertices were enqueued (which depends on their parents, not on alphabetical order): A D B C G F E H.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic