Skip to content
GATEiit

GATE CSE 2025 Session 2 Q59: Output of the double-BFS algorithm on a given tree

Question

Consider the following algorithm someAlgo that takes an undirected graph GG as input.

someAlgo(GG)

1. Let vv be any vertex in GG. Run BFS on GG starting at vv. Let uu be a vertex in GG at maximum distance from vv as given by the BFS.

2. Run BFS on GG again with uu as the starting vertex. Let zz be the vertex at maximum distance from uu as given by the BFS.

3. Output the distance between uu and zz in GG.

An unlabelled tree with 14 vertices drawn as dots and lines
Tap the figure to enlarge

The output of someAlgo(TT) for the tree shown in the given figure is ___________. (Answer in integer)

Try it: numerical answer: type the value

Numerical answer. No negative marking.
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

WhatsAppDownload image

Source: official GATE 2025 Session 2 CS question paper and final answer key, organised by IIT Roorkee. Question paper · Answer key