Data Structures & AlgorithmsVery hardGATE CS · TCS NQT

In the undirected graph with edges (1,2), (1,3), (2,4), (3,4), (4,5), (3,6), how many vertices are at shortest-path distance exactly 2 from vertex 1?

  1. A. 3
  2. B. 2
  3. C. 4
  4. D. 5

Correct answer: B. 2

A BFS from vertex 1 assigns distance 1 to 2 and 3, then distance 2 to the unvisited neighbours 4 and 6. Vertex 5 is at distance 3.

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic