Data Structures & AlgorithmsVery hardGATE CS · ICPC regional · Product Company SDE

An undirected graph has adjacency lists (neighbours visited in alphabetical order) A: [G]; B: [C]; C: [B, D]; D: [C, F, G]; E: [F]; F: [D, E]; G: [A, D]. What is the BFS traversal order starting from A?

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

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

BFS visits A, enqueues its unvisited neighbours in sorted order, then repeatedly dequeues the front of the queue and enqueues its unvisited neighbours, giving A, G, D, C, F, B, E. A DFS from the same start and adjacency lists would instead give A, G, D, C, B, F, E, since DFS dives down one branch before backtracking.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic