Data Structures & AlgorithmsModerateGATE CS

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

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

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

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 B D H G C E F.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic