GATE CSE 2025 Session 2 Q59: Output of the double-BFS algorithm on a given tree
- Algorithms
- 2 marks
- NAT
- No negative marking
Question
Consider the following algorithm someAlgo that takes an undirected graph as input.
someAlgo()
1. Let be any vertex in . Run BFS on starting at . Let be a vertex in at maximum distance from as given by the BFS.
2. Run BFS on again with as the starting vertex. Let be the vertex at maximum distance from as given by the BFS.
3. Output the distance between and in .

The output of someAlgo() for the tree shown in the given figure is ___________. (Answer in integer)
Try it: numerical answer: type the value
Show answer and explanation
Answer: 6 (official GATE 2025 final answer key)
Concept used: On a tree, two BFS passes find a farthest pair of vertices, so the output is the tree's diameter.
First step: Find the longest path between two leaves: count edges from the lower-left leaf through the two central vertices to the far-right leaf.
Full step-by-step solution
Detailed working, common mistakes and linked mock tests will be available with a free GATEiit account. Log-in is coming soon.
Share this question
Source: official GATE 2025 Session 2 CS question paper and final answer key, organised by IIT Roorkee. Question paper · Answer key
More Algorithms PYQs
- 2026 Session 1 · Q17Solve nested recurrences T1 = 4T1(n/2) + T2, T2 = 5T2(n/4) + log n
- 2026 Session 1 · Q41Shortest paths with at most k edges: two statements
- 2026 Session 1 · Q47Condition forcing a vertex into every minimum vertex cover
- 2026 Session 1 · Q49Cycle and cut properties of MSTs with distinct weights
- 2026 Session 1 · Q50Impossible DFS discovery/finish orderings for a DAG edge
- 2026 Session 1 · Q55Properties of 2-colorable (bipartite) graphs